diff options
| -rw-r--r-- | roseh.moe.go | 44 | ||||
| -rw-r--r-- | static/404.png | bin | 0 -> 1034642 bytes | |||
| -rw-r--r-- | templates/404.html.template | 16 |
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 Binary files differnew file mode 100644 index 0000000..72d9d02 --- /dev/null +++ b/static/404.png 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> |
