-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
162 lines (138 loc) · 3.92 KB
/
Copy pathmain.go
File metadata and controls
162 lines (138 loc) · 3.92 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"embed"
"fmt"
"log"
"net/http"
"os"
"time"
"1rg-server/cal"
"1rg-server/config"
"1rg-server/database"
"1rg-server/rolodex"
"1rg-server/templates"
"github.com/gorilla/csrf"
)
//go:embed assets
var assets embed.FS
var calendarsProvided bool
const (
calUpdateInterval = 30 * time.Minute
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: ./1rg-server path/to/config.toml")
return
}
log.Print("starting")
log.Print("loading config")
err := config.LoadConfig(os.Args[1])
if err != nil {
log.Fatal(err)
}
if config.IsProduction {
log.Print("mode: production")
} else {
log.Print("mode: debug")
}
log.Print("initializing database")
db, err := database.Init()
if err != nil {
log.Fatal(err)
}
calendarsProvided = config.CalendarsProvided()
if calendarsProvided {
log.Print("loading events from calendars")
err = cal.LoadEvents()
if err != nil {
log.Fatal(err)
}
go func() {
for {
time.Sleep(calUpdateInterval)
log.Print("loading events from calendars")
err = cal.LoadEvents()
if err != nil {
log.Printf("cal.LoadEvents: %v", err)
}
}
}()
} else {
log.Print("not all calendars were configured, skipping")
}
log.Print("setting up HTTP handlers")
// Homepage handler
http.HandleFunc("GET /", mainPageHandler)
// Asset handler
// Use embedded assets in prod and disk assets when debugging
if config.IsProduction {
http.Handle("GET /assets/", http.FileServerFS(assets))
} else {
http.Handle("GET /assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
}
// Module handlers
rolodexHandler, err := rolodex.NewHandler(db)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("GET /rolodex", rolodexHandler.IndexHandler)
http.HandleFunc("GET /rolodex/add", rolodexHandler.AddGetHandler)
http.HandleFunc("POST /rolodex/add", rolodexHandler.AddPostHandler)
http.HandleFunc("GET /rolodex/edit/{id}", rolodexHandler.EditGetHandler)
http.HandleFunc("POST /rolodex/edit/{id}", rolodexHandler.EditPostHandler)
var protector func(http.Handler) http.Handler
if config.IsProduction {
protector = csrf.Protect(
[]byte(config.Config.CSRFKey),
csrf.SameSite(csrf.SameSiteStrictMode),
csrf.Path("/"),
// TODO: will production be over HTTPS?
)
} else {
protector = csrf.Protect(
[]byte(config.Config.CSRFKey),
csrf.Secure(false),
csrf.TrustedOrigins([]string{"localhost:8080"}),
csrf.SameSite(csrf.SameSiteStrictMode),
csrf.Path("/"),
)
}
log.Print("listening on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", protector(http.DefaultServeMux)))
}
func mainPageHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
// This handler was used as the default for an unhandled path
http.NotFound(w, r)
return
}
data := struct {
MeetingRooms string
Events []cal.SimpleEvent
}{}
if calendarsProvided {
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.Events = cal.EventsToday()
} else {
data.MeetingRooms = "Meeting room status: unknown"
}
templates.RenderTemplate(w, "index", data)
}