Skip to content
Snippets Groups Projects
Commit c26b7fd68dd7 authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

cmd: Fix & improve WithUI()

parent 849953bbb703
No related branches found
No related tags found
No related merge requests found
Pipeline #113920 failed
package cmd
import (
"fmt"
"net/http"
"strconv"
......@@ -66,5 +65,4 @@
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) {
......@@ -70,5 +68,4 @@
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")
......
......@@ -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,8 +108,8 @@
}
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) {
......@@ -98,8 +113,8 @@
}
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,7 +137,7 @@
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)
}
......@@ -126,6 +141,7 @@
if err != nil {
panic(err)
}
g.Namespace = cfg.Name
serveFound = true
break
......@@ -134,6 +150,6 @@
if !serveFound {
panic("serve command not found")
}
})
})(program)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment