Add exports to make router usable as a library

This commit is contained in:
Soam Vasani
2016-10-29 20:40:41 -07:00
parent 83c995e019
commit fbd76149f5
3 changed files with 8 additions and 30 deletions
+3 -2
View File
@@ -42,10 +42,11 @@ func (fh *functionHandler) handler(responseWriter http.ResponseWriter, request *
// Cache miss: request the Pool Manager to make a new service.
serviceUrl, poolErr := fh.getServiceForFunction()
if poolErr != nil {
// now we're really screwed
log.Printf("Failed to get service for function (%v,%v): %v",
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
}
+4 -27
View File
@@ -42,19 +42,9 @@ package router
import (
"fmt"
"github.com/gorilla/mux"
flag "github.com/ogier/pflag"
"net/http"
)
type (
options struct {
port int
poolManagerUrl string
controllerUrl string
//...
}
)
// 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
@@ -66,27 +56,14 @@ func router(httpTriggerSet *HTTPTriggerSet) *mutableRouter {
return mr
}
func server(port int, httpTriggerSet *HTTPTriggerSet) {
func serve(port int, httpTriggerSet *HTTPTriggerSet) {
mr := router(httpTriggerSet)
url := fmt.Sprintf(":%v", port)
http.ListenAndServe(url, mr)
}
func getOptions() *options {
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()
func Start(port int, controllerUrl string, poolmgrUrl string) {
fmap := makeFunctionServiceMap()
triggers := makeHTTPTriggerSet(fmap, options.controllerUrl, options.poolManagerUrl)
server(options.port, triggers)
triggers := makeHTTPTriggerSet(fmap, controllerUrl, poolmgrUrl)
serve(port, triggers)
}
+1 -1
View File
@@ -38,7 +38,7 @@ func TestRouter(t *testing.T) {
triggers.triggers = append(triggers.triggers, fission.HTTPTrigger{UrlPattern: triggerUrl, Function: *fn})
port := 4242
go server(port, triggers)
go serve(port, triggers)
time.Sleep(100 * time.Millisecond)
testUrl := fmt.Sprintf("http://localhost:%v%v", port, triggerUrl)