-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
566 lines (537 loc) · 17.3 KB
/
options.go
File metadata and controls
566 lines (537 loc) · 17.3 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
// Copyright 2025 The Rivaas Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"crypto/tls"
"fmt"
"time"
"rivaas.dev/errors"
"rivaas.dev/openapi"
"rivaas.dev/router"
"rivaas.dev/validation"
)
// Option defines functional options for app configuration.
// Option functions are used to configure an App instance during creation.
type Option func(*config)
// WithServiceName sets the service name used in observability metadata.
// An empty name causes validation to fail during [New].
//
// Example:
//
// app.New(app.WithServiceName("my-api"))
func WithServiceName(name string) Option {
return func(c *config) {
c.serviceName = name
}
}
// WithServiceVersion sets the service version used in observability metadata.
// An empty version causes validation to fail during [New].
//
// Example:
//
// app.New(app.WithServiceVersion("v1.0.0"))
func WithServiceVersion(version string) Option {
return func(c *config) {
c.serviceVersion = version
}
}
// WithEnvironment sets the environment mode.
// Valid values are "development" or "production". Invalid values cause
// validation to fail during [New].
//
// Environment affects:
// - Access log scope (when unset via [WithAccessLogScope], production defaults to errors-only, development to all)
// - Startup banner (development shows route table)
// - Terminal colors (production strips ANSI sequences)
//
// Example:
//
// app.New(app.WithEnvironment("production"))
func WithEnvironment(env string) Option {
return func(c *config) {
c.environment = env
}
}
// WithPort sets the server listen port.
// Default is 8080 for HTTP; when using [WithTLS] or [WithMTLS] the default is 8443.
// Override with WithPort(n) in all cases. Can be overridden by RIVAAS_PORT when [WithEnv] is used.
//
// Example:
//
// app.New(app.WithPort(3000))
func WithPort(port int) Option {
return func(c *config) {
c.server.port = port
}
}
// WithHost sets the host/interface to bind the HTTP server to.
// Default is "" (all interfaces, equivalent to "0.0.0.0").
// Use "127.0.0.1" or "localhost" to restrict to local connections only.
//
// Example:
//
// // Bind to all interfaces (default)
// app.New(app.WithPort(8080))
//
// // Bind to localhost only (e.g., behind reverse proxy)
// app.New(
// app.WithHost("127.0.0.1"),
// app.WithPort(8080),
// )
func WithHost(host string) Option {
return func(c *config) {
c.server.host = host
}
}
// ServerOption configures server settings.
type ServerOption func(*serverConfig)
// WithReadTimeout sets the server read timeout.
// WithReadTimeout configures how long the server waits to read the entire request.
//
// Example:
//
// app.New(
// app.WithServer(
// app.WithReadTimeout(10 * time.Second),
// ),
// )
func WithReadTimeout(d time.Duration) ServerOption {
return func(sc *serverConfig) {
sc.readTimeout = d
}
}
// WithWriteTimeout sets the server write timeout.
// WithWriteTimeout configures how long the server waits to write the response.
//
// Example:
//
// app.New(
// app.WithServer(
// app.WithWriteTimeout(10 * time.Second),
// ),
// )
func WithWriteTimeout(d time.Duration) ServerOption {
return func(sc *serverConfig) {
sc.writeTimeout = d
}
}
// WithIdleTimeout sets the server idle timeout.
// WithIdleTimeout configures how long the server waits for the next request on a keep-alive connection.
//
// Example:
//
// app.New(
// app.WithServer(
// app.WithIdleTimeout(60 * time.Second),
// ),
// )
func WithIdleTimeout(d time.Duration) ServerOption {
return func(sc *serverConfig) {
sc.idleTimeout = d
}
}
// WithReadHeaderTimeout sets the server read header timeout.
// WithReadHeaderTimeout configures how long the server waits to read request headers.
//
// Example:
//
// app.New(
// app.WithServer(
// app.WithReadHeaderTimeout(2 * time.Second),
// ),
// )
func WithReadHeaderTimeout(d time.Duration) ServerOption {
return func(sc *serverConfig) {
sc.readHeaderTimeout = d
}
}
// WithMaxHeaderBytes sets the maximum size of request headers.
// WithMaxHeaderBytes configures the maximum number of bytes allowed in request headers.
//
// Example:
//
// app.New(
// app.WithServer(
// app.WithMaxHeaderBytes(1 << 20), // 1MB
// ),
// )
func WithMaxHeaderBytes(n int) ServerOption {
return func(sc *serverConfig) {
sc.maxHeaderBytes = n
}
}
// WithShutdownTimeout sets the graceful shutdown timeout.
// WithShutdownTimeout configures how long the server waits for graceful shutdown to complete.
//
// Example:
//
// app.New(
// app.WithServer(
// app.WithShutdownTimeout(30 * time.Second),
// ),
// )
func WithShutdownTimeout(d time.Duration) ServerOption {
return func(sc *serverConfig) {
sc.shutdownTimeout = d
}
}
// WithServer configures server settings using functional options.
//
// Example:
//
// app.New(
// app.WithServer(
// app.WithReadTimeout(15 * time.Second),
// app.WithWriteTimeout(15 * time.Second),
// app.WithShutdownTimeout(30 * time.Second),
// ),
// )
func WithServer(opts ...ServerOption) Option {
return func(c *config) {
// Apply options to the existing server config (which already has defaults)
for i, opt := range opts {
if opt == nil {
c.validationErrors = append(c.validationErrors, fmt.Errorf("app: server option at index %d cannot be nil", i))
continue
}
opt(c.server)
}
}
}
// WithTLS configures the server to serve HTTPS using the given certificate and key files.
// Only one of WithTLS or WithMTLS may be used. Both certFile and keyFile must be non-empty.
// Default listen port is 8443 unless overridden by [WithPort] or RIVAAS_PORT when [WithEnv] is used.
//
// Example:
//
// app.New(
// app.WithServiceName("my-api"),
// app.WithTLS("server.crt", "server.key"), // default port 8443; use WithPort(443) to override
// )
// // ...
// app.Start(ctx)
func WithTLS(certFile, keyFile string) Option {
return func(c *config) {
c.server.tlsCertFile = certFile
c.server.tlsKeyFile = keyFile
if c.server.port == DefaultPort {
c.server.port = DefaultTLSPort
}
}
}
// WithMTLS configures the server to serve HTTPS with mutual TLS (mTLS) using the given
// server certificate and options. Only one of WithTLS or WithMTLS may be used.
// Default listen port is 8443 unless overridden by [WithPort] or RIVAAS_PORT when [WithEnv] is used.
//
// Example:
//
// serverCert, _ := tls.LoadX509KeyPair("server.crt", "server.key")
// app.New(
// app.WithServiceName("my-api"),
// app.WithMTLS(serverCert,
// app.WithClientCAs(caCertPool),
// app.WithMinVersion(tls.VersionTLS13),
// ), // default port 8443; use WithPort(443) to override
// )
// // ...
// app.Start(ctx)
func WithMTLS(serverCert tls.Certificate, opts ...MTLSOption) Option {
return func(c *config) {
c.server.mtlsServerCert = serverCert
c.server.mtlsOpts = c.server.mtlsOpts[:0]
for i, opt := range opts {
if opt == nil {
c.validationErrors = append(c.validationErrors, fmt.Errorf("app: mTLS option at index %d cannot be nil", i))
continue
}
c.server.mtlsOpts = append(c.server.mtlsOpts, opt)
}
if c.server.port == DefaultPort {
c.server.port = DefaultTLSPort
}
}
}
// WithMiddleware adds middleware during app initialization.
// Middleware provided here will be added before any middleware added via Use().
// Multiple calls to WithMiddleware are supported and will accumulate.
//
// Note: This does not affect default middleware (recovery). Use WithoutDefaultMiddleware()
// to disable default middleware.
//
// Example:
//
// app.New(
// app.WithServiceName("my-service"),
// app.WithMiddleware(
// middleware.Logger(),
// middleware.Recovery(),
// ),
// )
func WithMiddleware(middlewares ...HandlerFunc) Option {
return func(c *config) {
if c.middleware == nil {
c.middleware = &middlewareConfig{}
}
for i, middleware := range middlewares {
if middleware == nil {
c.validationErrors = append(c.validationErrors, fmt.Errorf("app: middleware at index %d cannot be nil", i))
continue
}
c.middleware.functions = append(c.middleware.functions, middleware)
}
}
}
// WithoutDefaultMiddleware disables the default middleware (recovery).
// Use this when you want full control over middleware and don't want the framework
// to automatically add recovery middleware.
//
// Example:
//
// app.New(
// app.WithServiceName("my-service"),
// app.WithoutDefaultMiddleware(),
// app.WithMiddleware(myCustomRecovery), // Add your own
// )
func WithoutDefaultMiddleware() Option {
return func(c *config) {
if c.middleware == nil {
c.middleware = &middlewareConfig{}
}
c.middleware.disableDefaults = true
}
}
// WithRouter passes router options through to the underlying router.
//
// Example:
//
// app := app.New(
// app.WithServiceName("my-service"),
// app.WithRouter(
// router.WithBloomFilterSize(2000),
// router.WithoutCancellationCheck(),
// router.WithTemplateRouting(true),
// router.WithVersioning(),
// ),
// )
//
// Multiple calls to WithRouter accumulate options.
func WithRouter(opts ...router.Option) Option {
return func(c *config) {
if c.router == nil {
c.router = &routerConfig{}
}
for i, opt := range opts {
if opt == nil {
c.validationErrors = append(c.validationErrors,
fmt.Errorf("app: router option at index %d cannot be nil", i))
continue
}
c.router.options = append(c.router.options, opt)
}
}
}
// WithValidationEngine sets the validation engine used by [Context.Bind] and [Context.Validate].
// When set, the app uses this engine instead of the package-level [validation.DefaultEngine].
// Use this for custom validation configuration (e.g. redaction, MaxErrors) or test isolation.
//
// Example:
//
// engine := validation.MustNew(validation.WithRedactor(myRedactor))
// app := app.MustNew(
// app.WithServiceName("my-api"),
// app.WithValidationEngine(engine),
// )
func WithValidationEngine(engine *validation.Engine) Option {
return func(c *config) {
c.validationEngine = engine
}
}
// openapiConfig holds OpenAPI configuration for the app layer.
// openapiConfig stores OpenAPI settings and initialization state.
// options is set by WithOpenAPI and consumed in config.validate() to build config.
type openapiConfig struct {
enabled bool
options []openapi.Option // raw options until finalization in validate()
config *openapi.API
initErr error // Stores initialization error to be checked during validation
}
// WithOpenAPI enables OpenAPI specification generation with the given options.
// Service name and version are automatically injected from app-level configuration
// after all options are applied (in config validation), so option order does not matter.
// If not explicitly set via openapi.WithTitle(), the app's service name and version are used.
//
// Example:
//
// app.New(
// app.WithServiceName("my-service"),
// app.WithServiceVersion("v1.0.0"),
// app.WithOpenAPI(
// openapi.WithTitle("My API", "1.0.0"),
// openapi.WithDescription("API description"),
// openapi.WithBearerAuth("bearerAuth", "JWT authentication"),
// openapi.WithServer("http://localhost:8080", "Local development"),
// openapi.WithSwaggerUI(true, "/docs"),
// openapi.WithUIDocExpansion(openapi.DocExpansionList),
// openapi.WithUISyntaxTheme(openapi.SyntaxThemeMonokai),
// ),
// )
func WithOpenAPI(opts ...openapi.Option) Option {
return func(c *config) {
c.openapi = &openapiConfig{
enabled: true,
options: opts,
}
}
}
// WithErrorFormatterFor configures an error formatter from options.
// The app builds the formatter via errors.New(opts...); invalid options are reported during config validation.
//
// Use empty mediaType ("") for a single formatter for all responses (no content negotiation).
// Use a non-empty mediaType (e.g. "application/problem+json") to register a formatter for content negotiation;
// multiple calls accumulate. Cannot mix: use either a single formatter ("") or content-negotiated formatters, not both.
//
// Example:
//
// // Single formatter for all responses
// app.New(
// app.WithServiceName("my-service"),
// app.WithErrorFormatterFor("", errors.WithRFC9457("https://api.example.com/problems")),
// )
//
// // Content negotiation by Accept header
// app.New(
// app.WithServiceName("my-service"),
// app.WithErrorFormatterFor("application/problem+json", errors.WithRFC9457("https://api.example.com/problems")),
// app.WithErrorFormatterFor("application/json", errors.WithSimple()),
// app.WithDefaultErrorFormat("application/problem+json"),
// )
func WithErrorFormatterFor(mediaType string, opts ...errors.Option) Option {
return func(c *config) {
if c.errors == nil {
c.errors = &errorsConfig{}
}
formatter, err := errors.New(opts...)
if err != nil {
c.errors.initErr = err
return
}
c.errors.initErr = nil
if mediaType == "" {
if len(c.errors.formatters) > 0 {
c.errors.modeErr = fmt.Errorf("cannot use single error formatter when content-negotiated formatters are configured")
return
}
c.errors.formatter = formatter
c.errors.formatters = nil
c.errors.singleFormatterExplicitlySet = true
return
}
if c.errors.singleFormatterExplicitlySet {
c.errors.modeErr = fmt.Errorf("cannot use content-negotiated formatters when single error formatter is configured")
return
}
// Switch to content-negotiated mode: clear single formatter and add to map.
if c.errors.formatters == nil {
c.errors.formatters = make(map[string]errors.Formatter)
}
c.errors.formatters[mediaType] = formatter
c.errors.formatter = nil
}
}
// WithErrorFormatters configures multiple error formatters with content negotiation by Accept header.
// Advanced: use when you need to pass pre-built or custom formatters. Prefer [WithErrorFormatterFor]
// for option-based configuration.
//
// Example:
//
// app.New(
// app.WithServiceName("my-service"),
// app.WithErrorFormatters(map[string]errors.Formatter{
// "application/problem+json": errors.MustNew(errors.WithRFC9457("https://api.example.com/problems")),
// "application/json": errors.MustNew(errors.WithSimple()),
// }),
// app.WithDefaultErrorFormat("application/problem+json"),
// )
func WithErrorFormatters(formatters map[string]errors.Formatter) Option {
return func(c *config) {
if c.errors == nil {
c.errors = &errorsConfig{}
}
c.errors.formatters = formatters
c.errors.formatter = nil
c.errors.singleFormatterExplicitlySet = false
}
}
// WithDefaultErrorFormat sets the default format when no Accept header matches.
// Only used when content-negotiated formatters are configured (via [WithErrorFormatterFor] with non-empty media types or [WithErrorFormatters]).
//
// Example:
//
// app.New(
// app.WithErrorFormatterFor("application/problem+json", errors.WithRFC9457("...")),
// app.WithErrorFormatterFor("application/json", errors.WithSimple()),
// app.WithDefaultErrorFormat("application/problem+json"),
// )
func WithDefaultErrorFormat(mediaType string) Option {
return func(c *config) {
if c.errors == nil {
c.errors = &errorsConfig{}
}
c.errors.defaultFormat = mediaType
}
}
// WithObservability configures all observability components: metrics, tracing, and logging.
// This is the single entry point for configuring the three pillars of observability.
//
// Components:
// - WithLogging: enables structured logging (service name/version auto-injected)
// - WithMetrics: enables metrics collection (Prometheus, OTLP)
// - WithTracing: enables distributed tracing (OTLP, Jaeger)
//
// Shared settings (apply to all components):
// - WithExcludePaths, WithExcludePrefixes, WithExcludePatterns, WithoutDefaultExclusions
// - WithAccessLogging, WithAccessLogScope, WithSlowThreshold
//
// Default exclusions include common health/probe paths:
// /health, /livez, /ready, /readyz, /live, /metrics, /debug/*
//
// Example:
//
// app.MustNew(
// app.WithServiceName("orders-api"),
// app.WithServiceVersion("v1.0.0"),
// app.WithObservability(
// app.WithLogging(logging.WithJSONHandler(), logging.WithDebugLevel()),
// app.WithMetrics(), // Prometheus is default; use metrics.WithOTLP() for OTLP
// app.WithTracing(tracing.WithOTLP("localhost:4317")),
// app.WithExcludePaths("/custom-health"),
// app.WithExcludePrefixes("/internal/", "/admin/"),
// app.WithAccessLogScope(app.AccessLogScopeErrorsOnly),
// app.WithSlowThreshold(500 * time.Millisecond),
// ),
// )
func WithObservability(opts ...ObservabilityOption) Option {
return func(c *config) {
if c.observability == nil {
c.observability = defaultObservabilitySettings()
}
for i, opt := range opts {
if opt == nil {
c.observability.validationErrors = append(c.observability.validationErrors, fmt.Errorf("app: observability option at index %d cannot be nil", i))
continue
}
opt(c.observability)
}
}
}