From 5a5ad101047b30958ecc8ffe1921f90bd4591c94 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 17 Oct 2024 17:01:56 -0700 Subject: Add a 404 page. --- roseh.moe.go | 44 +++++++++++++++++++++++++++++++------------- static/404.png | Bin 0 -> 1034642 bytes templates/404.html.template | 16 ++++++++++++++++ 3 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 static/404.png create mode 100644 templates/404.html.template 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, ` @@ -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), diff --git a/static/404.png b/static/404.png new file mode 100644 index 0000000..72d9d02 Binary files /dev/null and b/static/404.png differ diff --git a/templates/404.html.template b/templates/404.html.template new file mode 100644 index 0000000..b468b56 --- /dev/null +++ b/templates/404.html.template @@ -0,0 +1,16 @@ + + + + Not Found + + +
+
+ {{.Path}} was not found +
+
+ +
+
+ + -- cgit v1.3.1