Support for running under a subpath, log connections

This commit is contained in:
Jarno Rankinen 2024-02-10 19:27:12 +02:00
parent e1e9c619e0
commit fe2ce513e2
4 changed files with 24 additions and 14 deletions

19
main.go
View File

@ -17,6 +17,7 @@ import (
var static embed.FS var static embed.FS
var templates map[string]*template.Template var templates map[string]*template.Template
var configFile string var configFile string
var basePath string
var strs map[string]map[string]string var strs map[string]map[string]string
func main() { func main() {
@ -29,13 +30,20 @@ func main() {
slog.Error(fmt.Sprintf("error reading configuration: %s", err.Error())) slog.Error(fmt.Sprintf("error reading configuration: %s", err.Error()))
os.Exit(1) os.Exit(1)
} }
basePath = "/"
if cfg.Basepath != "" {
basePath = cfg.Basepath
if !strings.HasSuffix(basePath, "/") {
basePath = basePath + "/"
}
}
configFile = *cfgFile configFile = *cfgFile
staticFileServer := http.FileServer(http.FS(static)) staticFileServer := http.FileServer(http.FS(static))
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", home) mux.HandleFunc(basePath, home)
mux.HandleFunc("/light", home) mux.HandleFunc(basePath+"light", home)
mux.HandleFunc("/dark", home) mux.HandleFunc(basePath+"dark", home)
mux.Handle("/static/", staticFileServer) mux.Handle(basePath+"static/", http.StripPrefix(basePath, staticFileServer))
if photoIsLocal(cfg.Profile.Photo) { if photoIsLocal(cfg.Profile.Photo) {
dir, relDir := getPhotoPaths(cfg) dir, relDir := getPhotoPaths(cfg)
mux.Handle(dir, http.StripPrefix(dir, http.FileServer(http.Dir(relDir)))) 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) { 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) { if !slices.Contains(acceptedPages, r.URL.Path) {
http.NotFound(w, r) http.NotFound(w, r)
} }

View File

@ -6,10 +6,11 @@ import (
) )
type Resume struct { type Resume struct {
Theme string `yaml:"theme"` Basepath string `yaml:"basepath"`
Meta *Meta `yaml:"meta"` Theme string `yaml:"theme"`
Profile *Profile `yaml:"profile"` Meta *Meta `yaml:"meta"`
Year string Profile *Profile `yaml:"profile"`
Year string
} }
type Profile struct { type Profile struct {

View File

@ -7,7 +7,7 @@
<head> <head>
{{ template "meta" . }} {{ template "meta" . }}
<link rel="stylesheet" href="/static/css/{{ .Theme }}-style.css" /> <link rel="stylesheet" href="{{ .Basepath }}/static/css/{{ .Theme }}-style.css" />
<style> <style>
{{ if .Profile.Skills }} {{ if .Profile.Skills }}
.skilltag { .skilltag {
@ -251,9 +251,9 @@
<div class="content has-text-centered"> <div class="content has-text-centered">
<div class="noprint"> <div class="noprint">
{{ if eq .Theme "light" }} {{ if eq .Theme "light" }}
<a href="/dark">{{ translate "see_dark" $lang }}</a> <a href="{{ .Basepath }}/dark">{{ translate "see_dark" $lang }}</a>
{{ else }} {{ else }}
<a href="/light">{{ translate "see_light" $lang }}</a> <a href="{{ .Basepath }}/light">{{ translate "see_light" $lang }}</a>
{{ end }} {{ end }}
<br /> <br />
Copyright © {{ .Year }} {{ .Profile.Name }}. {{ translate "all_rights_reserved" $lang }} Copyright © {{ .Year }} {{ .Profile.Name }}. {{ translate "all_rights_reserved" $lang }}

View File

@ -26,7 +26,7 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway" /> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway" />
</noscript> </noscript>
<link rel="stylesheet" href="/static/css/dark-style.css" /> <link rel="stylesheet" href="{{ .Basepath }}/static/css/dark-style.css" />
<link rel="stylesheet" href="/static/css/github-corner.css" /> <link rel="stylesheet" href="{{ .Basepath }}/static/css/github-corner.css" />
{{ end }} {{ end }}