More clean handling of the contextpath.

Now using long option names for the options of converge to be consistent with the other components.
This commit is contained in:
Erik Brakkee 2024-08-04 22:17:51 +02:00
parent 1b76add15b
commit 46d4467e94
3 changed files with 42 additions and 26 deletions

View File

@ -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" ]

View File

@ -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,11 +50,11 @@ func printHelp(msg string) {
"both on linux and on windows.\n" +
"\n" +
"Options\n" +
"-s <contentdir>: directory where static content of converge is placed\n" +
"-d <downloaddir>: directory where downloads of converge are placed\n" +
"-c <contextpath>: by default all content is served at /. Use this option to specify\n" +
"--static <contentdir>: directory where static content of converge is placed\n" +
"--downloads <downloaddir>: directory where downloads of converge are placed\n" +
"--ccntext <contextpath>: 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" +
" 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")

View File

@ -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)
}