Skip to content

Commit 2e9745a

Browse files
committed
add some comments to iris.NewGuide and update httpexpect module
1 parent b30cbdb commit 2e9745a

File tree

3 files changed

+68
-32
lines changed

3 files changed

+68
-32
lines changed

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12
1919
github.com/google/uuid v1.3.0
2020
github.com/gorilla/securecookie v1.1.1
21-
github.com/iris-contrib/httpexpect/v2 v2.12.1
21+
github.com/iris-contrib/httpexpect/v2 v2.15.1
2222
github.com/iris-contrib/schema v0.0.6
2323
github.com/json-iterator/go v1.1.12
2424
github.com/kataras/blocks v0.0.7
@@ -60,7 +60,9 @@ require (
6060
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
6161
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
6262
github.com/dustin/go-humanize v1.0.1 // indirect
63+
github.com/fatih/color v1.15.0 // indirect
6364
github.com/go-ole/go-ole v1.2.6 // indirect
65+
github.com/gobwas/glob v0.2.3 // indirect
6466
github.com/gobwas/httphead v0.1.0 // indirect
6567
github.com/gobwas/pool v0.2.1 // indirect
6668
github.com/gobwas/ws v1.3.0 // indirect
@@ -72,6 +74,7 @@ require (
7274
github.com/iris-contrib/go.uuid v2.0.0+incompatible // indirect
7375
github.com/josharian/intern v1.0.0 // indirect
7476
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
77+
github.com/mattn/go-colorable v0.1.13 // indirect
7578
github.com/mattn/go-isatty v0.0.19 // indirect
7679
github.com/mediocregopher/radix/v3 v3.8.1 // indirect
7780
github.com/minio/highwayhash v1.0.2 // indirect

go.sum

+10-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iris_guide.go

+54-27
Original file line numberDiff line numberDiff line change
@@ -216,65 +216,87 @@ import (
216216
return s.repos.Tests().ListTests(ctx)
217217
}
218218
*/
219-
func NewGuide() Step1 {
219+
func NewGuide() Guide {
220220
return &step1{}
221221
}
222222

223223
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 {
225228
// AllowOrigin defines the CORS allowed domains.
226229
// Many can be splitted by comma.
227230
// If "*" is provided then all origins are accepted (use it for public APIs).
228-
AllowOrigin(originLine string) Step2
231+
AllowOrigin(originLine string) CompressionGuide
229232
}
230233

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 {
232237
// Compression enables or disables the gzip (or any other client-preferred) compression algorithm
233238
// for response writes.
234-
Compression(b bool) Step3
239+
Compression(b bool) HealthGuide
235240
}
236241

237-
Step3 interface {
242+
// HealthGuide is the 3rd step of the Guide.
243+
// Health enables the /health route.
244+
HealthGuide interface {
238245
// Health enables the /health route.
239246
// If "env" and "developer" are given, these fields will be populated to the client
240247
// through headers and environment on health route.
241-
Health(b bool, env, developer string) Step4
248+
Health(b bool, env, developer string) TimeoutGuide
242249
}
243250

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 {
245254
// 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
247256
}
248257

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 {
250262
// RouterMiddlewares registers one or more handlers to run before everything else.
251-
RouterMiddlewares(handlers ...Handler) Step5
263+
RouterMiddlewares(handlers ...Handler) MiddlewareGuide
252264
// Middlewares registers one or more handlers to run before the requested route's handler.
253-
Middlewares(handlers ...Handler) Step6
265+
Middlewares(handlers ...Handler) ServiceGuide
254266
}
255267

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
257273
// Services registers one or more dependencies that APIs can use.
258-
Services(deps ...interface{}) Step7
274+
Services(deps ...interface{}) ApplicationBuilder
259275
}
260276

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 {
262282
// Handle registers a simple route on specific method and (dynamic) path.
263283
// It simply calls the Iris Application's Handle method.
264284
// 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
266286
// 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
268288
// Build builds the application with the prior configuration and returns the
269289
// Iris Application instance for further customizations.
270290
//
271291
// Use "Build" before "Listen" or "Run" to apply further modifications
272292
// to the framework before starting the server. Calling "Build" is optional.
273293
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")).
275295
// Use "Run" instead if you need to customize the HTTP/2 server itself.
276296
Listen(hostPort string, configurators ...Configurator) error // Listen OR Run.
277297
// 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.
278300
Run(runner Runner, configurators ...Configurator) error
279301
}
280302
)
@@ -283,7 +305,7 @@ type step1 struct {
283305
originLine string
284306
}
285307

286-
func (s *step1) AllowOrigin(originLine string) Step2 {
308+
func (s *step1) AllowOrigin(originLine string) CompressionGuide {
287309
s.originLine = originLine
288310
return &step2{
289311
step1: s,
@@ -296,7 +318,7 @@ type step2 struct {
296318
enableCompression bool
297319
}
298320

299-
func (s *step2) Compression(b bool) Step3 {
321+
func (s *step2) Compression(b bool) HealthGuide {
300322
s.enableCompression = b
301323
return &step3{
302324
step2: s,
@@ -310,7 +332,7 @@ type step3 struct {
310332
env, developer string
311333
}
312334

313-
func (s *step3) Health(b bool, env, developer string) Step4 {
335+
func (s *step3) Health(b bool, env, developer string) TimeoutGuide {
314336
s.enableHealth = b
315337
s.env, s.developer = env, developer
316338
return &step4{
@@ -327,7 +349,7 @@ type step4 struct {
327349
serverTimeoutWrite time.Duration
328350
}
329351

330-
func (s *step4) Timeout(requestResponseLife, read, write time.Duration) Step5 {
352+
func (s *step4) Timeout(requestResponseLife, read, write time.Duration) MiddlewareGuide {
331353
s.handlerTimeout = requestResponseLife
332354

333355
s.serverTimeoutRead = read
@@ -344,12 +366,12 @@ type step5 struct {
344366
middlewares []Handler
345367
}
346368

347-
func (s *step5) RouterMiddlewares(handlers ...Handler) Step5 {
369+
func (s *step5) RouterMiddlewares(handlers ...Handler) MiddlewareGuide {
348370
s.routerMiddlewares = append(s.routerMiddlewares, handlers...)
349371
return s
350372
}
351373

352-
func (s *step5) Middlewares(handlers ...Handler) Step6 {
374+
func (s *step5) Middlewares(handlers ...Handler) ServiceGuide {
353375
s.middlewares = handlers
354376

355377
return &step6{
@@ -367,7 +389,12 @@ type step6 struct {
367389
configuratorsAsDeps []Configurator
368390
}
369391

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 {
371398
s.deps = deps
372399
for _, d := range deps {
373400
if d == nil {
@@ -409,12 +436,12 @@ type step7SimpleRoute struct {
409436
handlers []Handler
410437
}
411438

412-
func (s *step7) Handle(method, path string, handlers ...Handler) Step7 {
439+
func (s *step7) Handle(method, path string, handlers ...Handler) ApplicationBuilder {
413440
s.handlers = append(s.handlers, step7SimpleRoute{method: method, path: path, handlers: handlers})
414441
return s
415442
}
416443

417-
func (s *step7) API(prefix string, c ...router.PartyConfigurator) Step7 {
444+
func (s *step7) API(prefix string, c ...router.PartyConfigurator) ApplicationBuilder {
418445
if s.m == nil {
419446
s.m = make(map[string][]router.PartyConfigurator)
420447
}

0 commit comments

Comments
 (0)