aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/dashboard/dashboard.go
blob: c3839f29edd90d0c67cfb04d37e60cd835042773 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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"),
	))
}