# HG changeset patch # User Christophe de Vienne <christophe@cdevienne.info> # Date 1732039906 -3600 # Tue Nov 19 19:11:46 2024 +0100 # Node ID c26b7fd68dd753795a6f1f0920ac64c02cfeb824 # Parent 849953bbb7039881f88be35a4d655aaa8c807fab cmd: Fix & improve WithUI() diff --git a/cmd/oapi.go b/cmd/oapi.go --- a/cmd/oapi.go +++ b/cmd/oapi.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "net/http" "strconv" @@ -66,9 +65,7 @@ return func(program *Program) { program.Version.APIVersion = oapi.Info().Title + " " + oapi.Info().Version WithMiddleware(func(next http.Handler) http.Handler { - fmt.Println("oapi middleware") return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - fmt.Println("oapi", r.URL.Path) if r.URL.Path == "/swagger.json" { json := oapi.JSON() rw.Header().Set("Content-Type", "application/json") diff --git a/cmd/program_ui.go b/cmd/program_ui.go --- a/cmd/program_ui.go +++ b/cmd/program_ui.go @@ -16,7 +16,7 @@ fs fs.FS paths []string - External string `long:"ui-external" ini-name:"ui-ui-external" description:"UI external server"` + External string `long:"external" ini-name:"external" description:"UI external server"` } func NewIgnoreNotFoundResponseWriter(rw http.ResponseWriter) *IgnoreNotFoundResponseWriter { @@ -74,7 +74,22 @@ return rw.next.Write(data) } -func WithUI(uifs fs.FS, prefix string) Option { +type UIConfig struct { + Name string + Description string + Prefix string +} + +func WithUI(uifs fs.FS, cfg *UIConfig) Option { + if cfg == nil { + cfg = &UIConfig{} + } + if cfg.Name == "" { + cfg.Name = "ui" + } + if cfg.Description == "" { + cfg.Description = "User Interface" + } uiOptions := UIOptions{ fs: uifs, } @@ -93,13 +108,13 @@ } uiHandler = httputil.NewSingleHostReverseProxy(u) } - if prefix != "" { - uiHandler = http.StripPrefix(prefix, uiHandler) + if cfg.Prefix != "" { + uiHandler = http.StripPrefix(cfg.Prefix, uiHandler) } return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - if prefix != "" { - if !strings.HasPrefix(r.URL.Path, prefix) { + if cfg.Prefix != "" { + if !strings.HasPrefix(r.URL.Path, cfg.Prefix) { next.ServeHTTP(rw, r) } } @@ -122,10 +137,11 @@ var serveFound bool for _, cmd := range program.Parser.Commands() { if cmd.Name == "serve" { - _, err := cmd.AddGroup("ui", "User Interface", &uiOptions) + g, err := cmd.AddGroup(cfg.Name, cfg.Description, &uiOptions) if err != nil { panic(err) } + g.Namespace = cfg.Name serveFound = true break @@ -134,6 +150,6 @@ if !serveFound { panic("serve command not found") } - }) + })(program) } }