diff options
author | sanine-a <sanine.not@pm.me> | 2023-05-15 13:01:25 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-05-15 13:01:25 -0500 |
commit | 8b47f868ce97f50757225a72b958ed483336fd0c (patch) | |
tree | 992e736787868e599fe06267aebbbd9d968e8b37 | |
parent | 9db22ca2f86f41f98baa23a8e53d69c22ece2a67 (diff) |
add ability to serve under subdirectory
-rw-r--r-- | config/config.go | 1 | ||||
-rw-r--r-- | main.go | 10 | ||||
-rw-r--r-- | proxy.go | 6 |
3 files changed, 9 insertions, 8 deletions
diff --git a/config/config.go b/config/config.go index a371beb..85c73f0 100644 --- a/config/config.go +++ b/config/config.go @@ -21,6 +21,7 @@ type Endpoint struct { type Config struct { ListenAddress string + PathPrefix string AssetDirectory string LoginTimeout int Users []User @@ -25,21 +25,21 @@ func main() { users := getUsers(conf) // add phlox endpoints - http.HandleFunc("/phlox/login", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(conf.PathPrefix + "/phlox/login", func(w http.ResponseWriter, r *http.Request) { Login(w, r, users, sessions, pages) }) - http.HandleFunc("/phlox/logout", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(conf.PathPrefix + "/phlox/logout", func(w http.ResponseWriter, r *http.Request) { Logout(w, r, sessions) }) - http.HandleFunc("/phlox/asset/", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(conf.PathPrefix + "/phlox/asset/", func(w http.ResponseWriter, r *http.Request) { filename := strings.TrimPrefix(r.URL.Path, "/phlox/asset/") path := filepath.Join(conf.AssetDirectory, filename) http.ServeFile(w, r, path) }) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(conf.PathPrefix + "/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { w.Header().Add("Location", "/phlox/login") w.WriteHeader(http.StatusTemporaryRedirect) @@ -50,7 +50,7 @@ func main() { // add reverse proxy endpoints for _, e := range conf.Endpoints { - addEndpoint(sessions, pages, e) + addEndpoint(conf, sessions, pages, e) } // timer for inactivity log out @@ -19,7 +19,7 @@ type Endpoint struct { } -func addEndpoint(s *auth.Sessions, pages page.Pages, e config.Endpoint) { +func addEndpoint(conf config.Config, s *auth.Sessions, pages page.Pages, e config.Endpoint) { log.Infof("proxying endpoint %v to %v", e.Path, e.Address) origin, err := url.Parse(e.Address) if err != nil { @@ -31,12 +31,12 @@ func addEndpoint(s *auth.Sessions, pages page.Pages, e config.Endpoint) { Origin: origin, } - http.HandleFunc(e.Path, func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(conf.PathPrefix + e.Path, func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Location", e.Path + "/") w.WriteHeader(http.StatusPermanentRedirect) }) - http.HandleFunc(e.Path + "/", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(conf.PathPrefix + e.Path + "/", func(w http.ResponseWriter, r *http.Request) { log.Infof("REQ: %v", r.URL.Path) proxy(w, r, s, pages, end) }) |