Repository navigation
Expand file tree
/
Copy pathapp.go
More file actions
1049 lines (908 loc) · 32.9 KB
/
Copy pathapp.go
File metadata and controls
1049 lines (908 loc) · 32.9 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package vukem
import (
"cmp"
"context"
"crypto/tls"
"errors"
"fmt"
"html/template"
"io"
"io/fs"
"log/slog"
"net"
"net/netip"
"net/url"
"os"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/AlexGalhardo/vukem/analytics"
"github.com/AlexGalhardo/vukem/bingwmt"
"github.com/AlexGalhardo/vukem/cache"
"github.com/AlexGalhardo/vukem/config"
"github.com/AlexGalhardo/vukem/crypt"
"github.com/AlexGalhardo/vukem/database"
"github.com/AlexGalhardo/vukem/devtools"
"github.com/AlexGalhardo/vukem/events"
"github.com/AlexGalhardo/vukem/flags"
"github.com/AlexGalhardo/vukem/i18n"
"github.com/AlexGalhardo/vukem/indexnow"
"github.com/AlexGalhardo/vukem/inertia"
"github.com/AlexGalhardo/vukem/logging"
"github.com/AlexGalhardo/vukem/logging/telegram"
"github.com/AlexGalhardo/vukem/mail"
"github.com/AlexGalhardo/vukem/openapi"
"github.com/AlexGalhardo/vukem/orm"
"github.com/AlexGalhardo/vukem/queue"
"github.com/AlexGalhardo/vukem/ratelimit"
"github.com/AlexGalhardo/vukem/router"
"github.com/AlexGalhardo/vukem/schedule"
"github.com/AlexGalhardo/vukem/schema"
"github.com/AlexGalhardo/vukem/session"
"github.com/AlexGalhardo/vukem/view"
"github.com/AlexGalhardo/vukem/vite"
)
// Kind is the runtime in which the application is running. Providers can be
// limited to some kinds (see [KindFilter]).
type Kind string
// Runtime kinds.
const (
KindWeb Kind = "web"
KindConsole Kind = "console"
KindTest Kind = "test"
// KindWarmup builds the app (config, Register, routes) but never runs
// providers' Boot or Shutdown, so no connection is opened. Commands that
// only read the route table (codegen:routes, codegen:client,
// codegen:types, openapi:export) run in it, and it tolerates a missing
// APP_KEY. Boot and Run return ErrWarmupRun; never expose its Router.
KindWarmup Kind = "warmup"
)
// Config is the core application configuration, loaded from the environment.
type Config struct {
// Name is the application name, shown in logs, mail and pages.
Name string `env:"APP_NAME" default:"Vukem"`
// Env is development, test or production; any other value is a boot error, and unset or empty means production.
Env config.Env `env:"APP_ENV" default:"production"`
// Debug shows error details and the debug page; it must be false when APP_ENV is production.
Debug bool `env:"APP_DEBUG"`
// URL is the public origin of the app, used for absolute URLs.
URL string `env:"APP_URL" default:"http://localhost:3000"`
// Key is the secret key that signs and encrypts; see the APP_KEY section of docs/configuration.md.
Key config.Secret `env:"APP_KEY"`
// PreviousKeys are former APP_KEY values (comma-separated, same format)
// that still open what they sealed or signed after a key rotation:
// app.Crypt tokens (encrypted cookies and columns, queued mail), cookie
// sessions, the maintenance bypass and, through DerivePreviousKeys,
// signed URLs and social login state. New values always use APP_KEY.
PreviousKeys config.Secret `env:"APP_PREVIOUS_KEYS"`
// Host is the listen address (0.0.0.0 listens on all IPv4 interfaces).
Host string `env:"HOST" default:"localhost"`
// Port is the listen port, 0 to 65535; 0 picks a free port.
Port int `env:"PORT" default:"3000"`
// ShutdownTimeout is how long a graceful shutdown may take.
ShutdownTimeout time.Duration `env:"SHUTDOWN_TIMEOUT" default:"15s"`
// H2C serves HTTP/2 without TLS (prior knowledge, for gRPC clients) in
// production too, behind a proxy that speaks h2c. It is always on
// outside production.
H2C bool `env:"HTTP_H2C"`
// TLSCertFile and TLSKeyFile (PEM) serve HTTPS on PORT with HTTP/2
// (ALPN h2) and HTTP/1.1, TLS 1.2 or later; the pair is reloaded when
// either file changes. Set both or neither. See WithTLSConfig.
TLSCertFile string `env:"TLS_CERT_FILE"`
// TLSKeyFile is the PEM private key that pairs with TLSCertFile.
TLSKeyFile string `env:"TLS_KEY_FILE"`
// TLSRedirectAddr (e.g. ":80") also listens for plain HTTP there and
// redirects every request (308) to APP_URL, which must be https. Without
// it a TLS server has no plaintext listener.
TLSRedirectAddr string `env:"TLS_REDIRECT_ADDR"`
// MaxHeaderBytes caps the request line plus headers the HTTP server
// reads (4096 to 1048576 bytes, Go's own default; outside is a boot
// error). Larger requests get 431.
MaxHeaderBytes int `env:"HTTP_MAX_HEADER_BYTES" default:"65536"`
// LogLevel is debug, info, warn or error.
LogLevel string `env:"LOG_LEVEL" default:"info"`
// LogFormat is text or json; empty means json in production and text elsewhere.
LogFormat string `env:"LOG_FORMAT"`
// LogTruncateIP makes the request log write the /24 (IPv4) or /48
// (IPv6) network instead of the client address (data minimisation).
LogTruncateIP bool `env:"LOG_TRUNCATE_IP"`
// NoIndex sends X-Robots-Tag: noindex, nofollow on every response, so
// search engines never index the site (staging, previews). Pass it to
// robots.File.NoIndex for a disallow-all robots.txt too.
NoIndex bool `env:"NOINDEX"`
// LogDedupWindow collapses repeated identical ERROR records of the
// default logger into one summary per window (a Go duration; "0"
// disables it). Empty means 1m in production and off elsewhere.
LogDedupWindow string `env:"LOG_DEDUP_WINDOW"`
// APIVersion is info.version of the OpenAPI document.
APIVersion string `env:"API_VERSION" default:"1.0.0"`
// OpenAPIPrefix keeps only routes under it in the document (e.g. /api).
OpenAPIPrefix string `env:"OPENAPI_PREFIX"`
// OpenAPIServers lists the document's server URLs, comma-separated
// (default APP_URL); WithOpenAPI's Servers win over it.
OpenAPIServers []string `env:"OPENAPI_SERVERS"`
// OpenAPIStripPrefix removes this path prefix from the documented paths
// (openapi.Info.StripPrefix), for a server URL that already ends with
// it; WithOpenAPI's StripPrefix wins over it.
OpenAPIStripPrefix string `env:"OPENAPI_STRIP_PREFIX"`
// OpenAPIRequiredOutputs sets openapi.Info.RequiredOutputs: response
// schemas list the fields the server always sends as required.
OpenAPIRequiredOutputs bool `env:"OPENAPI_REQUIRED_OUTPUTS"`
// OpenAPITypedOnly sets openapi.Info.TypedOnly: a plain route under
// the prefix fails the document build.
OpenAPITypedOnly bool `env:"OPENAPI_TYPED_ONLY"`
// OpenAPIInProduction serves /openapi.json in production too.
OpenAPIInProduction bool `env:"OPENAPI_IN_PRODUCTION"`
// Devtools enables the /_vukem dashboard (refused in production).
Devtools bool `env:"DEVTOOLS"`
// DevtoolsInsecureAllowRemote serves the dashboard to non-loopback
// clients (containers, remote VMs). Anyone who reaches the app then
// sees its requests, queries, logs and mail.
DevtoolsInsecureAllowRemote bool `env:"DEVTOOLS_INSECURE_ALLOW_REMOTE"`
// TrustedProxies lists the proxies (comma-separated CIDRs or IPs) whose
// X-Forwarded-For is honoured by c.IP(). Empty trusts nobody; an
// invalid entry is a boot error.
TrustedProxies string `env:"TRUSTED_PROXIES"`
// TrustForwardedProto treats X-Forwarded-Proto: https from a trusted
// proxy as HTTPS (HSTS). It requires TRUSTED_PROXIES.
TrustForwardedProto bool `env:"TRUST_FORWARDED_PROTO"`
// HSTSForce sends HSTS on every response in production, for TLS
// proxies whose range cannot be put in TRUSTED_PROXIES. It requires an
// https APP_URL in every environment but is applied only in production
// (docs/security.md, A02).
HSTSForce bool `env:"HSTS_FORCE"`
// ProblemBaseURL prefixes the "type" URI of every RFC 9457 problem
// (BASE/not-found, BASE/validation); empty means "about:blank".
// It must be an http or https URL without credentials, query or
// fragment; a trailing slash is dropped (docs/security.md, A05).
ProblemBaseURL string `env:"APP_PROBLEM_BASE_URL"`
// ClientIPHeader names a single-IP header set by the trusted proxy
// (such as X-Real-IP) that c.IP() reads instead of X-Forwarded-For.
// It requires TRUSTED_PROXIES.
ClientIPHeader string `env:"CLIENT_IP_HEADER"`
// RateLimitIPv6Prefix is the prefix length ratelimit.ByIP groups IPv6
// clients by (1-128): one client usually owns a whole /64 and can
// rotate addresses inside it. IPv4 keys stay per address.
RateLimitIPv6Prefix int `env:"RATE_LIMIT_IPV6_PREFIX" default:"64"`
// Analytics configures Umami, Google Analytics 4 and Search Console
// verification (UMAMI_*, GA_*, GOOGLE_SITE_VERIFICATION*); all off
// when unset.
Analytics analytics.Config
// BingWebmaster holds BING_WEBMASTER_API_KEY for bingwmt.New (off
// when unset).
BingWebmaster bingwmt.Config
// IndexNow configures INDEXNOW_KEY (served at /{key}.txt) for
// indexnow.New; off when unset.
IndexNow indexnow.Config
}
// Origin returns APP_URL without a trailing slash or any path, such as
// "https://app.example.com", for building absolute URLs. It returns APP_URL
// trimmed of "/" when it does not parse as a URL.
func (c *Config) Origin() string {
u, err := url.Parse(c.URL)
if err != nil || u.Scheme == "" || u.Host == "" {
return strings.TrimSuffix(c.URL, "/")
}
return u.Scheme + "://" + u.Host
}
// CallbackURL returns Origin() plus path ("/auth/google/callback"), for
// provider redirect URLs. In production it fails unless APP_URL is an https
// origin without a path on a public host, so a deploy left on
// http://localhost:3000 stops at boot instead of at the provider.
func (c *Config) CallbackURL(path string) (string, error) {
if !strings.HasPrefix(path, "/") || strings.HasPrefix(path, "//") {
return "", fmt.Errorf("vukem: callback path %q must start with a single /", path)
}
if c.IsProduction() {
u, err := url.Parse(c.URL)
if err != nil || u.Scheme != "https" || u.Host == "" || strings.Trim(u.Path, "/") != "" ||
u.RawQuery != "" || u.User != nil {
return "", fmt.Errorf("vukem: APP_URL %q must be a plain https origin in production", c.URL)
}
if ip, _ := netip.ParseAddr(u.Hostname()); u.Hostname() == "localhost" ||
strings.HasSuffix(u.Hostname(), ".localhost") || ip.IsLoopback() || ip.IsUnspecified() {
return "", fmt.Errorf("vukem: APP_URL %q points at localhost in production", c.URL)
}
}
return c.Origin() + path, nil
}
// IsProduction reports whether APP_ENV is production.
func (c *Config) IsProduction() bool { return c.Env == config.Production }
// App is a Vukem application: configuration, logger, providers and the
// long-running runners (HTTP server, workers). Services added by providers
// live in typed fields of the structs that embed or reference the App.
type App struct {
// Config is the core configuration.
Config Config
// Source gives access to every configuration value (for module configs).
Source *config.Source
// Key is the decoded APP_KEY; derive purpose-specific keys from it
// (DeriveKey returns an error instead of panicking when it is unset).
// It is zero only outside production when APP_KEY is unset.
Key config.Key
// Crypt encrypts with a key derived from APP_KEY and still decrypts
// values sealed with APP_PREVIOUS_KEYS. DB() hands it to orm.Encrypted.
// It is nil when Key is zero: Open and Decrypt then return
// crypt.ErrNoKey, while Seal and Encrypt panic.
Crypt *crypt.Encrypter
// Log is the application logger.
Log *slog.Logger
// Kind is the runtime kind (web, console, test).
Kind Kind
// Router holds the HTTP routes; /up is registered by default.
Router *router.Router
// Views renders templates when WithViews was given (nil otherwise).
Views *view.Engine
// Vite renders asset tags when WithVite was given (nil otherwise).
Vite *vite.Vite
// Inertia is the Inertia adapter when WithInertia was given (nil
// otherwise).
Inertia *inertia.Inertia
// Version is the build version given by WithVersion (empty otherwise).
Version string
// I18n holds the translations when WithTranslations was given (nil
// otherwise); DefaultMiddleware then resolves the request locale.
I18n *i18n.Bundle
providers []Provider
runners []namedRunner
hooks []func(context.Context) error
fuzzAuth func(context.Context, *App) (FuzzAuth, error)
commands map[string]Command
viewPages []view.Decl
health health
stdout io.Writer
stderr io.Writer
stdin io.Reader
ready chan<- string
tlsConfig *tls.Config
noHTTP bool
// keyOptional is WithKeyOptional: DeriveKey names the option in its error.
keyOptional bool
// bootRoutes run at the end of Boot (WithRoutesAfterBoot).
bootRoutes []func(r *router.Router, app *App) error
sessOnce sync.Once
sessions *session.Manager
sessStore session.Store
// previous are the decoded APP_PREVIOUS_KEYS (DerivePreviousKeys).
previous []config.Key
// sessStoreFunc builds sessStore on first use (WithSessionStoreFunc).
sessStoreFunc func(*App) (session.Store, error)
// sessPruner is the session store when it keeps expired rows (database).
sessPruner sessionPruner
sessErr error
mail mailState
inst instruments
cacheOnce sync.Once
cache *cache.Cache
cacheErr error
flagDefs []flags.AnyFlag
flagsOnce sync.Once
ff *flags.Flags
flagsErr error
// Telegram is the Telegram log handler; use it for Contact and Send.
// It is never nil: without TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID it
// is disabled (Configured reports false, Send drops, SendSync returns
// telegram.ErrDisabled).
Telegram *telegram.Handler
// telegramLogs reports whether log records are forwarded to Telegram
// (TELEGRAM_LOG).
telegramLogs bool
// Events is the application event bus (WithListeners registers listeners).
Events *events.Bus
// Devtools is the development dashboard when WithDevtools or
// DEVTOOLS=true enabled it (nil otherwise).
Devtools *devtools.Devtools
jobs []queue.Registration
jobFuncs []func(*App) []queue.Registration
queueDriver queue.Driver
// queueWork overrides the QUEUE_* worker settings of the queue runner
// inside serve (WithQueueWork).
queueWork queue.WorkOptions
cacheStore cache.Store
// Built on first use of Queue() / Cache() (WithQueueDriverFunc,
// WithCacheStoreFunc).
queueDriverFunc func(*App) (queue.Driver, error)
cacheStoreFunc func(*App) (cache.Store, error)
// rateLimiters replaces the RATE_LIMIT_STORE factory (WithRateLimiters).
rateLimiters ratelimit.Factory
// openAPIInfo is the document base App.OpenAPI builds from
// (WithOpenAPI); nil falls back to the configuration alone.
openAPIInfo *openapi.Info
schedules []func(*schedule.Schedule, *App)
scheduleOnce sync.Once
schedule *schedule.Schedule
scheduleErr error
queueOnce sync.Once
queue *queue.Queue
queueErr error
// queueDB is the database queue driver Boot prepared (tables created).
queueDB *queue.Database
// queueFunc* hold the queueDriverFunc result, resolved once at Boot
// (QUEUE_CONNECTION=database) or by the first Queue call.
queueFuncOnce sync.Once
queueFuncDriver queue.Driver
queueFuncErr error
now func() time.Time
// proxies is the parsed TRUSTED_PROXIES.
proxies []*net.IPNet
dbOnce sync.Once
db *database.DB
dbErr error
drivers []database.Driver
queryObs []database.QueryObserver
modelList []orm.ModelInfo
migrations []schema.Migration
seeders []AppSeeder
// benchScenarios are the load-test scenarios of bench:run (WithBench).
benchScenarios []BenchScenario
// hiddenCommands are left out of the "list" output (WithHiddenCommands).
hiddenCommands map[string]bool
// aboutInfo adds lines to the about command (WithAbout).
aboutInfo []func(app *App) [][2]string
mu sync.Mutex
booted bool
// bootMu serializes Boot, so a concurrent call waits for the first
// one and gets its error.
bootMu sync.Mutex
bootErr error
shutdown bool
}
// readConfigFile reads the file an "@file:" configuration value names
// (Docker/Kubernetes secrets; symlinks are followed, a relative path is
// resolved against the working directory). Only regular files are read:
// the path is checked with os.Stat before it is opened, because opening a
// FIFO or a terminal (/dev/stdin, a named pipe) blocks; the opened file
// must be the one checked (os.SameFile), and at most one byte past
// config's 1 MiB cap is read, so /dev/zero cannot exhaust memory.
func readConfigFile(name string) ([]byte, error) {
info, err := os.Stat(name)
if err != nil {
return nil, err
}
if !info.Mode().IsRegular() {
return nil, fmt.Errorf("%s is not a regular file", name)
}
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
opened, err := f.Stat()
if err != nil {
return nil, err
}
if !os.SameFile(info, opened) {
return nil, fmt.Errorf("%s changed while it was opened", name)
}
return io.ReadAll(io.LimitReader(f, 1<<20+1))
}
// Option configures New.
type Option func(*options) error
type options struct {
defaultCommand string
source *config.Source
fsys fs.FS
getenv func(string) (string, bool)
envDefaults map[string]string
readFile func(string) ([]byte, error)
logger *slog.Logger
logOut io.Writer
kind Kind
providers []Provider
runners []namedRunner
healthChecks []namedHealthCheck
version string
name string
fuzzAuth func(context.Context, *App) (FuzzAuth, error)
proxies []*net.IPNet
validate []func(app *App) error
routes []func(r *router.Router, app *App) error
bootRoutes []func(r *router.Router, app *App) error
views fs.FS
viewFuncs template.FuncMap
viewPages []view.Decl
tlsConfig *tls.Config
reactEmails fs.FS
vite fs.FS
inertia *inertia.Config
lang fs.FS
now func() time.Time
migrations []schema.Migration
seeders []AppSeeder
benchScenarios []BenchScenario
hiddenCommands map[string]bool
aboutInfo []func(app *App) [][2]string
mailTransport mail.Transport
listeners []events.Registration
jobs []queue.Registration
jobFuncs []func(*App) []queue.Registration
queueDriver queue.Driver
queueWork queue.WorkOptions
cacheStore cache.Store
sessionStore session.Store
queueFunc func(*App) (queue.Driver, error)
cacheFunc func(*App) (cache.Store, error)
sessionFunc func(*App) (session.Store, error)
rateLimiters ratelimit.Factory
openAPI *openapi.Info
eventsFake *events.Fake
schedules []func(*schedule.Schedule, *App)
drivers []database.Driver
queryObs []database.QueryObserver
modelList []orm.ModelInfo
stdout io.Writer
stderr io.Writer
stdin io.Reader
ready chan<- string
noHTTP bool
devtools bool
keyOptional bool
// deferMissingKey lets a console command be resolved before APP_KEY is
// required. Boot still refuses a missing key (see requireConfiguredKey).
deferMissingKey bool
earlyCommands []Command
telegram []func(*telegram.Config)
flags []flags.AnyFlag
}
// New builds an App: it loads and validates configuration and registers
// providers. It refuses insecure configuration (see docs/security.md, A02).
func New(opts ...Option) (*App, error) {
o := options{kind: KindWeb}
for _, opt := range opts {
if err := opt(&o); err != nil {
return nil, err
}
}
src, err := appSource(&o)
if err != nil {
return nil, err
}
if o.envDefaults != nil {
src.SetDefaults(o.envDefaults)
}
cfg, err := config.Load[Config](src)
if err != nil {
return nil, err
}
if o.name != "" && src.Get("APP_NAME") == "" {
cfg.Name = o.name
}
cfg.Devtools = cfg.Devtools || o.devtools
for _, fn := range o.validate {
probe := &App{Config: cfg, Source: src, Kind: o.kind, Version: o.version}
if err := fn(probe); err != nil {
return nil, fmt.Errorf("vukem: validate: %w", err)
}
}
key, proxies, previous, err := checkBootConfig(&cfg, src, &o)
if err != nil {
return nil, err
}
enc, err := newCrypt(key, previous)
if err != nil {
return nil, err
}
dedupWindow, err := logDedupWindow(&cfg)
if err != nil {
return nil, err
}
logs, err := newAppLogging(&o, &cfg, src, dedupWindow)
if err != nil {
return nil, err
}
logger := logs.log
now := o.now
if now == nil {
now = time.Now
}
var (
dt *devtools.Devtools
routes *router.Router
)
if cfg.Devtools {
dt = devtools.New(devtools.Config{
InsecureAllowRemote: cfg.DevtoolsInsecureAllowRemote,
Level: logLevel(&cfg),
Routes: func() []router.RouteInfo { return routes.Routes() },
Now: now,
})
logger = slog.New(slog.NewMultiHandler(logger.Handler(), dt.LogHandler()))
}
app := &App{
Config: cfg,
Source: src,
Key: key,
Crypt: enc,
previous: previous,
Log: logger,
Kind: o.kind,
Version: o.version,
fuzzAuth: o.fuzzAuth,
providers: o.providers,
runners: o.runners,
stdout: cmp.Or(o.stdout, io.Writer(os.Stdout)),
stderr: cmp.Or(o.stderr, io.Writer(os.Stderr)),
stdin: cmp.Or(o.stdin, io.Reader(os.Stdin)),
ready: o.ready,
tlsConfig: o.tlsConfig,
noHTTP: o.noHTTP,
keyOptional: o.keyOptional,
drivers: o.drivers,
queryObs: o.queryObs,
modelList: o.modelList,
now: now,
proxies: proxies,
migrations: o.migrations,
seeders: o.seeders,
benchScenarios: o.benchScenarios,
hiddenCommands: o.hiddenCommands,
aboutInfo: o.aboutInfo,
jobs: o.jobs,
jobFuncs: o.jobFuncs,
queueDriver: o.queueDriver,
queueWork: o.queueWork,
cacheStore: o.cacheStore,
sessStore: o.sessionStore,
queueDriverFunc: o.queueFunc,
cacheStoreFunc: o.cacheFunc,
rateLimiters: o.rateLimiters,
openAPIInfo: o.openAPI,
sessStoreFunc: o.sessionFunc,
schedules: o.schedules,
mail: mailState{transport: o.mailTransport},
flagDefs: o.flags,
}
if key.IsZero() && cfg.Env == config.Development && !o.keyOptional {
app.Log.Warn("APP_KEY is not set: sessions, encryption and signed URLs fail until you run `vukem key:generate`",
"env", string(cfg.Env))
}
app.Router = app.newRouter()
app.warnProxiesAndHSTS()
routes = app.Router
// Before the providers run, so a provider's AddHealthCheck with the
// same name replaces a build-time one rather than the other way round.
for _, hc := range o.healthChecks {
app.AddHealthCheck(hc.name, hc.check)
}
if dt != nil {
app.enableDevtools(dt)
}
app.attachLogging(logs)
app.initEvents(&o, dt)
if err := app.addServeRunners(&o); err != nil {
return nil, err
}
if err := app.initFrontend(&o); err != nil {
return nil, err
}
if err := app.initViews(&o); err != nil {
return nil, err
}
if err := app.registerProvidersAndRoutes(&o); err != nil {
return nil, err
}
for _, c := range o.earlyCommands {
app.AddCommand(c)
}
app.bootRoutes = o.bootRoutes
if app.Kind == KindWarmup {
if err := app.addBootRoutes(); err != nil {
return nil, err
}
}
return app, nil
}
// appSource is the WithSource source, or the one loaded from the WithEnv
// file system (the working directory by default) and environment.
func appSource(o *options) (*config.Source, error) {
if o.source != nil {
return o.source, nil
}
fsys, getenv := o.fsys, o.getenv
if fsys == nil {
fsys = os.DirFS(".")
}
if getenv == nil {
getenv = os.LookupEnv
}
readFile := o.readFile
if readFile == nil {
readFile = readConfigFile
}
return config.LoadSource(fsys, getenv, readFile)
}
// appLogging is the logger New builds, with the Telegram and dedup
// handlers whose shutdown hooks attachLogging registers.
type appLogging struct {
log *slog.Logger
tg *telegram.Handler
tgLogs bool
dedup *logging.Dedup
}
func newAppLogging(o *options, cfg *Config, src *config.Source, dedupWindow time.Duration) (appLogging, error) {
logger := o.logger
ownLogger := logger == nil
if ownLogger {
out := o.logOut
if out == nil {
out = os.Stderr
}
logger = newLogger(cfg, out)
}
tg, tgLogs, err := telegramHandler(src, cfg, o.telegram)
if err != nil {
return appLogging{}, err
}
if tgLogs {
logger = slog.New(slog.NewMultiHandler(logger.Handler(), tg))
}
// Dedup sits in front of Telegram too, so an error burst is one chat
// message plus a summary. A WithLogger logger is used as given.
var dedup *logging.Dedup
if ownLogger && dedupWindow > 0 {
dedup = logging.NewDedup(logger.Handler(), logging.DedupOptions{Window: dedupWindow})
logger = slog.New(dedup)
}
return appLogging{log: logger, tg: tg, tgLogs: tgLogs, dedup: dedup}, nil
}
func (a *App) warnProxiesAndHSTS() {
if catchAll := slices.ContainsFunc(a.proxies, func(n *net.IPNet) bool {
ones, _ := n.Mask.Size()
return ones == 0
}); catchAll {
a.Log.Warn("TRUSTED_PROXIES trusts every address: any client can set its own c.IP(); " +
"trust only the proxy's network")
}
if a.Config.HSTSForce && !a.Config.IsProduction() {
a.Log.Warn("HSTS_FORCE is ignored outside production: HSTS is not forced", "env", string(a.Config.Env))
}
}
func (a *App) attachLogging(logs appLogging) {
a.Telegram = logs.tg
a.telegramLogs = logs.tgLogs
if logs.tg.Configured() {
a.OnShutdown(logs.tg.Close)
}
if logs.dedup != nil {
// Registered after Telegram so it runs first: the pending summaries
// still reach the chat.
a.OnShutdown(logs.dedup.Close)
}
}
func (a *App) initEvents(o *options, dt *devtools.Devtools) {
a.Events = events.New(a.Log, 0)
a.Events.Register(o.listeners...)
if o.eventsFake != nil {
a.Events.RecordTo(o.eventsFake)
}
if dt != nil {
a.Events.SetObserver(dt)
}
a.Router.Services().Events = a.Events
a.Events.SetQueue(func() (events.Enqueuer, error) { return a.Queue() })
a.OnShutdown(a.Events.Shutdown)
}
// addServeRunners runs the queue worker and the scheduler inside serve
// when their config asks for it.
func (a *App) addServeRunners(o *options) error {
qcfg, err := configLoad[QueueConfig](a)
if err != nil {
return err
}
if a.Kind == KindWeb && qcfg.inServe(o.queueDriver != nil || o.queueFunc != nil) {
a.AddRunner("queue", a.queueRunner(qcfg))
}
scfg, err := configLoad[ScheduleConfig](a)
if err != nil {
return err
}
if scfg.InServe && a.Kind == KindWeb {
a.AddRunner("schedule", a.scheduleRunner())
}
return nil
}
// initFrontend sets up Vite, then i18n, then Inertia, which reads Vite.
func (a *App) initFrontend(o *options) error {
if o.vite != nil {
if err := a.initVite(o.vite); err != nil {
return err
}
}
if o.lang != nil {
var err error
if a.I18n, err = i18n.Load(o.lang, i18n.Options{Default: a.Source.Get("APP_LOCALE")}); err != nil {
return err
}
if err := a.I18n.AddBuiltin(); err != nil {
return err
}
}
if o.inertia != nil {
return a.initInertia(*o.inertia, o.fsys)
}
return nil
}
func (a *App) initVite(fsys fs.FS) error {
// A hot file left by a killed dev server must not point tests or
// production at a dead Vite port (VE-7).
public := fsys
if a.Config.Env == config.Test {
// Dev stays on so a missing build renders a comment, not an error.
public = withoutHotFile{fsys}
}
var err error
if a.Vite, err = vite.New(vite.Config{FS: public, Dev: !a.Config.IsProduction()}); err != nil {
return err
}
if hot := a.Vite.HotURL(); hot != "" {
a.Log.Warn("vite: the public hot file points assets and SSR at the dev server; "+
"delete it if the dev server is not running", "file", "hot", "url", hot)
}
return nil
}
func (a *App) initInertia(icfg inertia.Config, project fs.FS) error {
if icfg.Version == nil && a.Vite != nil {
icfg.Version = a.Vite.Version
}
if icfg.SSR != nil && icfg.SSR.HotURL == nil && a.Vite != nil {
ssr := *icfg.SSR
ssr.HotURL = a.Vite.HotURL
icfg.SSR = &ssr
}
var err error
if a.Inertia, err = inertia.New(icfg); err != nil {
return err
}
// Strict SSR answers 503 when the SSR server is down, so the instance
// is not ready without it.
if icfg.SSR != nil && icfg.SSR.Strict {
a.AddHealthCheck("inertia-ssr", a.Inertia.SSRHealthy)
}
// Set only when nil, so a hook installed earlier is kept; routes run
// later and may replace it.
if a.Router.Services().ErrorPage == nil {
a.Router.Services().ErrorPage = a.Inertia.ErrorPage
}
// Without WithEnv the project is the working directory, as for config.
if project == nil {
project = os.DirFS(".")
}
if comp := cmp.Or(icfg.ErrorComponent, "Error"); !a.Config.IsProduction() && a.Kind == KindWeb &&
inertiaPageMissing(project, comp) {
a.Log.Warn("inertia error page not found: errors on Inertia visits will not render",
"component", comp, "want", inertiaPagesDir+"/"+comp+".{tsx,vue,svelte}",
"fix", "add the page, set inertia.Config.ErrorComponent, or set app.Router.Services().ErrorPage = nil")
}
return nil
}
func (a *App) initViews(o *options) error {
if o.views == nil && o.reactEmails == nil && len(o.viewFuncs) > 0 {
return errors.New("vukem: WithViewFuncs needs WithViews; pass vukem.WithViews(...) to vukem.New as well")
}
if o.views == nil && len(o.viewPages) > 0 {
return errors.New("vukem: WithViewPages needs WithViews")
}
a.viewPages = o.viewPages
if o.views != nil {
engine, err := view.New(view.Config{
FS: o.views, Reload: !a.Config.IsProduction(), URLs: a.Router, Vite: a.Vite, I18n: a.I18n,
Funcs: o.viewFuncs, Analytics: a.Config.Analytics,
})
if err != nil {
return err
}
a.Views = engine
a.Router.Services().Views = engine
}
if o.reactEmails != nil {
// No layouts: an exported React Email page is a whole document.
engine, err := view.New(view.Config{
FS: o.reactEmails, Reload: !a.Config.IsProduction(), URLs: a.Router, I18n: a.I18n, Funcs: o.viewFuncs,
})
if err != nil {
return fmt.Errorf("vukem: WithReactEmails: %w", err)
}
a.mail.react = engine
}
return nil
}
// duplicateProvider reports the first provider name used twice.
func duplicateProvider(providers []Provider) error {
seen := map[string]Provider{}
for _, p := range providers {
np, ok := p.(NamedProvider)
if !ok || np.Name() == "" {
continue
}
if first, dup := seen[np.Name()]; dup {
return fmt.Errorf("vukem: provider %q registered twice (%T and %T); remove one of them from WithProviders",
np.Name(), first, p)
}
seen[np.Name()] = p
}
return nil
}
func (a *App) registerProvidersAndRoutes(o *options) error {
if err := duplicateProvider(a.providers); err != nil {
return err
}
for _, p := range a.providers {
if !runsIn(p, a.Kind) {
continue
}
if err := p.Register(a); err != nil {
return fmt.Errorf("vukem: register %T: %w", p, err)
}
}
for _, fn := range o.routes {
if err := fn(a.Router, a); err != nil {
return fmt.Errorf("vukem: routes: %w", err)
}
}
return nil
}
// Stdout is where commands print results (WithStdout, os.Stdout by
// default). Commands write here instead of os.Stdout so tests can capture
// or silence them.
func (a *App) Stdout() io.Writer { return a.stdout }
// Stderr is where commands print diagnostics (WithStderr, os.Stderr by
// default).
func (a *App) Stderr() io.Writer { return a.stderr }
// Stdin is where commands read input (WithStdin, os.Stdin by default).
func (a *App) Stdin() io.Reader { return a.stdin }
// DeriveKey returns an n-byte key for purpose derived from APP_KEY (see
// config.Key.Derive). It fails with config.ErrKeyMissing when the app
// booted without APP_KEY, so a key-derived feature fails closed.
func (a *App) DeriveKey(purpose string, n int) ([]byte, error) {
if a.Key.IsZero() {
if a.keyOptional {
return nil, fmt.Errorf(
"vukem: %s: %w (the app was built with WithKeyOptional; set APP_KEY to use this feature)",
purpose,
config.ErrKeyMissing,
)
}
return nil, fmt.Errorf("vukem: %s: %w", purpose, config.ErrKeyMissing)
}
return a.Key.Derive(purpose, n), nil