From fe2ce513e2026e5cf38f4742fb3884531ef43ae0 Mon Sep 17 00:00:00 2001 From: Jarno Rankinen Date: Sat, 10 Feb 2024 19:27:12 +0200 Subject: [PATCH] Support for running under a subpath, log connections --- main.go | 19 ++++++++++++++----- resume.go | 9 +++++---- templates/index.html | 6 +++--- templates/metatag.html | 4 ++-- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index a7502dd..d43e697 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( var static embed.FS var templates map[string]*template.Template var configFile string +var basePath string var strs map[string]map[string]string func main() { @@ -29,13 +30,20 @@ func main() { slog.Error(fmt.Sprintf("error reading configuration: %s", err.Error())) os.Exit(1) } + basePath = "/" + if cfg.Basepath != "" { + basePath = cfg.Basepath + if !strings.HasSuffix(basePath, "/") { + basePath = basePath + "/" + } + } configFile = *cfgFile staticFileServer := http.FileServer(http.FS(static)) mux := http.NewServeMux() - mux.HandleFunc("/", home) - mux.HandleFunc("/light", home) - mux.HandleFunc("/dark", home) - mux.Handle("/static/", staticFileServer) + mux.HandleFunc(basePath, home) + mux.HandleFunc(basePath+"light", home) + mux.HandleFunc(basePath+"dark", home) + mux.Handle(basePath+"static/", http.StripPrefix(basePath, staticFileServer)) if photoIsLocal(cfg.Profile.Photo) { dir, relDir := getPhotoPaths(cfg) mux.Handle(dir, http.StripPrefix(dir, http.FileServer(http.Dir(relDir)))) @@ -46,7 +54,8 @@ func main() { } func home(w http.ResponseWriter, r *http.Request) { - acceptedPages := []string{"/", "/light", "/dark"} + fmt.Println("Client: " + r.Header.Get("X-Forwarded-For") + " Request: " + r.URL.Path) + acceptedPages := []string{basePath, basePath + "light", basePath + "dark"} if !slices.Contains(acceptedPages, r.URL.Path) { http.NotFound(w, r) } diff --git a/resume.go b/resume.go index 39c7568..23228f9 100644 --- a/resume.go +++ b/resume.go @@ -6,10 +6,11 @@ import ( ) type Resume struct { - Theme string `yaml:"theme"` - Meta *Meta `yaml:"meta"` - Profile *Profile `yaml:"profile"` - Year string + Basepath string `yaml:"basepath"` + Theme string `yaml:"theme"` + Meta *Meta `yaml:"meta"` + Profile *Profile `yaml:"profile"` + Year string } type Profile struct { diff --git a/templates/index.html b/templates/index.html index b224c06..8dd7967 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,7 +7,7 @@ {{ template "meta" . }} - +