Add exports to make router usable as a library
This commit is contained in:
@@ -42,10 +42,11 @@ func (fh *functionHandler) handler(responseWriter http.ResponseWriter, request *
|
|||||||
// Cache miss: request the Pool Manager to make a new service.
|
// Cache miss: request the Pool Manager to make a new service.
|
||||||
serviceUrl, poolErr := fh.getServiceForFunction()
|
serviceUrl, poolErr := fh.getServiceForFunction()
|
||||||
if poolErr != nil {
|
if poolErr != nil {
|
||||||
// now we're really screwed
|
|
||||||
log.Printf("Failed to get service for function (%v,%v): %v",
|
log.Printf("Failed to get service for function (%v,%v): %v",
|
||||||
fh.Function.Name, fh.Function.Uid, poolErr)
|
fh.Function.Name, fh.Function.Uid, poolErr)
|
||||||
responseWriter.WriteHeader(500) // TODO: make this smarter based on the actual error
|
// We might want a specific error code or header for fission
|
||||||
|
// failures as opposed to user function bugs.
|
||||||
|
http.Error(responseWriter, poolErr.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-27
@@ -42,19 +42,9 @@ package router
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
flag "github.com/ogier/pflag"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
|
||||||
options struct {
|
|
||||||
port int
|
|
||||||
poolManagerUrl string
|
|
||||||
controllerUrl string
|
|
||||||
//...
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// request url ---[mux]---> Function(name,uid) ----[fmap]----> k8s service url
|
// request url ---[mux]---> Function(name,uid) ----[fmap]----> k8s service url
|
||||||
|
|
||||||
// request url ---[trigger]---> Function(name, deployment) ----[deployment]----> Function(name, uid) ----[pool mgr]---> k8s service url
|
// request url ---[trigger]---> Function(name, deployment) ----[deployment]----> Function(name, uid) ----[pool mgr]---> k8s service url
|
||||||
@@ -66,27 +56,14 @@ func router(httpTriggerSet *HTTPTriggerSet) *mutableRouter {
|
|||||||
return mr
|
return mr
|
||||||
}
|
}
|
||||||
|
|
||||||
func server(port int, httpTriggerSet *HTTPTriggerSet) {
|
func serve(port int, httpTriggerSet *HTTPTriggerSet) {
|
||||||
mr := router(httpTriggerSet)
|
mr := router(httpTriggerSet)
|
||||||
url := fmt.Sprintf(":%v", port)
|
url := fmt.Sprintf(":%v", port)
|
||||||
http.ListenAndServe(url, mr)
|
http.ListenAndServe(url, mr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getOptions() *options {
|
func Start(port int, controllerUrl string, poolmgrUrl string) {
|
||||||
options := &options{}
|
|
||||||
|
|
||||||
flag.IntVar(&options.port, "port", 80, "Port to listen on")
|
|
||||||
|
|
||||||
// default to using dns service discovery
|
|
||||||
flag.StringVar(&options.poolManagerUrl, "poolmanager_url", "http://poolmanager/", "URL for the PoolManager service")
|
|
||||||
flag.StringVar(&options.controllerUrl, "controller_url", "http://controller/", "URL for the controller service")
|
|
||||||
|
|
||||||
return options
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
options := getOptions()
|
|
||||||
fmap := makeFunctionServiceMap()
|
fmap := makeFunctionServiceMap()
|
||||||
triggers := makeHTTPTriggerSet(fmap, options.controllerUrl, options.poolManagerUrl)
|
triggers := makeHTTPTriggerSet(fmap, controllerUrl, poolmgrUrl)
|
||||||
server(options.port, triggers)
|
serve(port, triggers)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func TestRouter(t *testing.T) {
|
|||||||
triggers.triggers = append(triggers.triggers, fission.HTTPTrigger{UrlPattern: triggerUrl, Function: *fn})
|
triggers.triggers = append(triggers.triggers, fission.HTTPTrigger{UrlPattern: triggerUrl, Function: *fn})
|
||||||
|
|
||||||
port := 4242
|
port := 4242
|
||||||
go server(port, triggers)
|
go serve(port, triggers)
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
testUrl := fmt.Sprintf("http://localhost:%v%v", port, triggerUrl)
|
testUrl := fmt.Sprintf("http://localhost:%v%v", port, triggerUrl)
|
||||||
|
|||||||
Reference in New Issue
Block a user