@@ -216,65 +216,87 @@ import (
216
216
return s.repos.Tests().ListTests(ctx)
217
217
}
218
218
*/
219
- func NewGuide () Step1 {
219
+ func NewGuide () Guide {
220
220
return & step1 {}
221
221
}
222
222
223
223
type (
224
- Step1 interface {
224
+ // Guide is the simplify API builder.
225
+ // It's a step-by-step builder which can be used to build an Iris Application
226
+ // with the most common features.
227
+ Guide interface {
225
228
// AllowOrigin defines the CORS allowed domains.
226
229
// Many can be splitted by comma.
227
230
// If "*" is provided then all origins are accepted (use it for public APIs).
228
- AllowOrigin (originLine string ) Step2
231
+ AllowOrigin (originLine string ) CompressionGuide
229
232
}
230
233
231
- Step2 interface {
234
+ // CompressionGuide is the 2nd step of the Guide.
235
+ // Compression (gzip or any other client requested) can be enabled or disabled.
236
+ CompressionGuide interface {
232
237
// Compression enables or disables the gzip (or any other client-preferred) compression algorithm
233
238
// for response writes.
234
- Compression (b bool ) Step3
239
+ Compression (b bool ) HealthGuide
235
240
}
236
241
237
- Step3 interface {
242
+ // HealthGuide is the 3rd step of the Guide.
243
+ // Health enables the /health route.
244
+ HealthGuide interface {
238
245
// Health enables the /health route.
239
246
// If "env" and "developer" are given, these fields will be populated to the client
240
247
// through headers and environment on health route.
241
- Health (b bool , env , developer string ) Step4
248
+ Health (b bool , env , developer string ) TimeoutGuide
242
249
}
243
250
244
- Step4 interface {
251
+ // TimeoutGuide is the 4th step of the Guide.
252
+ // Timeout defines the http timeout, server read & write timeouts.
253
+ TimeoutGuide interface {
245
254
// Timeout defines the http timeout, server read & write timeouts.
246
- Timeout (requestResponseLife , read time.Duration , write time.Duration ) Step5
255
+ Timeout (requestResponseLife , read time.Duration , write time.Duration ) MiddlewareGuide
247
256
}
248
257
249
- Step5 interface {
258
+ // MiddlewareGuide is the 5th step of the Guide.
259
+ // It registers one or more handlers to run before everything else (RouterMiddlewares) or
260
+ // before registered routes (Middlewares).
261
+ MiddlewareGuide interface {
250
262
// RouterMiddlewares registers one or more handlers to run before everything else.
251
- RouterMiddlewares (handlers ... Handler ) Step5
263
+ RouterMiddlewares (handlers ... Handler ) MiddlewareGuide
252
264
// Middlewares registers one or more handlers to run before the requested route's handler.
253
- Middlewares (handlers ... Handler ) Step6
265
+ Middlewares (handlers ... Handler ) ServiceGuide
254
266
}
255
267
256
- Step6 interface {
268
+ // ServiceGuide is the 6th step of the Guide.
269
+ // It is used to register deferrable functions and, most importantly, dependencies that APIs can use.
270
+ ServiceGuide interface {
271
+ // Deferrables registers one or more functions to be ran when the server is terminated.
272
+ Deferrables (closers ... func ()) ServiceGuide
257
273
// Services registers one or more dependencies that APIs can use.
258
- Services (deps ... interface {}) Step7
274
+ Services (deps ... interface {}) ApplicationBuilder
259
275
}
260
276
261
- Step7 interface {
277
+ // ApplicationBuilder is the final step of the Guide.
278
+ // It is used to register APIs controllers (PartyConfigurators) and
279
+ // its Build, Listen and Run methods configure and build the actual Iris application
280
+ // based on the previous steps.
281
+ ApplicationBuilder interface {
262
282
// Handle registers a simple route on specific method and (dynamic) path.
263
283
// It simply calls the Iris Application's Handle method.
264
284
// Use the "API" method instead to keep the app organized.
265
- Handle (method , path string , handlers ... Handler ) Step7
285
+ Handle (method , path string , handlers ... Handler ) ApplicationBuilder
266
286
// API registers a router which is responsible to serve the /api group.
267
- API (pathPrefix string , c ... router.PartyConfigurator ) Step7
287
+ API (pathPrefix string , c ... router.PartyConfigurator ) ApplicationBuilder
268
288
// Build builds the application with the prior configuration and returns the
269
289
// Iris Application instance for further customizations.
270
290
//
271
291
// Use "Build" before "Listen" or "Run" to apply further modifications
272
292
// to the framework before starting the server. Calling "Build" is optional.
273
293
Build () * Application // optional call.
274
- // Listen calls the Application's Listen method.
294
+ // Listen calls the Application's Listen method which is a shortcut of Run(iris.Addr("hostPort")) .
275
295
// Use "Run" instead if you need to customize the HTTP/2 server itself.
276
296
Listen (hostPort string , configurators ... Configurator ) error // Listen OR Run.
277
297
// Run calls the Application's Run method.
298
+ // The 1st argument is a Runner (iris.Listener, iris.Server, iris.Addr, iris.TLS, iris.AutoTLS and iris.Raw).
299
+ // The 2nd argument can be used to add custom configuration right before the server is up and running.
278
300
Run (runner Runner , configurators ... Configurator ) error
279
301
}
280
302
)
@@ -283,7 +305,7 @@ type step1 struct {
283
305
originLine string
284
306
}
285
307
286
- func (s * step1 ) AllowOrigin (originLine string ) Step2 {
308
+ func (s * step1 ) AllowOrigin (originLine string ) CompressionGuide {
287
309
s .originLine = originLine
288
310
return & step2 {
289
311
step1 : s ,
@@ -296,7 +318,7 @@ type step2 struct {
296
318
enableCompression bool
297
319
}
298
320
299
- func (s * step2 ) Compression (b bool ) Step3 {
321
+ func (s * step2 ) Compression (b bool ) HealthGuide {
300
322
s .enableCompression = b
301
323
return & step3 {
302
324
step2 : s ,
@@ -310,7 +332,7 @@ type step3 struct {
310
332
env , developer string
311
333
}
312
334
313
- func (s * step3 ) Health (b bool , env , developer string ) Step4 {
335
+ func (s * step3 ) Health (b bool , env , developer string ) TimeoutGuide {
314
336
s .enableHealth = b
315
337
s .env , s .developer = env , developer
316
338
return & step4 {
@@ -327,7 +349,7 @@ type step4 struct {
327
349
serverTimeoutWrite time.Duration
328
350
}
329
351
330
- func (s * step4 ) Timeout (requestResponseLife , read , write time.Duration ) Step5 {
352
+ func (s * step4 ) Timeout (requestResponseLife , read , write time.Duration ) MiddlewareGuide {
331
353
s .handlerTimeout = requestResponseLife
332
354
333
355
s .serverTimeoutRead = read
@@ -344,12 +366,12 @@ type step5 struct {
344
366
middlewares []Handler
345
367
}
346
368
347
- func (s * step5 ) RouterMiddlewares (handlers ... Handler ) Step5 {
369
+ func (s * step5 ) RouterMiddlewares (handlers ... Handler ) MiddlewareGuide {
348
370
s .routerMiddlewares = append (s .routerMiddlewares , handlers ... )
349
371
return s
350
372
}
351
373
352
- func (s * step5 ) Middlewares (handlers ... Handler ) Step6 {
374
+ func (s * step5 ) Middlewares (handlers ... Handler ) ServiceGuide {
353
375
s .middlewares = handlers
354
376
355
377
return & step6 {
@@ -367,7 +389,12 @@ type step6 struct {
367
389
configuratorsAsDeps []Configurator
368
390
}
369
391
370
- func (s * step6 ) Services (deps ... interface {}) Step7 {
392
+ func (s * step6 ) Deferrables (closers ... func ()) ServiceGuide {
393
+ s .closers = append (s .closers , closers ... )
394
+ return s
395
+ }
396
+
397
+ func (s * step6 ) Services (deps ... interface {}) ApplicationBuilder {
371
398
s .deps = deps
372
399
for _ , d := range deps {
373
400
if d == nil {
@@ -409,12 +436,12 @@ type step7SimpleRoute struct {
409
436
handlers []Handler
410
437
}
411
438
412
- func (s * step7 ) Handle (method , path string , handlers ... Handler ) Step7 {
439
+ func (s * step7 ) Handle (method , path string , handlers ... Handler ) ApplicationBuilder {
413
440
s .handlers = append (s .handlers , step7SimpleRoute {method : method , path : path , handlers : handlers })
414
441
return s
415
442
}
416
443
417
- func (s * step7 ) API (prefix string , c ... router.PartyConfigurator ) Step7 {
444
+ func (s * step7 ) API (prefix string , c ... router.PartyConfigurator ) ApplicationBuilder {
418
445
if s .m == nil {
419
446
s .m = make (map [string ][]router.PartyConfigurator )
420
447
}
0 commit comments