forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iris_guide.go
625 lines (514 loc) · 16.2 KB
/
iris_guide.go
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
package iris
import (
"strings"
"time"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/core/router"
"github.com/kataras/iris/v12/middleware/cors"
"github.com/kataras/iris/v12/middleware/modrevision"
"github.com/kataras/iris/v12/middleware/recover"
"github.com/kataras/iris/v12/x/errors"
)
// NewGuide returns a simple Iris API builder.
//
// Example Code:
/*
package main
import (
"context"
"database/sql"
"time"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/x/errors"
)
func main() {
iris.NewGuide().
AllowOrigin("*").
Compression(true).
Health(true, "development", "kataras").
Timeout(0, 20*time.Second, 20*time.Second).
Middlewares().
Services(
// openDatabase(),
// NewSQLRepoRegistry,
NewMemRepoRegistry,
NewTestService,
).
API("/tests", new(TestAPI)).
Listen(":80")
}
// Recommendation: move it to /api/tests/api.go file.
type TestAPI struct {
TestService *TestService
}
func (api *TestAPI) Configure(r iris.Party) {
r.Get("/", api.listTests)
}
func (api *TestAPI) listTests(ctx iris.Context) {
tests, err := api.TestService.ListTests(ctx)
if err != nil {
errors.Internal.LogErr(ctx, err)
return
}
ctx.JSON(tests)
}
// Recommendation: move it to /pkg/storage/sql/db.go file.
type DB struct {
*sql.DB
}
func openDatabase( your database configuration... ) *DB {
conn, err := sql.Open(...)
// handle error.
return &DB{DB: conn}
}
func (db *DB) Close() error {
return nil
}
// Recommendation: move it to /pkg/repository/registry.go file.
type RepoRegistry interface {
Tests() TestRepository
InTransaction(ctx context.Context, fn func(RepoRegistry) error) error
}
// Recommendation: move it to /pkg/repository/registry/memory.go file.
type repoRegistryMem struct {
tests TestRepository
}
func NewMemRepoRegistry() RepoRegistry {
return &repoRegistryMem{
tests: NewMemTestRepository(),
}
}
func (r *repoRegistryMem) Tests() TestRepository {
return r.tests
}
func (r *repoRegistryMem) InTransaction(ctx context.Context, fn func(RepoRegistry) error) error {
return nil
}
// Recommendation: move it to /pkg/repository/registry/sql.go file.
type repoRegistrySQL struct {
db *DB
tests TestRepository
}
func NewSQLRepoRegistry(db *DB) RepoRegistry {
return &repoRegistrySQL{
db: db,
tests: NewSQLTestRepository(db),
}
}
func (r *repoRegistrySQL) Tests() TestRepository {
return r.tests
}
func (r *repoRegistrySQL) InTransaction(ctx context.Context, fn func(RepoRegistry) error) error {
return nil
// your own database transaction code, may look something like that:
// tx, err := r.db.BeginTx(ctx, nil)
// if err != nil {
// return err
// }
// defer tx.Rollback()
// newRegistry := NewSQLRepoRegistry(tx)
// if err := fn(newRegistry);err!=nil{
// return err
// }
// return tx.Commit()
}
// Recommendation: move it to /pkg/test/test.go
type Test struct {
Name string `db:"name"`
}
// Recommendation: move it to /pkg/test/repository.go
type TestRepository interface {
ListTests(ctx context.Context) ([]Test, error)
}
type testRepositoryMem struct {
tests []Test
}
func NewMemTestRepository() TestRepository {
list := []Test{
{Name: "test1"},
{Name: "test2"},
{Name: "test3"},
}
return &testRepositoryMem{
tests: list,
}
}
func (r *testRepositoryMem) ListTests(ctx context.Context) ([]Test, error) {
return r.tests, nil
}
type testRepositorySQL struct {
db *DB
}
func NewSQLTestRepository(db *DB) TestRepository {
return &testRepositorySQL{db: db}
}
func (r *testRepositorySQL) ListTests(ctx context.Context) ([]Test, error) {
query := `SELECT * FROM tests ORDER BY created_at;`
rows, err := r.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
tests := make([]Test, 0)
for rows.Next() {
var t Test
if err := rows.Scan(&t.Name); err != nil {
return nil, err
}
tests = append(tests, t)
}
if err := rows.Err(); err != nil {
return nil, err
}
return tests, nil
}
// Recommendation: move it to /pkg/service/test_service.go file.
type TestService struct {
repos RepoRegistry
}
func NewTestService(registry RepoRegistry) *TestService {
return &TestService{
repos: registry,
}
}
func (s *TestService) ListTests(ctx context.Context) ([]Test, error) {
return s.repos.Tests().ListTests(ctx)
}
*/
func NewGuide() Guide {
return &step1{}
}
type (
// Guide is the simplify API builder.
// It's a step-by-step builder which can be used to build an Iris Application
// with the most common features.
Guide interface {
// AllowOrigin defines the CORS allowed domains.
// Many can be splitted by comma.
// If "*" is provided then all origins are accepted (use it for public APIs).
AllowOrigin(originLine string) CompressionGuide
}
// CompressionGuide is the 2nd step of the Guide.
// Compression (gzip or any other client requested) can be enabled or disabled.
CompressionGuide interface {
// Compression enables or disables the gzip (or any other client-preferred) compression algorithm
// for response writes.
Compression(b bool) HealthGuide
}
// HealthGuide is the 3rd step of the Guide.
// Health enables the /health route.
HealthGuide interface {
// Health enables the /health route.
// If "env" and "developer" are given, these fields will be populated to the client
// through headers and environment on health route.
Health(b bool, env, developer string) TimeoutGuide
}
// TimeoutGuide is the 4th step of the Guide.
// Timeout defines the http timeout, server read & write timeouts.
TimeoutGuide interface {
// Timeout defines the http timeout, server read & write timeouts.
Timeout(requestResponseLife, read time.Duration, write time.Duration) MiddlewareGuide
}
// MiddlewareGuide is the 5th step of the Guide.
// It registers one or more handlers to run before everything else (RouterMiddlewares) or
// before registered routes (Middlewares).
MiddlewareGuide interface {
// RouterMiddlewares registers one or more handlers to run before everything else.
RouterMiddlewares(handlers ...Handler) MiddlewareGuide
// Middlewares registers one or more handlers to run before the requested route's handler.
Middlewares(handlers ...Handler) ServiceGuide
}
// ServiceGuide is the 6th step of the Guide.
// It is used to register deferrable functions and, most importantly, dependencies that APIs can use.
ServiceGuide interface {
// Deferrables registers one or more functions to be ran when the server is terminated.
Deferrables(closers ...func()) ServiceGuide
// Prefix sets the API Party prefix path.
// Usage: WithPrefix("/api").
WithPrefix(prefixPath string) ServiceGuide
// WithoutPrefix disables the API Party prefix path.
// Usage: WithoutPrefix(), same as WithPrefix("").
WithoutPrefix() ServiceGuide
// Services registers one or more dependencies that APIs can use.
Services(deps ...interface{}) ApplicationBuilder
}
// ApplicationBuilder is the final step of the Guide.
// It is used to register APIs controllers (PartyConfigurators) and
// its Build, Listen and Run methods configure and build the actual Iris application
// based on the previous steps.
ApplicationBuilder interface {
// Handle registers a simple route on specific method and (dynamic) path.
// It simply calls the Iris Application's Handle method.
// Use the "API" method instead to keep the app organized.
Handle(method, path string, handlers ...Handler) ApplicationBuilder
// API registers a router which is responsible to serve the /api group.
API(pathPrefix string, c ...router.PartyConfigurator) ApplicationBuilder
// Build builds the application with the prior configuration and returns the
// Iris Application instance for further customizations.
//
// Use "Build" before "Listen" or "Run" to apply further modifications
// to the framework before starting the server. Calling "Build" is optional.
Build() *Application // optional call.
// Listen calls the Application's Listen method which is a shortcut of Run(iris.Addr("hostPort")).
// Use "Run" instead if you need to customize the HTTP/2 server itself.
Listen(hostPort string, configurators ...Configurator) error // Listen OR Run.
// Run calls the Application's Run method.
// The 1st argument is a Runner (iris.Listener, iris.Server, iris.Addr, iris.TLS, iris.AutoTLS and iris.Raw).
// The 2nd argument can be used to add custom configuration right before the server is up and running.
Run(runner Runner, configurators ...Configurator) error
}
)
type step1 struct {
originLine string
}
func (s *step1) AllowOrigin(originLine string) CompressionGuide {
s.originLine = originLine
return &step2{
step1: s,
}
}
type step2 struct {
step1 *step1
enableCompression bool
}
func (s *step2) Compression(b bool) HealthGuide {
s.enableCompression = b
return &step3{
step2: s,
}
}
type step3 struct {
step2 *step2
enableHealth bool
env, developer string
}
func (s *step3) Health(b bool, env, developer string) TimeoutGuide {
s.enableHealth = b
s.env, s.developer = env, developer
return &step4{
step3: s,
}
}
type step4 struct {
step3 *step3
handlerTimeout time.Duration
serverTimeoutRead time.Duration
serverTimeoutWrite time.Duration
}
func (s *step4) Timeout(requestResponseLife, read, write time.Duration) MiddlewareGuide {
s.handlerTimeout = requestResponseLife
s.serverTimeoutRead = read
s.serverTimeoutWrite = write
return &step5{
step4: s,
}
}
type step5 struct {
step4 *step4
routerMiddlewares []Handler // top-level router middlewares, fire even on 404s.
middlewares []Handler
}
func (s *step5) RouterMiddlewares(handlers ...Handler) MiddlewareGuide {
s.routerMiddlewares = append(s.routerMiddlewares, handlers...)
return s
}
func (s *step5) Middlewares(handlers ...Handler) ServiceGuide {
s.middlewares = handlers
return &step6{
step5: s,
prefix: getDefaultAPIPrefix(),
}
}
type step6 struct {
step5 *step5
deps []interface{}
// derives from "deps".
closers []func()
// derives from "deps".
configuratorsAsDeps []Configurator
// API Party optional prefix path.
// If this is nil then it defaults to "/api" in order to keep backwards compatibility,
// otherwise can be set to empty or a custom one.
prefix *string
}
func (s *step6) Deferrables(closers ...func()) ServiceGuide {
s.closers = append(s.closers, closers...)
return s
}
var defaultAPIPrefix = "/api"
func getDefaultAPIPrefix() *string {
return &defaultAPIPrefix
}
// WithPrefix sets the API Party prefix path.
// Usage: WithPrefix("/api").
func (s *step6) WithPrefix(prefixPath string) ServiceGuide {
if prefixPath == "" {
return s.WithoutPrefix()
}
*s.prefix = prefixPath
return s
}
// WithoutPrefix disables the API Party prefix path, same as WithPrefix("").
// Usage: WithoutPrefix()
func (s *step6) WithoutPrefix() ServiceGuide {
s.prefix = nil
return s
}
func (s *step6) getPrefix() string {
if s.prefix == nil { // if WithoutPrefix called then API has no prefix.
return ""
}
apiPrefix := *s.prefix
if apiPrefix == "" { // if not nil but empty (this shouldn't happen) then it defaults to "/api".
apiPrefix = defaultAPIPrefix
}
return apiPrefix
}
func (s *step6) Services(deps ...interface{}) ApplicationBuilder {
s.deps = deps
for _, d := range deps {
if d == nil {
continue
}
switch cb := d.(type) {
case func():
s.closers = append(s.closers, cb)
case func() error:
s.closers = append(s.closers, func() { cb() })
case interface{ Close() }:
s.closers = append(s.closers, cb.Close)
case interface{ Close() error }:
s.closers = append(s.closers, func() {
cb.Close()
})
case Configurator:
s.configuratorsAsDeps = append(s.configuratorsAsDeps, cb)
}
}
return &step7{
step6: s,
}
}
type step7 struct {
step6 *step6
app *Application
m map[string][]router.PartyConfigurator
handlers []step7SimpleRoute
}
type step7SimpleRoute struct {
method, path string
handlers []Handler
}
func (s *step7) Handle(method, path string, handlers ...Handler) ApplicationBuilder {
s.handlers = append(s.handlers, step7SimpleRoute{method: method, path: path, handlers: handlers})
return s
}
func (s *step7) API(prefix string, c ...router.PartyConfigurator) ApplicationBuilder {
if s.m == nil {
s.m = make(map[string][]router.PartyConfigurator)
}
s.m[prefix] = append(s.m[prefix], c...)
return s
}
func (s *step7) Build() *Application {
if s.app != nil {
return s.app
}
app := New()
app.SetContextErrorHandler(errors.DefaultContextErrorHandler)
app.Macros().SetErrorHandler(errors.DefaultPathParameterTypeErrorHandler)
routeFilters := s.step6.step5.routerMiddlewares
if !context.HandlerExists(routeFilters, errors.RecoveryHandler) {
// If not errors.RecoveryHandler registered, then use the default one.
app.UseRouter(recover.New())
}
app.UseRouter(routeFilters...)
app.UseRouter(func(ctx Context) {
ctx.Header("Server", "Iris")
if dev := s.step6.step5.step4.step3.developer; dev != "" {
ctx.Header("X-Developer", dev)
}
ctx.Next()
})
if allowOrigin := s.step6.step5.step4.step3.step2.step1.originLine; strings.TrimSpace(allowOrigin) != "" && allowOrigin != "none" {
corsMiddleware := cors.New().HandleErrorFunc(errors.FailedPrecondition.Err).AllowOrigin(allowOrigin).Handler()
app.UseRouter(corsMiddleware)
}
if s.step6.step5.step4.step3.step2.enableCompression {
app.Use(Compression)
}
for _, middleware := range s.step6.step5.middlewares {
if middleware == nil {
continue
}
app.Use(middleware)
}
if configAsDeps := s.step6.configuratorsAsDeps; len(configAsDeps) > 0 {
app.Configure(configAsDeps...)
}
if s.step6.step5.step4.step3.enableHealth {
app.Get("/health", modrevision.New(modrevision.Options{
ServerName: "Iris Server",
Env: s.step6.step5.step4.step3.env,
Developer: s.step6.step5.step4.step3.developer,
}))
}
if deps := s.step6.deps; len(deps) > 0 {
app.EnsureStaticBindings().RegisterDependency(deps...)
}
apiPrefix := s.step6.getPrefix()
for prefix, c := range s.m {
app.PartyConfigure(apiPrefix+prefix, c...)
}
for _, route := range s.handlers {
app.Handle(route.method, route.path, route.handlers...)
}
if readTimeout := s.step6.step5.step4.serverTimeoutRead; readTimeout > 0 {
app.ConfigureHost(func(su *Supervisor) {
su.Server.ReadTimeout = readTimeout
su.Server.IdleTimeout = readTimeout
if v, recommended := readTimeout/4, 5*time.Second; v > recommended {
su.Server.ReadHeaderTimeout = v
} else {
su.Server.ReadHeaderTimeout = recommended
}
})
}
if writeTimeout := s.step6.step5.step4.serverTimeoutWrite; writeTimeout > 0 {
app.ConfigureHost(func(su *Supervisor) {
su.Server.WriteTimeout = writeTimeout
})
}
var defaultConfigurators = []Configurator{
WithoutServerError(ErrServerClosed, ErrURLQuerySemicolon),
WithOptimizations,
WithRemoteAddrHeader(
"X-Real-Ip",
"X-Forwarded-For",
"CF-Connecting-IP",
"True-Client-Ip",
"X-Appengine-Remote-Addr",
),
WithTimeout(s.step6.step5.step4.handlerTimeout),
}
app.Configure(defaultConfigurators...)
s.app = app
return app
}
func (s *step7) Listen(hostPort string, configurators ...Configurator) error {
return s.Run(Addr(hostPort), configurators...)
}
func (s *step7) Run(runner Runner, configurators ...Configurator) error {
app := s.Build()
defer func() {
// they will be called on interrupt signals too,
// because Iris has a builtin mechanism to call server's shutdown on interrupt.
for _, cb := range s.step6.closers {
if cb == nil {
continue
}
cb()
}
}()
return app.Run(runner, configurators...)
}