-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
104 lines (85 loc) · 2.69 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
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
package main
import (
"embed"
"log/slog"
"mouji/commons/auth"
"mouji/commons/session"
"mouji/commons/sqlite"
"mouji/commons/templates"
"mouji/features/home"
"mouji/features/login"
"mouji/features/pageviews"
"mouji/features/projects"
"mouji/features/settings"
"mouji/features/users"
"net/http"
"os"
"strings"
"time"
)
//go:embed all:commons all:features
var resources embed.FS
//go:embed assets/*
var assets embed.FS
//go:embed migrations/*.sql
var migrations embed.FS
func main() {
defer func() {
if r := recover(); r != nil {
slog.Error("killing server", "error", r)
os.Exit(1)
}
}()
sqlite.NewDB()
defer sqlite.DB.Close()
sqlite.Migrate(migrations)
templates.NewTemplates(resources)
go runBackgroundTasks()
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
port = ":" + port
slog.Info("starting server", "port", port)
err := http.ListenAndServe(port, newRouter())
if err != nil {
panic(err)
}
}
func newRouter() *http.ServeMux {
mux := http.NewServeMux()
// public
mux.HandleFunc("GET /assets/", handleStaticAssets)
mux.HandleFunc("GET /collect", pageviews.HandleCollect)
mux.HandleFunc("GET /login", login.HandleLoginPage)
mux.HandleFunc("POST /login", login.HandleLoginSubmit)
// private
addPrivateRoute(mux, "GET /", home.HandleHomePage)
addPrivateRoute(mux, "GET /settings", settings.HandleSettingsPage)
addPrivateRoute(mux, "GET /settings/server_url", settings.HandleServerURLPage)
addPrivateRoute(mux, "POST /settings/server_url", settings.HandleServerURLSubmit)
addPrivateRoute(mux, "GET /users/new", users.HandleNewUserPage)
addPrivateRoute(mux, "POST /users/new", users.HandleNewUserSubmit)
addPrivateRoute(mux, "GET /users/me/password", users.HandleChangePasswordPage)
addPrivateRoute(mux, "POST /users/me/password", users.HandleChangePasswordSubmit)
addPrivateRoute(mux, "GET /projects/new", projects.HandleNewProjectPage)
addPrivateRoute(mux, "GET /projects/{project_id}", projects.HandleEditProjectPage)
addPrivateRoute(mux, "POST /projects/", projects.HandleProjectDetailSubmit)
addPrivateRoute(mux, "POST /projects/{project_id}", projects.HandleProjectDetailSubmit)
return mux
}
func handleStaticAssets(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, ".woff2") {
w.Header().Set("Cache-Control", "public, max-age=31536000") // 1 year
}
http.FileServer(http.FS(assets)).ServeHTTP(w, r)
}
func addPrivateRoute(mux *http.ServeMux, pattern string, handlerFunc func(w http.ResponseWriter, r *http.Request)) {
handler := http.HandlerFunc(handlerFunc)
mux.HandleFunc(pattern, auth.EnsureAuthenticated(handler))
}
func runBackgroundTasks() {
for range time.Tick(24 * time.Hour) {
session.DeleteExpiredSessions()
}
}