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 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)
}

View File

@ -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 {

View File

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