diff --git a/Dockerfile b/Dockerfile index a5d6b8d..07c1aa2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,4 +28,4 @@ COPY --from=builder /opt/converge/bin/agent \ /opt/converge/static/ COPY --from=builder /opt/converge/static/ /opt/converge/static/ -ENTRYPOINT ["/opt/converge/bin/converge", "-s", "/opt/converge/static", "-d", "/opt/converge/static" ] +ENTRYPOINT ["/opt/converge/bin/converge", "--static", "/opt/converge/static", "--downloads", "/opt/converge/static" ] diff --git a/cmd/converge/converge.go b/cmd/converge/converge.go index 531f4c2..fb5f0c6 100644 --- a/cmd/converge/converge.go +++ b/cmd/converge/converge.go @@ -29,7 +29,7 @@ func parsePublicId(path string) (publicId string, _ error) { func catchAllHandler(contextPath string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, contextPath+"/docs", http.StatusFound) + http.Redirect(w, r, contextPath+"docs", http.StatusFound) } } @@ -50,12 +50,12 @@ func printHelp(msg string) { "both on linux and on windows.\n" + "\n" + "Options\n" + - "-s : directory where static content of converge is placed\n" + - "-d : directory where downloads of converge are placed\n" + - "-c : by default all content is served at /. Use this option to specify\n" + - " a different context path. For instance to host converge at a base\n" + - " URL of https://example.com/converge, specify /converg (and without\n" + - " trailing slash. " + "--static : directory where static content of converge is placed\n" + + "--downloads : directory where downloads of converge are placed\n" + + "--ccntext : by default all content is served at /. Use this option to specify\n" + + " a different context path. For instance to host converge at a base\n" + + " URL of https://example.com/converge/, specify /converge/ (with\n" + + " trailing slash. " fmt.Fprintln(os.Stderr, helpText) os.Exit(1) } @@ -64,26 +64,26 @@ func main() { downloaddir := "." staticdir := "../static" - contextpath := "" + contextpath := "/" args := os.Args[1:] for len(args) > 0 && strings.HasPrefix(args[0], "-") { switch args[0] { - case "-d": + case "--downloads": if len(args) <= 1 { - printHelp("The -d option expects an argument") + printHelp("The --downloads option expects an argument") } downloaddir = args[1] args = args[1:] - case "-s": + case "--static": if len(args) <= 1 { - printHelp("The -s option expects an argument") + printHelp("The --static option expects an argument") } staticdir = args[1] args = args[1:] - case "-c": + case "--context": if len(args) <= 1 { - printHelp("The -c option expects an argument") + printHelp("The --context option expects an argument") } contextpath = args[1] args = args[1:] @@ -174,24 +174,23 @@ func main() { // websocket endpoints // TODO remove, simulate contextpath + context := HttpContext{path: contextpath} - http.HandleFunc(contextpath+"/agent/", registrationService.Handle) - http.HandleFunc(contextpath+"/client/", clientService.Handle) - http.HandleFunc(contextpath+"/ws/sessions", sessionService.Handle) + context.HandleFunc("agent/", registrationService.Handle) + context.HandleFunc("client/", clientService.Handle) + context.HandleFunc("ws/sessions", sessionService.Handle) // create filehandler with templating for html files. - http.Handle(contextpath+"/docs/", http.StripPrefix(contextpath+"/docs/", http.HandlerFunc(pageHandler))) - http.Handle(contextpath+"/static/", http.StripPrefix(contextpath+"/static/", + context.Handle("docs/", http.StripPrefix("docs/", http.HandlerFunc(pageHandler))) + context.Handle("static/", http.StripPrefix("static/", http.FileServer(http.Dir(staticdir)))) - http.Handle(contextpath+"/downloads/", http.StripPrefix(contextpath+"/downloads/", + context.Handle("downloads/", http.StripPrefix("downloads/", http.FileServer(http.Dir(downloaddir)))) - // TODO remove for testing contextpath - - http.HandleFunc(contextpath+"/", catchAllHandler(contextpath)) - // create usage generator - http.HandleFunc(contextpath+"/usage", generateCLIExammple) + context.HandleFunc("usage", generateCLIExammple) + + http.HandleFunc("/", catchAllHandler(contextpath)) // Start HTTP server fmt.Println("Rendez-vous server listening on :8000") diff --git a/cmd/converge/httpcontext.go b/cmd/converge/httpcontext.go new file mode 100644 index 0000000..04a81e7 --- /dev/null +++ b/cmd/converge/httpcontext.go @@ -0,0 +1,17 @@ +package main + +import "net/http" + +// dealing with the context path in a simple way + +type HttpContext struct { + path string +} + +func (context HttpContext) Handle(pattern string, handler http.Handler) { + http.Handle(context.path+pattern, http.StripPrefix(context.path, handler)) +} + +func (context HttpContext) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) { + http.HandleFunc(context.path+pattern, handler) +}