-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (69 loc) · 1.74 KB
/
main.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"embed"
"encoding/json"
"html/template"
"io/fs"
"log/slog"
"net/http"
"os"
)
type system struct {
Name string
Description string
URL string
Color string
Icon string
IconStyle template.CSS
Sensitive bool
}
//go:embed static/*
var assets embed.FS
//go:embed index.html
var indexFile string
func main() {
darkmodeURL, ok := os.LookupEnv("DARKMODE_URL")
if !ok {
darkmodeURL = "https://darkmode.datasektionen.se/"
}
indexTemplate, err := template.New("index.html").Parse(indexFile)
if err != nil {
panic(err)
}
http.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) {
res, err := http.Get(darkmodeURL)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
slog.Error("Failed fetching darkmode status", "error", err)
}
var darkmode bool
if err := json.NewDecoder(res.Body).Decode(&darkmode); err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
slog.Error("Failed parsing darkmode status", "error", err)
}
systemsToShow := systems
if darkmode {
systemsToShow = make([]system, 0, len(systems))
for _, system := range systems {
if !system.Sensitive {
systemsToShow = append(systemsToShow, system)
}
}
}
if err := indexTemplate.Execute(w, systemsToShow); err != nil {
http.Error(w, "Failed rendering template", http.StatusInternalServerError)
slog.Error("Failed rendering template", "error", err)
}
})
fs, err := fs.Sub(assets, "static")
if err != nil {
panic(err)
}
http.Handle("/", http.FileServer(http.FS(fs)))
port, ok := os.LookupEnv("PORT")
if !ok {
port = "3000"
}
slog.Info("Started", "port", port)
http.ListenAndServe(":"+port, nil)
}