Repository navigation
Expand file tree
/
Copy pathiris_builder.go
More file actions
1216 lines (1122 loc) · 47.2 KB
/
Copy pathiris_builder.go
File metadata and controls
1216 lines (1122 loc) · 47.2 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 iris
import (
stdContext "context"
stdErrors "errors"
"fmt"
"os"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/kataras/iris/v14/rest/errors"
"github.com/kataras/iris/v14/middleware/accesslog"
"github.com/kataras/iris/v14/middleware/compress"
"github.com/kataras/iris/v14/middleware/cors"
"github.com/kataras/iris/v14/middleware/modrevision"
"github.com/kataras/iris/v14/middleware/rate"
"github.com/kataras/iris/v14/middleware/recovery"
)
// NewBuilder returns a step-by-step builder for a production-ready Iris
// application: the common middleware, the error handling and the dependency
// container are set up in the right order for you.
//
// The order is enforced by the compiler, not by documentation. Each step
// returns a narrower interface that exposes only the steps that may follow:
//
// 1. Prefix(path) or NoPrefix(): where the API routes live
// 2. AllowOrigin(line) or Cors(*cors.CORS): CORS
// 3. Compression(bool): request and response compression
// 4. LogRequests(bool) or LogRequestsWith(*accesslog.AccessLog): the access log
// 5. Health(bool, env, developer) or NoHealth(): the /health probe
// 6. Errors(*errors.Options), Deferrables(...), Services(...)
// 7. RouterMiddlewares(...), then Middlewares(...)
// 8. API(path, controllers...), repeatable
// 9. Build() *Application
//
// Steps 1 to 6 only record what they are given. The middleware is registered
// on the first RouterMiddlewares or Middlewares call, guarded by a sync.Once,
// which is why the order it runs in is fixed no matter how the chain is
// written; see RouterMiddlewares for that order.
//
// A failure status that reaches the client with nothing written answers with
// the rest/errors JSON envelope, next to the errors the application reports
// through rest/errors itself. That covers:
//
// - a status of 400 or above set by a route handler or by a middleware
// given to RouterMiddlewares, Middlewares or API, whether or not it
// stopped the chain: ctx.StopWithStatus(403), ctx.NotFound(), the 401 of
// the jwt and basicauth middleware, a file the file server does not have;
// - an unknown path (404);
// - a method a known path does not serve (405, when
// Options.Router.FireMethodNotAllowed is on; it is off by default, and
// the answer is then the 404 one).
//
// Two failures are answered ahead of that handler and keep their bare status:
//
// - a rejection from a handler a Configurator registered with UseRouter.
// It runs before the builder's own middleware, this one included;
// - the 415 of the compression middleware, for a request body that is not
// what its Content-Encoding says.
//
// What the client reads for an error stored on the Context
// (ctx.StopWithError, ctx.StopWithPlainError, ctx.SetErr): nothing of a
// private error (iris.PrivateError), and no mapping is asked about one. It
// renders the status text below 500 ("Unauthorized") and, at 500 and above,
// the generic "server error" while the error is logged. An error a mapping
// recognises, and that is not private, renders through its mapping. Any other
// error: below 500 its text is the message; at 500 and above the text is
// logged and the client reads the generic "server error", except for a stored
// *errors.Error that is not private, which is sent as it is.
//
// A response that has a body, that declares a Content-Length, or whose status
// line is already on the wire, is left as it is, and so is a hijacked
// connection. To answer 404 or 405 yourself, register HandleNoRoute or
// HandleNoMethod from a Configurator given here: the builder fills those two
// slots only when they are empty at Build.
//
// Parameters:
// - configurators: Optional Iris application configurators for advanced setup
//
// For a worked example see the package examples for this function.
func NewBuilder(configurators ...Configurator) Builder {
app := New(configurators...)
app.RegisterDependency(app) // register itself as a dependency, so controllers/custom routers can use it directly.
// Install the rest/errors handlers here and not in the once-block below:
// a child Router copies the context options of its parent when Prefix
// creates it, which is the very first builder step. Installed any later, a
// prefixed API would keep the framework's default context error handler
// while a NoPrefix one renders the rest/errors body. The macros are not
// copied, a child shares them with its parent, but a route takes the error
// handler of its parameter types when it is registered, so that handler
// has to be in place before the first route as well.
app.Macros().SetErrorHandler(errors.DefaultPathParameterTypeErrorHandler)
app.EditOptions(func(o *Options) {
o.Context.ErrorHandler = errors.DefaultContextErrorHandler
})
return &builder{
app: app,
once: new(sync.Once),
}
}
type (
// Builder is the simplified API builder that provides a step-by-step approach
// to building production-ready Iris applications with common middleware,
// security features, and best practices built-in.
//
// The Builder enforces a logical order of configuration steps to ensure
// middleware and features are applied in the correct sequence.
Builder interface {
// Prefix sets the API Router prefix path for all registered routes.
// This is useful for versioning (e.g., "/api/v1") or organizing APIs.
//
// Parameters:
// - prefixPath: The path prefix (must start with "/")
//
// Example:
// Prefix("/api/v1") // All routes will be under /api/v1/*
//
// Returns CorsBuilder for the next configuration step.
Prefix(prefixPath string) CorsBuilder
// NoPrefix disables the API Router prefix path, making routes available
// directly under the root path. This is equivalent to Prefix("").
//
// Example:
// NoPrefix() // Routes available directly under /*
//
// Returns CorsBuilder for the next configuration step.
NoPrefix() CorsBuilder
}
// CorsBuilder configures Cross-Origin Resource Sharing (CORS) middleware.
// CORS is essential for web applications that need to handle requests
// from different domains, such as frontend applications.
CorsBuilder interface {
// AllowOrigin configures CORS allowed origins for cross-domain requests.
// This is a security-critical setting that controls which domains
// can access your API from browsers.
//
// Parameters:
// - originLine: Allowed origins, comma-separated or "*" for all origins
//
// Security Notes:
// - Use "*" only for public APIs without sensitive data
// - For production, specify exact domains: "https://yourdomain.com"
// - Multiple origins: "https://app.com,https://admin.app.com"
// - Use "none" to disable CORS middleware entirely
//
// Examples:
// AllowOrigin("*") // Allow all (use cautiously)
// AllowOrigin("https://myapp.com") // Single domain
// AllowOrigin("https://app.com,https://admin.com") // Multiple domains
// AllowOrigin("none") // Disable CORS
//
// Returns CompressionBuilder for the next configuration step.
AllowOrigin(originLine string) CompressionBuilder
// Cors registers a fully configured CORS value, for the settings
// AllowOrigin does not reach: credentials, exposed headers, max age,
// the origin extractor and the error handler.
//
// AllowOrigin(line) is the shorthand for:
//
// Cors(cors.New().HandleErrorFunc(errors.FailedPrecondition.Err).AllowOrigin(line))
//
// The value is used as given, so call HandleErrorFunc yourself to keep
// the error mapping AllowOrigin installs.
//
// A wildcard origin does not advertise credentials unless they are
// enabled explicitly, because browsers reject the "*" plus credentials
// combination. Call AllowCredentials when a non-browser client needs it:
//
// Cors(cors.New().
// HandleErrorFunc(errors.FailedPrecondition.Err).
// AllowOrigin("*").
// AllowCredentials())
//
// Returns CompressionBuilder for the next configuration step.
Cors(c *cors.CORS) CompressionBuilder
}
// CompressionBuilder configures HTTP response compression to reduce bandwidth
// usage and improve response times, especially for JSON APIs and large payloads.
CompressionBuilder interface {
// Compression enables or disables automatic response compression using
// gzip or other client-preferred algorithms (brotli, deflate).
//
// The compression middleware automatically:
// - Detects client compression support via Accept-Encoding header
// - Compresses responses above a minimum size threshold
// - Skips compression for already-compressed content types
// - Handles streaming responses efficiently
//
// Performance Notes:
// - Enable for production APIs to reduce bandwidth by 60-80%
// - Minimal CPU overhead for most applications
// - Particularly effective for JSON responses
//
// Parameters:
// - enableReadWriteCompression: true to enable, false to disable
//
// Example:
// Compression(true) // Enable compression (recommended for production)
// Compression(false) // Disable compression (useful for debugging)
//
// Returns RequestLoggingBuilder for the next configuration step.
Compression(enableReadWriteCompression bool) RequestLoggingBuilder
}
// RequestLoggingBuilder configures structured request/response logging for
// observability, debugging, and compliance requirements.
RequestLoggingBuilder interface {
// LogRequests logs every request and response as structured JSON, which is
// what makes traffic readable to both a log pipeline and a person.
//
// The access log captures:
// - Request details: method, path, headers, body
// - Response details: status code, headers, body, latency
// - Client information: IP address, user agent
// - Timing information: request duration, timestamps
// - Error details: panic logs, error traces
//
// Output destinations:
// - File: access_log.json (automatically created)
// - Console: stdout (can be disabled by modifying the middleware)
//
// Parameters:
// - enableRequestAccessLog: true to enable logging, false to disable
//
// Example:
// LogRequests(true) // Enable for development and production monitoring
// LogRequests(false) // Disable for performance-critical scenarios
//
// Returns HealthBuilder for the next configuration step.
LogRequests(enableRequestAccessLog bool) HealthBuilder
// LogRequestsWith registers the given pre-configured access log
// middleware and its closer. Use it when the tuned defaults of
// LogRequests(true) do not fit: custom output, formatter, fields.
//
// ac := accesslog.File("access_log.json")
// ac.AddFields(func(ctx iris.Context, f *accesslog.Fields) { ... })
// builder.LogRequestsWith(ac)
//
// LogRequests(true) is a shortcut for LogRequestsWith with the
// framework's tuned default configuration. A nil value is ignored.
//
// Returns HealthBuilder for the next configuration step.
LogRequestsWith(ac *accesslog.AccessLog) HealthBuilder
}
// HealthBuilder configures a health check endpoint for monitoring, load balancers,
// and container orchestration platforms like Kubernetes.
HealthBuilder interface {
// Health registers a health check endpoint at /health that reports the
// application status, the build it is running and the environment.
//
// The health endpoint returns:
// - Application version and build information
// - Environment details (development, staging, production)
// - Developer contact information
// - Cloud platform detection (AWS, GCP, Azure, etc.)
// - Module revision information
// - HTTP 200 OK status for healthy applications
//
// Use cases:
// - Load balancer health checks
// - Kubernetes liveness/readiness probes
// - Monitoring system integration
// - Deployment verification
//
// The endpoint is registered on the application's root Router, not under
// Prefix: a probe lives at /health whatever the API path is, which is
// what load balancers and orchestrators expect.
//
// Parameters:
// - enableHealthEndpoint: true to enable the /health endpoint
// - env: Environment name (e.g., "development", "staging", "production")
// - developer: Developer or team contact information. It is also the
// value of the X-Developer response header, set on every response by
// the built-in router middleware, even when the endpoint is disabled.
//
// Examples:
// Health(true, "production", "api-team@company.com")
// Health(true, "development", "john.doe@company.com")
// Health(false, "", "") // Disable health endpoint
//
// Returns ServiceBuilder for the next configuration step.
Health(enableHealthEndpoint bool, env, developer string) ServiceBuilder
// NoHealth disables the health check endpoint. This is equivalent to
// calling Health(false, "", "").
//
// Use this when:
// - Health checks are handled by external systems
// - Security policies prohibit exposing application information
// - Custom health check implementation is preferred
//
// Returns ServiceBuilder for the next configuration step.
NoHealth() ServiceBuilder
}
// ServiceBuilder configures error handling, dependency injection and graceful
// shutdown: the three parts of an application's lifecycle.
ServiceBuilder interface {
// Errors configures centralized error handling with custom error mappings,
// status codes, and response formats for consistent API error responses.
//
// The errors system provides:
// - Automatic error code mapping (database errors → HTTP status codes)
// - Structured error responses with details and correlation IDs
// - Custom error handlers for specific error types
// - Integration with logging and monitoring systems
//
// Common error mappings:
// - sql.ErrNoRows → 404 Not Found
// - validation errors → 400 Bad Request
// - authentication errors → 401 Unauthorized
// - authorization errors → 403 Forbidden
//
// Parameters:
// - options: Error handling configuration from errors.NewOptions()
//
// Example:
// Errors(errors.NewOptions().
// MapErrors(errors.NotFound, sql.ErrNoRows).
// MapErrors(errors.Unavailable, driver.ErrBadConn).
// MapErrorFunc(errors.DeadlineExceeded, func(err error) error {
// // Return an error to answer with this code, nil to pass.
// if stdErrors.Is(err, stdContext.DeadlineExceeded) {
// return fmt.Errorf("the database took too long: %w", err)
// }
// return nil
// }))
//
// Returns ServiceBuilder for chaining.
Errors(*errors.Options) ServiceBuilder
// Deferrables registers cleanup functions that Application.Shutdown
// runs, in the order given, after the server's own shutdown. `Run` and
// `Listen` return once they have finished. On an interrupt signal they
// run only when Shutdown is wired to it with
// iris.RegisterOnInterrupt(app.Shutdown): the server's built-in handler
// drains the connections and exits.
//
// Supported values (stdContext is the standard library's "context" package):
// - func() // Simple cleanup function
// - func() error // Cleanup with error handling
// - func(stdContext.Context) // Cleanup which respects the shutdown context
// - func(stdContext.Context) error // Both of the above
// - interface{ Close() error } // Objects with Close method returning error (io.Closer)
// - interface{ CloseWithContext(stdContext.Context) error } // Context-aware closers
//
// Any other value panics at registration time. An object with a plain
// Close() method (no error return) is not accepted, pass its method
// instead: Deferrables(worker.Close).
//
// Common use cases:
// - Closing database connections
// - Stopping background workers
// - Flushing caches or logs
// - Releasing external resources
//
// Parameters:
// - closers: One or more cleanup functions or closable objects
//
// Examples:
// Deferrables(func() { db.Close() })
// Deferrables(redisClient, elasticClient) // Objects with a Close() error method
// Deferrables(
// func() { worker.Stop() },
// func() error { return cache.Flush() },
// )
//
// Returns ServiceBuilder for chaining.
Deferrables(closers ...any) ServiceBuilder
// Services registers dependencies for dependency injection throughout the
// application. These dependencies can be used in controllers, middleware,
// and other components.
//
// Dependency types supported:
// - Service instances (databases, repositories, business services)
// - Configuration objects
// - Third-party clients (Redis, Elasticsearch, etc.)
// - Iris Configurators for advanced application setup
// - Functions that return services (factories)
//
// Dependency injection features:
// - Automatic constructor injection based on parameter types
// - Singleton lifecycle management
// - Interface-based dependency resolution
//
// Three values are treated specially:
// - a value with a Close() or Close() error method is registered as a
// dependency AND its Close is added to the shutdown closers, so it
// does not need a second Deferrables call;
// - an iris.Worker is registered as a dependency AND started after the
// initializers, then stopped at shutdown; when it is a closer too it
// is stopped before it is closed. See iris.Worker;
// - an iris.Configurator is applied to the application immediately and
// is not registered as a dependency.
//
// A func() or a func() error returns nothing injectable and panics
// here: pass it to Deferrables for cleanup, or implement
// iris.Initializer for startup work.
//
// Parameters:
// - dependencies: Services, configurations, or factories to register
//
// Examples:
// Services(
// database.New(config.DatabaseURL), // Database service
// repository.NewUserRepo, // Repository factory
// service.NewAuthService, // Business service factory
// &config.AppConfig{}, // Configuration object
// redis.NewClient(&redis.Options{}), // Third-party client
// )
//
// Returns MiddlewareBuilder for the next configuration step.
Services(dependencies ...any) MiddlewareBuilder
}
// MiddlewareBuilder configures the middleware pipeline with clear separation
// between global middleware (RouterMiddlewares) and API-specific middleware (Middlewares).
MiddlewareBuilder interface {
// RouterMiddlewares registers global middleware that executes for ALL requests,
// including 404s, static files, and health checks. This middleware runs at
// the top-level router before any route matching occurs.
//
// Use RouterMiddlewares for:
// - Request logging and metrics collection
// - Security headers (CORS, CSP, etc.)
// - Rate limiting across the entire application
// - Request tracing and correlation IDs
// - Path rewriting or request preprocessing
// - Global authentication checks
//
// Middleware execution order:
// 1. Built-in router middleware, registered once on the first
// RouterMiddlewares or Middlewares call, in this order:
// the rest errors options handler (Errors), the access log
// (LogRequests/LogRequestsWith), recovery, CORS (AllowOrigin/Cors),
// the Server and X-Developer headers, compression (Compression),
// the rest errors handler (rest/errors.Handler)
// 2. RouterMiddlewares (this method), in call order
// 3. API middleware (Middlewares method)
// 4. Route-specific middleware
// 5. Route handlers
//
// The order of the first group is the reason the earlier steps only
// record their configuration: the CORS rejection has to be written
// through the Errors options and be recorded by the access log, and the
// access log has to sit above recovery so that a panicking handler is
// recorded too. The rest errors handler is last so that it writes
// through the compression writer, and so that the middleware given here
// runs inside it: a handler that rejects with a bare status, such as
// ctx.StopWithStatus(429), answers the rest/errors envelope.
//
// Because the recovery middleware is registered before the middleware
// given here, a panic raised by such a handler is recovered as well.
//
// Two things sit outside that net. A panic inside the access log or
// inside the Errors options handler is raised above recovery, so
// net/http's own per-connection recovery catches it instead: the
// connection is dropped and the client sees no response at all. And
// under Prefix the access log and CORS are registered on the prefixed
// router, while the Errors handler, recovery, the Server and
// X-Developer headers, compression and the rest errors handler are
// application-wide - so a request to /health, outside the prefix, is
// neither logged nor CORS'd.
//
// Supported middleware types:
// - iris.Handler functions
// - Dependency injection functions that return iris.Handler
// - Built-in Iris middleware
//
// Parameters:
// - middlewares: One or more middleware functions or handlers
//
// Examples:
// RouterMiddlewares(func(ctx iris.Context) {
// // Global request logging
// ctx.Application().Logger().Infof("Request: %s %s", ctx.Method(), ctx.Path())
// ctx.Next()
// })
//
// RouterMiddlewares(func(logger *log.Logger) iris.Handler {
// // Middleware with dependency injection
// return func(ctx iris.Context) {
// logger.Printf("Request from IP: %s", ctx.RemoteAddr())
// ctx.Next()
// }
// })
//
// RouterMiddlewares(
// securityHeaders, // Security middleware
// requestTracing, // Tracing middleware
// rateLimiter, // Rate limiting
// )
//
// Returns MiddlewareBuilder for chaining additional router middleware.
RouterMiddlewares(middlewares ...any) MiddlewareBuilder
// Middlewares registers API-specific middleware that executes only for
// routes registered through the API() method. This middleware runs after
// router middleware but before individual route handlers.
//
// Use Middlewares for:
// - API authentication and authorization
// - Request validation specific to API routes
// - API versioning logic
// - Business logic preprocessing
// - API-specific rate limiting
// - Request/response transformation
//
// Middleware scope:
// - Applies only to routes registered via API() method
// - Does NOT apply to health checks, static files, or other routes
// - Executes after RouterMiddlewares
// - Can access services registered via dependency injection
//
// Parameters:
// - middlewares: One or more API-specific middleware functions
//
// Examples:
// Middlewares(func(ctx iris.Context) {
// // API-only authentication
// if !isAuthenticated(ctx) {
// ctx.StatusCode(401)
// return
// }
// ctx.Next()
// })
//
// Middlewares(func(authService *AuthService) iris.Handler {
// // Authentication with injected service
// return func(ctx iris.Context) {
// user, err := authService.ValidateToken(ctx)
// if err != nil {
// ctx.StatusCode(401)
// return
// }
// ctx.Values().Set("user", user)
// ctx.Next()
// }
// })
//
// Returns APIBuilder for the final configuration step.
Middlewares(middlewares ...any) APIBuilder
}
// APIBuilder is the final step for registering API controllers and building
// the complete application with all configured middleware and services.
APIBuilder interface {
// API registers controllers for a specific route group, enabling modular
// API organization with optional middleware per group.
//
// Controller types supported:
// - Structs implementing Routing(iris.Router) method
// - Factory functions that return controllers (with dependency injection)
// - Direct iris.Handler functions
// - Middleware functions (using iris.Middleware wrapper)
//
// Route group features:
// - Each API call creates a new route group under the specified path
// - Groups can have their own middleware stack
// - Controllers can access injected dependencies
// - Automatic method routing based on HTTP verbs (GET, POST, etc.)
//
// Parameters:
// - prefixPath: Route group prefix (e.g., "/users", "/admin", "/v1/products")
// - controllers: Controllers and/or middleware for this group
//
// Examples:
// // Simple controller registration
// API("/users", NewUserController)
//
// // Controller with middleware
// API("/admin",
// basicauth.Default(map[string]string{"admin": "secret"}),
// NewAdminController)
//
// // Multiple controllers in one group
// API("/api",
// NewUserController,
// NewProductController,
// NewOrderController)
//
// // Factory function with dependency injection
// API("/posts", func(db *sql.DB, cache *redis.Client) *PostController {
// return &PostController{db: db, cache: cache}
// })
//
// Controller example:
// type UserController struct {
// userService *UserService
// }
//
// func NewUserController(service *UserService) *UserController {
// return &UserController{userService: service}
// }
//
// func (c *UserController) Routing(r iris.Router) {
// r.Get("/", c.listUsers) // GET /users/
// r.Post("/", c.createUser) // POST /users/
// r.Get("/{id}", c.getUser) // GET /users/{id}
// r.Put("/{id}", c.updateUser) // PUT /users/{id}
// r.Delete("/{id}", c.deleteUser) // DELETE /users/{id}
// }
//
// Returns APIBuilder for registering additional API groups.
API(prefixPath string, controllers ...any) APIBuilder
// Build compiles and finalizes the application configuration, creating
// the complete Iris application instance ready for deployment.
//
// Build process:
// 1. Validates all registered dependencies
// 2. Initializes the dependency injection container
// 3. Applies all registered middleware in correct order
// 4. Registers all API routes and controllers
// 5. Sets up error handlers and recovery middleware
// 6. Validates route patterns and conflicts
//
// When to call Build():
// - Always: it is the terminal builder step, Listen and Run are methods
// of the *Application it returns
// - Useful for testing (build without starting the server)
// - Required for custom server configurations
//
// Returns:
// - *Application: The fully configured Iris application instance
//
// The returned Application is already built. Its mux was compiled from
// the routes registered up to this point, and a route registered on it
// afterwards is never served (the router logs a warning naming it and
// the file it came from). Register those through API instead, before
// Build.
//
// The returned Application can be used for:
// - Custom middleware registration on the pre-routing chain
// - Testing with httptest
// - Custom server startup with specific options
//
// Examples:
// // Simple usage
// app := builder.Build()
// app.Listen(":8080")
//
// // Custom server configuration
// app := builder.Build()
// app.Run(iris.ServerOptions{Addr: ":443", CertFile: "cert.pem", KeyFile: "key.pem"})
//
// // Testing
// app := builder.Build()
// e := httptest.New(t, app)
// e.GET("/api/users").Expect().Status(httptest.StatusOK)
//
// Note: Build() panics if there are configuration errors. This is
// intentional to catch issues early during application startup.
Build() *Application
}
)
// Initializer is implemented by services registered through Builder.Services
// (or RegisterDependency) that need a startup step after dependency injection
// completes and before the server starts: cache warm-up, connection priming,
// background pool setup. Init runs exactly once, inside Builder.Build, in
// service registration order; a returned error stops the application from
// building. Implementations derive their own timeouts from ctx.
// It is the startup counterpart of Builder.Deferrables.
type Initializer interface {
Init(ctx stdContext.Context) error
}
// Worker is implemented by a long-running service registered through
// Builder.Services: a scheduler, a queue consumer, a cache refresher. The
// builder starts it after every Initializer has returned and stops it when
// Application.Shutdown runs its closers, so it lives inside the startup and
// shutdown chain the builder already enforces instead of in a goroutine next
// to it. Its stop is one of those closers and runs in registration order:
// after the Deferrables and the Close of a service that Services was given
// before it, and before the Close of one given after it.
//
// A Worker is registered as a dependency too, exactly like a value with a
// Close method is, so the same value can be injected into controllers.
//
// It is the long-running counterpart of Initializer.
type Worker interface {
// Run blocks until the given context is done or the work fails. The
// context is cancelled at shutdown, just before Stop is called.
//
// An error returned before shutdown is logged with the worker's type name
// and nothing else: a failed worker does not take the HTTP server with it.
Run(ctx stdContext.Context) error
// Stop releases what Run acquired. It is called once, at shutdown, with a
// bounded context: the Application.Shutdown context when that one carries
// a deadline, otherwise a ten second one, so a Stop which hangs cannot
// hold the process open forever.
//
// Shutdown then waits for Run to return, within the same budget, so a
// worker is really finished when Shutdown reports success. A Run which
// does not return in time is logged and reported as a shutdown error.
//
// That ten second budget is per worker, not per shutdown: the closers run
// one after another, so N stuck workers cost N times ten seconds. Pass a
// deadline to Application.Shutdown to bound the whole chain instead; a
// caller-supplied deadline is used as it is and is shared by every closer.
//
// iris.RegisterOnInterrupt(app.Shutdown) supplies the deadline that
// RegisterOnInterrupt documents: the Timeout.ShutdownOnInterruptTimeout
// of the running server, 15 seconds by default, counted from the signal
// and spent first on the server's drain. After a drain that used all of
// it Stop receives a context that has expired already and Run gets no
// time to return: unless it has returned by that instant, that is the
// shutdown error above, and the process ends with status 1. Without that
// registration an interrupt signal does not call Stop at all: the server
// drains and the process exits.
Stop(ctx stdContext.Context) error
}
// workerStopTimeout bounds Worker.Stop, and the wait for Run after it, when
// the shutdown context does not carry a deadline of its own
// (app.Shutdown(context.Background())). It is spent per worker.
const workerStopTimeout = 10 * time.Second
// workerRegistration is the builder's bookkeeping for one registered Worker.
// The context and its cancel are created at registration, before any goroutine
// exists, so the field is never written after the value is published and a
// shutdown which arrives before the start still cancels the worker.
type workerRegistration struct {
worker Worker
ctx stdContext.Context
cancel stdContext.CancelFunc
// done is closed when the Run goroutine returns. Shutdown waits on it, so a
// worker is really finished when Shutdown reports success.
done chan struct{}
// startMu makes the start decision and the stop decision mutually exclusive.
// Two independent atomics are not enough: startWorker loads stopping then
// stores started, stopWorker stores stopping then loads started, and that
// shape lets both sides miss each other, so Close could run while a Run was
// about to be launched. The lock is never held across Stop or the wait.
startMu sync.Mutex
// started reports whether the Run goroutine was ever launched: nothing waits
// on done for a worker which never started. Written under startMu.
started atomic.Bool
// stopping tells the Run goroutine that the error it is about to return
// comes from the shutdown it was asked to perform, not from a failure. It
// also keeps a worker from starting after its shutdown step already ran.
stopping atomic.Bool
stopOnce sync.Once
}
type builder struct {
app *Application
router Router
once *sync.Once
initOnce sync.Once
workers []*workerRegistration
developer string
errorOptions *errors.Options
// cors and accessLog are kept here and registered inside the once-block,
// after the errors options handler, so both observe the Errors
// configuration: the access log above recovery so a panicking handler is
// still recorded, CORS below it so a rejection is both recovered and
// logged. See RouterMiddlewares for the full order.
cors *cors.CORS
accessLog *accesslog.AccessLog
enableCompression bool
}
func (g *builder) Prefix(prefixPath string) CorsBuilder {
g.router = g.app.Router(prefixPath)
return g
}
func (g *builder) NoPrefix() CorsBuilder {
g.router = g.app
return g
}
func (g *builder) AllowOrigin(originLine string) CompressionBuilder {
// Register the CORS middleware.
originLine = strings.ToLower(strings.TrimSpace(originLine))
if originLine == "none" {
return g
}
return g.Cors(cors.New().HandleErrorFunc(errors.FailedPrecondition.Err).AllowOrigin(originLine))
}
func (g *builder) Cors(c *cors.CORS) CompressionBuilder {
if c == nil {
return g
}
g.cors = c
return g
}
func (g *builder) Compression(enableReadWriteCompression bool) RequestLoggingBuilder {
g.enableCompression = enableReadWriteCompression
return g
}
func (g *builder) LogRequests(enableRequestAccessLog bool) HealthBuilder {
if enableRequestAccessLog {
return g.LogRequestsWith(defaultAccessLog())
}
return g
}
func (g *builder) LogRequestsWith(ac *accesslog.AccessLog) HealthBuilder {
if ac != nil {
g.accessLog = ac
g.app.AddCloser(ac.Close)
}
return g
}
// defaultAccessLog returns the framework's tuned access log configuration,
// used by LogRequests(true): JSON lines to access_log.json and stdout.
func defaultAccessLog() *accesslog.AccessLog {
ac := accesslog.File("access_log.json")
// Remove this line to disable logging to console:
ac.AddOutput(os.Stdout)
// The default configuration:
ac.Delim = '|'
ac.TimeFormat = "2006-01-02 15:04:05"
ac.Async = false
ac.IP = true
ac.BytesReceivedBody = true
ac.BytesSentBody = true
ac.BytesReceived = false
ac.BytesSent = false
ac.RequestBody = true
ac.ResponseBody = true
ac.KeepMultiLineError = true
ac.PanicLog = accesslog.LogHandler
// Default line format if formatter is missing:
// Time|Latency|Code|Method|Path|IP|Path Params Query Fields|Bytes Received|Bytes Sent|Request|Response|
//
// Set Custom Formatter:
ac.SetFormatter(&accesslog.JSON{
Indent: " ",
HumanTime: true,
})
return ac
}
func (g *builder) Health(enableHealthEndpoint bool, env, developer string) ServiceBuilder {
g.developer = developer
if enableHealthEndpoint {
g.app.Get("/health", modrevision.New(modrevision.Options{
ServerName: "Iris Server",
Env: env,
Developer: developer,
CloudDetection: true,
}))
}
return g
}
func (g *builder) NoHealth() ServiceBuilder {
return g
}
func (g *builder) Errors(opts *errors.Options) ServiceBuilder {
g.errorOptions = opts
return g
}
func (g *builder) Deferrables(closers ...any) ServiceBuilder {
for _, closer := range closers {
g.app.AddCloser(closer)
}
return g
}
func (g *builder) Services(deps ...any) MiddlewareBuilder {
dependencies := make([]any, 0, len(deps))
for _, d := range deps {
if d == nil {
continue // don't add it as a dependnecy.
}
switch cb := d.(type) {
case func(), func() error:
// A function with nothing to inject is not a service. It used to be
// registered as a shutdown closer, which silently turned a typo'd
// constructor into cleanup that never provided anything.
panic(fmt.Errorf("iris: builder: Services: %T provides no service (nothing injectable is returned); use Deferrables for cleanup or implement iris.Initializer for startup work", d))
case Worker:
// Before the Close cases on purpose: a value which is both a Worker
// and a Closer must be stopped before it is closed, and one closer
// does both, in that order.
dependencies = append(dependencies, cb)
g.registerWorker(cb)
case interface{ Close() }:
dependencies = append(dependencies, cb)
g.app.AddCloser(cb.Close)
case interface{ Close() error }:
dependencies = append(dependencies, cb)
g.app.AddCloser(cb.Close)
case Configurator:
g.app.Configure(cb) // don't add it as a dependency (bad value: function has zero outputs).
default:
dependencies = append(dependencies, d)
}
}
if err := g.router.RegisterDependency(dependencies...); err != nil {
panic(fmt.Errorf("iris: builder: Services: %w", err))
}
return g
}
func (g *builder) RouterMiddlewares(middlewares ...any) MiddlewareBuilder {
g.once.Do(func() { // Register the top-level router middlewares only once, before the API's middlewares.
// Register the rest errors options handler first: everything after it,
// the CORS rejection included, is written through the collector, the
// renderer and the id generator configured by Errors.
if g.errorOptions != nil {
g.app.UseRouter(g.errorOptions.Handler())
}
// The access log goes before recovery, not after it. accesslog.Handler
// does its logging when ctx.Next returns, not in a defer, so it has to
// be the outer one: recovery turns the panic into a 500 and returns
// normally, and the access log resumes and records the request. The
// other way round a panicking handler is logged nowhere and the
// PanicLog option is unreachable.
routerHandlers := g.app.RouterHandlers()
if g.accessLog != nil {
g.router.UseRouter(g.accessLog.Handler)
}
// A user-registered rest/errors.RecoveryHandler ("iris.errors.recover")
// counts as recovery too — don't stack a second recovery middleware on it.
if !routerHandlers.Exists("iris.recovery") && !routerHandlers.Exists("iris.errors.recover") {
// Register the recovery middleware.
g.app.UseRouter(recovery.WithErrorHandler(errors.Internal.LogErr), recovery.Handler)
}
// CORS is below recovery so a panic in its error handler is recovered,
// and below the access log so a rejection is recorded.
if g.cors != nil {
g.router.UseRouter(g.cors.Handler())
}
g.app.UseRouter(func(ctx Context) {
ctx.SetHeader("Server", "Iris")
if dev := g.developer; dev != "" {
ctx.SetHeader("X-Developer", dev)
}
ctx.Next()
})
if g.enableCompression && !routerHandlers.Exists("iris.compression") {
// Safe, not Handler: an unbounded decompressor turns a ~1MB gzip
// body into ~1GB of memory.
g.app.UseRouter(compress.Safe)
}
// The rest errors handler goes last. It renders a failure status that
// nobody gave a body to, so it has to write while the compression writer
// is still on the Context: after compression, or the envelope goes out
// uncompressed under a Content-Encoding header. Everything registered
// after this block (RouterMiddlewares, the API middleware, the routes)
// runs inside it, so a rejection from any of them is rendered too.
g.app.UseRouter(errors.Handler)
// Bind the rate limit exceed handler to the JSON version of errors package if the rate middleware registered.
rate.RegisterExceedHandler(g.router, errors.DefaultRateLimitExceededHandler)
})
for _, handler := range middlewares {
g.router.UseRouter(g.router.HandlerFunc(handler))
}
return g
}
func (g *builder) Middlewares(middlewares ...any) APIBuilder {
// To ensure that the top-level middlewares are registered before the API's middlewares.
g.RouterMiddlewares()