From fc9d3f4dfea54fb8cad3dda316295a25a2e08056 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Wed, 2 Oct 2024 17:26:41 -0400 Subject: Add first pass at dashboard --- dashboard/dashboard.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 dashboard/dashboard.go (limited to 'dashboard/dashboard.go') diff --git a/dashboard/dashboard.go b/dashboard/dashboard.go new file mode 100644 index 0000000..c3839f2 --- /dev/null +++ b/dashboard/dashboard.go @@ -0,0 +1,48 @@ +package dashboard + +import ( + "html/template" + "log/slog" + "net/http" + "nonsense-time/api" + "path/filepath" +) + +var Logger *slog.Logger +var templates map[string]*template.Template + +func Index(w http.ResponseWriter, req *http.Request) { + t, ok := templates["dashboard"] + + if !ok { + panic(t) + } + + // TODO: poll database + data := struct { + ServerStatus api.ServerStatus + }{ + ServerStatus: api.ServerStatus{}, + } + + err := t.Execute(w, data) + if err != nil { + Logger.Error("Unable to execute template", slog.Any("err", err)) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } +} + +func StaticHandler(w http.ResponseWriter, req *http.Request) { + http.StripPrefix("/static", http.FileServer(http.Dir("static"))).ServeHTTP(w, req) +} + +func init() { + Logger = slog.Default() + templates = make(map[string]*template.Template) + templates["dashboard"] = template.Must(template.New("base.html").ParseFiles( + filepath.Join("templates", "base.html"), + filepath.Join("templates", "dashboard", "dashboard.html"), + filepath.Join("templates", "dashboard", "server_status.html"), + )) +} -- cgit v1.2.3