-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
192 lines (176 loc) · 8.08 KB
/
main.go
File metadata and controls
192 lines (176 loc) · 8.08 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// YK Pao School Co-curricular Activities Selection System Backend
package main
import (
"context"
"crypto/tls"
"flag"
"log"
"log/slog"
"net"
"net/http"
"os"
"strings"
"time"
"git.sr.ht/~runxiyu/cca/db"
"github.com/MicahParks/keyfunc/v3"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
})
slog.SetDefault(slog.New(handler))
var configPath string
flag.StringVar(&configPath, "c", "cca.scfgs", "path to configuration file")
flag.Parse()
ctx := context.Background()
var err error
app := App{}
// Config
slog.Info(logMsgStartupConfigLoad, slog.String("path", configPath))
app.config, err = loadConfig(configPath)
if err != nil {
log.Fatalln(err)
}
// Database
slog.Info(logMsgStartupDBConnect)
dbCfg := app.config.Database
poolConfig, err := pgxpool.ParseConfig(dbCfg.URL)
if err != nil {
log.Fatalf("parse database config: %v", err)
}
if dbCfg.MaxConns > 0 {
poolConfig.MaxConns = dbCfg.MaxConns
}
if dbCfg.MinConns > 0 {
poolConfig.MinConns = dbCfg.MinConns
}
if dbCfg.MaxConnLifetime > 0 {
poolConfig.MaxConnLifetime = dbCfg.MaxConnLifetime
}
if dbCfg.MaxConnIdleTime > 0 {
poolConfig.MaxConnIdleTime = dbCfg.MaxConnIdleTime
}
if dbCfg.HealthCheckPeriod > 0 {
poolConfig.HealthCheckPeriod = dbCfg.HealthCheckPeriod
}
if dbCfg.ConnectTimeout > 0 {
poolConfig.ConnConfig.ConnectTimeout = dbCfg.ConnectTimeout
}
app.pool, err = pgxpool.NewWithConfig(ctx, poolConfig)
if err != nil {
log.Fatalf("open database: %v", err)
}
if err := app.pool.Ping(ctx); err != nil {
log.Fatalf("ping database: %v", err)
}
app.queries = db.New(app.pool)
version, err := app.queries.GetSchemaVersion(ctx)
if err != nil {
log.Fatalln(err)
}
if version != 1 {
log.Fatalln("Bad schema version")
}
// JWKS
slog.Info(logMsgStartupJWKSFetch, slog.String("jwks", app.config.OIDC.JWKS))
app.kf, err = keyfunc.NewDefault([]string{app.config.OIDC.JWKS})
if err != nil {
log.Fatalln(err)
}
// Templates
slog.Info(logMsgStartupTemplatesLoad)
err = app.admLoadTemplates()
if err != nil {
log.Fatalln(err)
}
// WebSocket hub
slog.Info(logMsgStartupWebsocketSetup)
app.wsHub = NewWebSocketHub()
go app.wsHub.Run()
// Router
slog.Info(logMsgStartupRoutesRegister)
mux := http.NewServeMux()
mux.HandleFunc("/{$}", app.handleIndex)
mux.HandleFunc("/auth", app.handleAuth)
mux.Handle("/admin/static/", http.StripPrefix("/admin/static/", http.FileServer(http.Dir("admin_static"))))
mux.HandleFunc("/admin/{$}", app.adminOnly("handleAdm", app.handleAdm))
mux.HandleFunc("/admin/notify", app.adminOnly("handleAdmNotify", app.handleAdmNotify))
mux.HandleFunc("/admin/periods", app.adminOnly("handleAdmPeriods", app.handleAdmPeriods))
mux.HandleFunc("/admin/periods/new", app.adminOnly("handleAdmPeriodsNew", app.handleAdmPeriodsNew))
mux.HandleFunc("/admin/periods/delete", app.adminOnly("handleAdmPeriodsDelete", app.handleAdmPeriodsDelete))
mux.HandleFunc("/admin/categories", app.adminOnly("handleAdmCategories", app.handleAdmCategories))
mux.HandleFunc("/admin/categories/new", app.adminOnly("handleAdmCategoriesNew", app.handleAdmCategoriesNew))
mux.HandleFunc("/admin/categories/delete", app.adminOnly("handleAdmCategoriesDelete", app.handleAdmCategoriesDelete))
mux.HandleFunc("/admin/grades", app.adminOnly("handleAdmGrades", app.handleAdmGrades))
mux.HandleFunc("/admin/grades/new", app.adminOnly("handleAdmGradesNew", app.handleAdmGradesNew))
mux.HandleFunc("/admin/grades/edit", app.adminOnly("handleAdmGradesEdit", app.handleAdmGradesEdit))
mux.HandleFunc("/admin/grades/bulk-enabled-update", app.adminOnly("handleAdmGradesBulkEnabledUpdate", app.handleAdmGradesBulkEnabledUpdate))
mux.HandleFunc("/admin/grades/delete", app.adminOnly("handleAdmGradesDelete", app.handleAdmGradesDelete))
mux.HandleFunc("/admin/grades/new-requirement-group", app.adminOnly("handleAdmGradesNewRequirementGroup", app.handleAdmGradesNewRequirementGroup))
mux.HandleFunc("/admin/grades/delete-requirement-group", app.adminOnly("handleAdmGradesDeleteRequirementGroup", app.handleAdmGradesDeleteRequirementGroup))
mux.HandleFunc("/admin/courses", app.adminOnly("handleAdmCourses", app.handleAdmCourses))
mux.HandleFunc("/admin/courses/new", app.adminOnly("handleAdmCoursesNew", app.handleAdmCoursesNew))
mux.HandleFunc("/admin/courses/edit", app.adminOnly("handleAdmCoursesEdit", app.handleAdmCoursesEdit))
mux.HandleFunc("/admin/courses/delete", app.adminOnly("handleAdmCoursesDelete", app.handleAdmCoursesDelete))
mux.HandleFunc("/admin/courses/import", app.adminOnly("handleAdmCoursesImport", app.handleAdmCoursesImport))
mux.HandleFunc("/admin/students", app.adminOnly("handleAdmStudents", app.handleAdmStudents))
mux.HandleFunc("/admin/students/new", app.adminOnly("handleAdmStudentsNew", app.handleAdmStudentsNew))
mux.HandleFunc("/admin/students/edit", app.adminOnly("handleAdmStudentsEdit", app.handleAdmStudentsEdit))
mux.HandleFunc("/admin/students/delete", app.adminOnly("handleAdmStudentsDelete", app.handleAdmStudentsDelete))
mux.HandleFunc("/admin/students/import", app.adminOnly("handleAdmStudentsImport", app.handleAdmStudentsImport))
mux.HandleFunc("/admin/selections", app.adminOnly("handleAdmSelections", app.handleAdmSelections))
mux.HandleFunc("/admin/selections/export", app.adminOnly("handleAdmSelectionsExport", app.handleAdmSelectionsExport))
mux.HandleFunc("/admin/selections/new", app.adminOnly("handleAdmSelectionsNew", app.handleAdmSelectionsNew))
mux.HandleFunc("/admin/selections/edit", app.adminOnly("handleAdmSelectionsEdit", app.handleAdmSelectionsEdit))
mux.HandleFunc("/admin/selections/delete", app.adminOnly("handleAdmSelectionsDelete", app.handleAdmSelectionsDelete))
mux.HandleFunc("/admin/selections/import", app.adminOnly("handleAdmSelectionsImport", app.handleAdmSelectionsImport))
mux.HandleFunc("/student", app.studentOnly("handleStu", app.handleStu))
mux.Handle("/student/assets/", http.StripPrefix("/student/assets/", http.FileServer(http.Dir("frontend/dist/assets/"))))
mux.HandleFunc("/student/", app.studentOnlyPlain("studentFrontend", func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/student/assets/") {
http.ServeFile(w, r, "./frontend/dist/index.html")
return
}
}))
mux.HandleFunc("/student/api/events", app.studentOnly("handleStuAPIEvents", app.handleStuAPIEvents))
mux.HandleFunc("/student/api/user_info", app.studentOnly("handleStuAPIInfo", app.handleStuAPIInfo))
mux.HandleFunc("/student/api/courses", app.studentOnly("handleStuAPICourses", app.handleStuAPICourses))
mux.HandleFunc("/student/api/periods", app.studentOnly("handleStuAPIPeriods", app.handleStuAPIPeriods))
mux.HandleFunc("/student/api/categories", app.studentOnly("handleStuAPICategories", app.handleStuAPICategories))
mux.HandleFunc("/student/api/grades", app.studentOnly("handleStuAPIGrades", app.handleStuAPIGrades))
mux.HandleFunc("/student/api/my_selections", app.studentOnly("handleStuAPIMySelections", app.handleStuAPIMySelections))
// Listen and serve
slog.Info(logMsgStartupListenerStart, slog.String("transport", app.config.Listen.Transport), slog.String("address", app.config.Listen.Address), slog.String("network", app.config.Listen.Network))
var l net.Listener
switch app.config.Listen.Transport {
case "plain":
l, err = net.Listen(app.config.Listen.Network, app.config.Listen.Address)
if err != nil {
log.Fatalf("Cannot listen plain: %v\n", err)
}
case "tls":
c, err := tls.LoadX509KeyPair(app.config.Listen.TLS.Cert, app.config.Listen.TLS.Key)
if err != nil {
log.Fatalf("Cannot load TLS keys: %v\n", err)
}
tc := tls.Config{
Certificates: []tls.Certificate{c},
MinVersion: tls.VersionTLS13,
}
l, err = tls.Listen(app.config.Listen.Network, app.config.Listen.Address, &tc)
if err != nil {
log.Fatalf("Cannot listen TLS: %v\n", err)
}
}
slog.Info(logMsgStartupServing)
server := &http.Server{
Handler: mux,
ReadHeaderTimeout: time.Second * time.Duration(10),
ErrorLog: newHTTPServerErrorLogger(),
}
if err := server.Serve(l); err != nil {
slog.Error(logMsgHTTPServerServeFailure, slog.Any("error", err))
os.Exit(1)
}
}