-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (62 loc) · 2.05 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
package main
import (
"embed"
"html/template"
"log"
"net/http"
"github.com/makew0rld/1rg-server/cal"
"github.com/makew0rld/1rg-server/config"
)
//go:embed assets
//go:embed templates
var content embed.FS
var templates = template.Must(template.ParseFS(content, "templates/*"))
func renderTemplate(w http.ResponseWriter, tmpl string, data any) {
err := templates.ExecuteTemplate(w, tmpl+".html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
log.Print("starting")
err := config.LoadConfig()
if err != nil {
log.Fatal(err)
}
err = cal.LoadEvents()
if err != nil {
log.Fatal(err)
}
http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
data := struct {
MeetingRooms string
PublicEvents []cal.SimpleEvent
PrivateEvents string
}{}
sf, gr, pr := cal.SecondFloorBusy(), cal.GreenRoomBusy(), cal.PurpleRoomBusy()
// Eight combos: 000 to 111
if !sf && !gr && !pr {
data.MeetingRooms = "No meeting rooms are currently booked."
} else if !sf && !gr && pr {
data.MeetingRooms = "The purple meeting room is booked, but the others are free."
} else if !sf && gr && !pr {
data.MeetingRooms = "The green meeting room is booked, but the others are free."
} else if !sf && gr && pr {
data.MeetingRooms = "Only the second floor meeting room is free."
} else if sf && !gr && !pr {
data.MeetingRooms = "The second floor meeting room is booked, but the others are free."
} else if sf && !gr && pr {
data.MeetingRooms = "Only the green meeting room is free."
} else if sf && gr && !pr {
data.MeetingRooms = "Only the purple meeting room is free."
} else if sf && gr && pr {
data.MeetingRooms = "All meeting rooms are booked."
}
data.PrivateEvents = "I'm not sure if there are any private events, try checking Discord."
data.PublicEvents = cal.PublicEventsToday()
renderTemplate(w, "index", data)
})
http.Handle("GET /assets/", http.FileServerFS(content))
log.Print("listening on localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}