Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
410 lines (375 loc) · 11.1 KB
/
Copy pathmain.go
File metadata and controls
410 lines (375 loc) · 11.1 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package main
import (
"database/sql"
"embed"
"errors"
"fmt"
"io/fs"
"log/slog"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path"
"strings"
"time"
)
//go:embed ui/dist/*
var uiDist embed.FS
//go:embed db/migrations/*.sql
var migrationFiles embed.FS
const defaultJWTSecret = "sheetbase-dev-secret-change-me-32-bytes-minimum"
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(args []string) error {
if len(args) == 0 {
return serve(args)
}
switch args[0] {
case "init":
return initApp(args[1:])
case "serve":
return serve(args[1:])
case "run":
return runManagedApp(args[1:])
case "up":
return upApp(args[1:])
case "down":
return downApp(args[1:])
case "start":
return startApp(args[1:])
case "stop":
return stopApp(args[1:])
case "restart":
return restartApp(args[1:])
case "migrate":
return migrateApp(args[1:])
case "upgrade":
return migrateApp(args[1:])
case "runtime":
if len(args) < 2 || (args[1] != "install" && args[1] != "update") {
return errors.New("usage: sheetbase runtime install [--home DIR]")
}
return installRuntimeApp(args[2:])
case "doctor":
return doctorApp(args[1:])
case "status":
return statusApp(args[1:])
case "systemd":
return systemdApp(args[1:])
case "backup":
return backupApp(args[1:])
case "export":
return exportApp(args[1:])
case "restore":
return restoreApp(args[1:])
case "help", "-h", "--help":
printUsage()
return nil
default:
return fmt.Errorf("unknown command %q\n\nRun `sheetbase help` for usage.", args[0])
}
}
func serve(args []string) error {
cfg, err := parseServeConfig(args)
if err != nil {
return err
}
if cfg.dbURL != "" && cfg.jwtSecret == defaultJWTSecret {
return errors.New("refusing to serve with the development JWT secret; run `sheetbase init` or `sheetbase start` first")
}
if err := setupAppLogging(newAppPaths(cfg.home)); err != nil {
return err
}
var auth *authService
if cfg.dbURL != "" {
auth, err = newAuthService(cfg.dbURL, cfg.jwtSecret)
if err != nil {
return err
}
}
handler, err := newUIHandler(cfg.postgrestURL, auth)
if err != nil {
return err
}
handler = withExportDownload(handler, newAppPaths(cfg.home), auth)
slog.Info("serving Sheetbase", "addr", cfg.appAddr)
err = http.ListenAndServe(cfg.appAddr, handler)
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
}
func withExportDownload(next http.Handler, paths appPaths, auth *authService) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Path == "/admin/export" {
if auth != nil {
if _, ok := auth.userID(r); !ok {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
}
target, err := os.CreateTemp("", "sheetbase-export-*.tar.gz")
if err != nil {
http.Error(w, "create export", http.StatusInternalServerError)
return
}
_ = target.Close()
defer os.Remove(target.Name())
cfg, configErr := parseAppConfig("export", []string{"--home", paths.home})
if configErr != nil {
http.Error(w, configErr.Error(), http.StatusInternalServerError)
return
}
if err := exportToFile(paths, cfg, target.Name()); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Disposition", `attachment; filename="sheetbase-export.tar.gz"`)
http.ServeFile(w, r, target.Name())
return
}
next.ServeHTTP(w, r)
})
}
func parseServeConfig(args []string) (appConfig, error) {
cfg, err := parseAppConfig("serve", args)
if err != nil {
return appConfig{}, err
}
if cfg.postgrestURL == "" {
cfg.postgrestURL = "http://127.0.0.1:" + cfg.postgrestPort
}
if cfg.dbURL == "" && !hasFlag(args, "db-url") {
cfg.dbURL = "postgres://postgres:postgres@127.0.0.1:" + cfg.postgresPort + "/postgres?sslmode=disable"
}
return cfg, nil
}
func setupAppLogging(paths appPaths) error {
if err := os.MkdirAll(paths.logs, 0o755); err != nil {
return err
}
file, err := os.OpenFile(paths.appLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
}
slog.SetDefault(slog.New(slog.NewTextHandler(file, nil)))
return nil
}
func beginCommandLog(command string, paths appPaths) func(*error) {
slog.Info("command started", "command", command, "home", paths.home)
return func(err *error) {
if err != nil && *err != nil {
slog.Error("command failed", "command", command, "home", paths.home, "error", (*err).Error())
return
}
slog.Info("command completed", "command", command, "home", paths.home)
}
}
func hasFlag(args []string, name string) bool {
for _, arg := range args {
if arg == "-"+name || arg == "--"+name || strings.HasPrefix(arg, "-"+name+"=") || strings.HasPrefix(arg, "--"+name+"=") {
return true
}
}
return false
}
func printUsage() {
fmt.Println(`Sheetbase
Usage:
sheetbase init [--home DIR]
sheetbase serve [--home DIR] [-addr :8080] [-postgrest-url http://127.0.0.1:3000] [-db-url postgres://...]
sheetbase run [--home DIR] [-addr :8080]
sheetbase up [--home DIR] [-addr :8080]
sheetbase down [--home DIR]
sheetbase start [--home DIR]
sheetbase stop [--home DIR]
sheetbase restart [--home DIR]
sheetbase migrate [--home DIR]
sheetbase upgrade [--home DIR]
sheetbase runtime install [--home DIR]
sheetbase doctor [--home DIR]
sheetbase status [--home DIR]
sheetbase backup [--home DIR] [--out FILE]
sheetbase export [--home DIR] [--out FILE]
sheetbase restore [--home DIR] --in FILE
sheetbase systemd [--home DIR] [--bin /usr/local/bin/sheetbase]
Commands:
init Create the Sheetbase home directory and config
serve Serve the embedded UI
run Start managed services and serve the UI
up Start PostgreSQL, PostgREST, and Sheetbase in the background
down Stop all three background services
start Start managed PostgreSQL and PostgREST processes
stop Stop managed PostgreSQL and PostgREST processes
migrate Apply embedded database migrations
upgrade Apply embedded database migrations
runtime Download verified PostgreSQL and PostgREST binaries
doctor Check managed runtime requirements
status Show managed process status
backup Write a PostgreSQL custom-format dump
export Write app metadata and a PostgreSQL dump to a tar.gz archive
restore Restore a PostgreSQL custom-format dump
systemd Print a systemd service unit
help Show this help`)
}
func newUIHandler(postgrestURL string, auth *authService) (http.Handler, error) {
dist, err := fs.Sub(uiDist, "ui/dist")
if err != nil {
return nil, fmt.Errorf("load embedded UI: %w", err)
}
fileServer := http.FileServer(http.FS(dist))
apiProxy, err := newAPIProxy(postgrestURL)
if err != nil {
return nil, err
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logged := strings.HasPrefix(r.URL.Path, "/api") || strings.HasPrefix(r.URL.Path, "/internal") || strings.HasPrefix(r.URL.Path, "/auth/") || strings.HasPrefix(r.URL.Path, "/admin/")
recorder := &responseLogWriter{ResponseWriter: w, status: http.StatusOK}
if logged {
defer func(start time.Time) {
slog.Info("http request",
"method", r.Method,
"path", r.URL.Path,
"status", recorder.status,
"duration_ms", time.Since(start).Milliseconds(),
)
}(time.Now())
w = recorder
}
if r.URL.Path == "/healthz" {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok\n"))
return
}
if auth != nil && strings.HasPrefix(r.URL.Path, "/auth/") {
handleAuth(auth, w, r)
return
}
if auth != nil && strings.HasPrefix(r.URL.Path, "/admin/api-keys") {
handleAPIKeys(auth, w, r)
return
}
if auth != nil && r.Method == http.MethodGet && r.URL.Path == "/internal/events" {
handleChangeEvents(auth, w, r)
return
}
if r.URL.Path == "/internal" || strings.HasPrefix(r.URL.Path, "/internal/") {
if auth != nil {
userID, ok := auth.userID(r)
if !ok {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
r.Header.Set("Authorization", "Bearer "+auth.jwt(userID))
}
apiProxy.ServeHTTP(w, r)
return
}
if r.URL.Path == "/api" || strings.HasPrefix(r.URL.Path, "/api/") {
if auth == nil {
http.Error(w, "API key authentication is unavailable", http.StatusServiceUnavailable)
return
}
active, err := auth.apiKeys.hasActive(r.Context())
if err != nil {
http.Error(w, "API key authentication is unavailable", http.StatusServiceUnavailable)
return
}
jwt := auth.publicAPIJWT()
if active {
apiKeyID, ok := auth.authenticateAPIKey(r)
if !ok {
http.Error(w, "invalid or missing API key", http.StatusUnauthorized)
return
}
jwt = auth.apiKeyJWT(apiKeyID)
}
r.Header.Del("X-API-Key")
r.Header.Del("Cookie")
r.Header.Set("Authorization", "Bearer "+jwt)
if auth.sheetForms != nil {
slug := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/"), "/")
if slug != "" && !strings.Contains(slug, "/") && !isPublicMetadataResource(slug) {
table, resolveErr := auth.sheetForms.tableNameBySlug(r.Context(), slug)
if resolveErr != nil {
if errors.Is(resolveErr, sql.ErrNoRows) {
http.NotFound(w, r)
return
}
http.Error(w, "API route is unavailable", http.StatusServiceUnavailable)
return
}
r.URL.Path = "/api/" + table
}
}
apiProxy.ServeHTTP(w, r)
return
}
if _, err := fs.Stat(dist, cleanAssetPath(r.URL.Path)); err == nil {
fileServer.ServeHTTP(w, r)
return
}
r.URL.Path = "/"
fileServer.ServeHTTP(w, r)
}), nil
}
func isPublicMetadataResource(resource string) bool {
switch resource {
case "sheet_forms", "sheet_fields", "sheet_views":
return true
default:
return false
}
}
type responseLogWriter struct {
http.ResponseWriter
status int
}
func (w *responseLogWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }
func (w *responseLogWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
func handleAuth(auth *authService, w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodPost && r.URL.Path == "/auth/setup":
auth.handleSetup(w, r)
case r.Method == http.MethodPost && r.URL.Path == "/auth/login":
auth.handleLogin(w, r)
case r.Method == http.MethodPost && r.URL.Path == "/auth/logout":
auth.handleLogout(w, r)
case r.Method == http.MethodGet && r.URL.Path == "/auth/me":
auth.handleMe(w, r)
default:
http.NotFound(w, r)
}
}
func newAPIProxy(rawURL string) (http.Handler, error) {
target, err := url.Parse(rawURL)
if err != nil {
return nil, fmt.Errorf("parse PostgREST URL: %w", err)
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Director = func(r *http.Request) {
r.URL.Scheme = target.Scheme
r.URL.Host = target.Host
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/api")
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/internal")
if r.URL.Path == "" {
r.URL.Path = "/"
}
r.Host = target.Host
}
return proxy, nil
}
func cleanAssetPath(urlPath string) string {
if urlPath == "" || urlPath == "/" {
return "index.html"
}
return strings.TrimPrefix(path.Clean("/"+urlPath), "/")
}