diff options
author | sanine <sanine.not@pm.me> | 2023-05-15 18:47:06 +0000 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-05-15 18:47:06 +0000 |
commit | 16c91c1139d9f03f42c63298f2179822b8d50eb2 (patch) | |
tree | 66b301c2c118478f3fffe474425de24cf121dd26 /main.go | |
parent | 8b47f868ce97f50757225a72b958ed483336fd0c (diff) |
properly implement subpath handlingmain
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 35 |
1 files changed, 23 insertions, 12 deletions
@@ -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) |