From 16c91c1139d9f03f42c63298f2179822b8d50eb2 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 15 May 2023 18:47:06 +0000 Subject: properly implement subpath handling --- main.go | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 6e37f43..44fed27 100644 --- a/main.go +++ b/main.go @@ -16,41 +16,52 @@ import ( log "github.com/sirupsen/logrus" ) + + + func main() { - configFile := parseFlags() + configFile := parseFlags() conf := loadConfig(configFile) + api := config.Api{ + Prefix: conf.PathPrefix, + Login: "/phlox/login", + Logout: "/phlox/logout", + Asset: "/phlox/asset/", + } + sessions := auth.NewSessionContainer() - pages := loadPages(conf) + pages := loadPages(conf, api) users := getUsers(conf) // add phlox endpoints - http.HandleFunc(conf.PathPrefix + "/phlox/login", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(api.Login, func(w http.ResponseWriter, r *http.Request) { Login(w, r, users, sessions, pages) }) - http.HandleFunc(conf.PathPrefix + "/phlox/logout", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(api.Logout, func(w http.ResponseWriter, r *http.Request) { Logout(w, r, sessions) }) - http.HandleFunc(conf.PathPrefix + "/phlox/asset/", func(w http.ResponseWriter, r *http.Request) { - filename := strings.TrimPrefix(r.URL.Path, "/phlox/asset/") + http.HandleFunc(api.Asset, func(w http.ResponseWriter, r *http.Request) { + filename := strings.TrimPrefix(r.URL.Path, api.Asset) path := filepath.Join(conf.AssetDirectory, filename) http.ServeFile(w, r, path) }) - http.HandleFunc(conf.PathPrefix + "/", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { - w.Header().Add("Location", "/phlox/login") + w.Header().Add("Location", api.Prefix + "/phlox/login") w.WriteHeader(http.StatusTemporaryRedirect) } else { pages.ServeError404(w) + log.Errorf("404: %v", r.URL.Path) } }) // add reverse proxy endpoints for _, e := range conf.Endpoints { - addEndpoint(conf, sessions, pages, e) + addEndpoint(conf, api, sessions, pages, e) } // timer for inactivity log out @@ -70,7 +81,7 @@ func main() { func parseFlags() string { var configFile string var username string - var passwd string + var passwd string flag.StringVar(&configFile, "c", "./config.json", "the configuration file to use") flag.StringVar(&passwd, "passwd", "", "hash a password") flag.StringVar(&username, "user", "", "optional username for the JSON output of --passwd") @@ -122,8 +133,8 @@ func loadConfig(filename string) config.Config { } -func loadPages(c config.Config) page.Pages { - pages, err := page.LoadPages(c) +func loadPages(c config.Config, api config.Api) page.Pages { + pages, err := page.LoadPages(c, api) if err != nil { fmt.Fprintf(os.Stderr, "failed to load html pages: %v\n", err.Error()) os.Exit(1) -- cgit v1.2.1