diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2024-10-17 17:01:56 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2024-10-17 17:01:56 -0700 |
| commit | 5a5ad101047b30958ecc8ffe1921f90bd4591c94 (patch) | |
| tree | 4cf2a622e327a09c7b8df211f6d90475cd320578 /roseh.moe.go | |
| parent | Add some timeouts. (diff) | |
| download | roseh.moe-5a5ad101047b30958ecc8ffe1921f90bd4591c94.tar.zst | |
Add a 404 page.
Diffstat (limited to 'roseh.moe.go')
| -rw-r--r-- | roseh.moe.go | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/roseh.moe.go b/roseh.moe.go index c95bb5e..ea7f4a3 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -1,8 +1,10 @@ package main import ( + "embed" "flag" "fmt" + "html/template" "log" "net/http" "net/http/httputil" @@ -15,11 +17,7 @@ var ( https = flag.Bool("https", false, "use https") ) -type server struct { - jellyfinProxy *httputil.ReverseProxy -} - -func (*server) index(w http.ResponseWriter, _ *http.Request) { +func index(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, `<!DOCTYPE html> <html lang="en"> <head> @@ -32,10 +30,25 @@ func (*server) index(w http.ResponseWriter, _ *http.Request) { `) } -func (s *server) cinema(w http.ResponseWriter, r *http.Request) { - s.jellyfinProxy.ServeHTTP(w, r) +//go:embed templates/404.html.template +var notFoundString string + +var notFoundTemplate = template.Must(template.New("notFound").Parse(notFoundString)) + +func notFound(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + type args struct { + Path string + } + if err := notFoundTemplate.Execute(w, args{Path: r.URL.Path}); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } +//go:embed static +var static embed.FS + func main() { flag.Parse() @@ -44,13 +57,18 @@ func main() { log.Fatalf("Jellyfin URL: %s", err) } - s := &server{ - jellyfinProxy: httputil.NewSingleHostReverseProxy(jellyfinURL), - } mux := http.NewServeMux() - mux.HandleFunc("/", s.index) - mux.HandleFunc("/cinema", s.cinema) - mux.HandleFunc("/cinema/", s.cinema) + jellyfinProxy := httputil.NewSingleHostReverseProxy(jellyfinURL) + mux.Handle("/cinema", jellyfinProxy) + mux.Handle("/cinema/", jellyfinProxy) + mux.Handle("/static/", http.FileServerFS(static)) + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" { + index(w, r) + } else { + notFound(w, r) + } + }) httpServer := http.Server{ Addr: fmt.Sprintf(":%d", *port), |
