summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-10-17 17:01:56 -0700
committerRose Hogenson <rosehogenson@posteo.net>2024-10-17 17:01:56 -0700
commit5a5ad101047b30958ecc8ffe1921f90bd4591c94 (patch)
tree4cf2a622e327a09c7b8df211f6d90475cd320578
parent30995a8fcce327f2af0a0332868428d9411350a8 (diff)
downloadroseh.moe-5a5ad101047b30958ecc8ffe1921f90bd4591c94.tar.zst
Add a 404 page.
-rw-r--r--roseh.moe.go44
-rw-r--r--static/404.pngbin0 -> 1034642 bytes
-rw-r--r--templates/404.html.template16
3 files changed, 47 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),
diff --git a/static/404.png b/static/404.png
new file mode 100644
index 0000000..72d9d02
--- /dev/null
+++ b/static/404.png
Binary files 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 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title>Not Found</title>
+ </head>
+ <body>
+ <center>
+ <div style="padding-top: 100px">
+ <font size="10"><code>{{.Path}}</code> was not found</font>
+ </div>
+ <div style="position:fixed;bottom:0px;width:100%">
+ <img style="height:50vh" src="/static/404.png">
+ </div>
+ </center>
+ </body>
+</html>