forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iris_guide.go
538 lines (437 loc) · 12.8 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
package iris
import (
"time"
"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() Step1 {
return &step1{}
}
type (
Step1 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) Step2
}
Step2 interface {
// Compression enables or disables the gzip (or any other client-preferred) compression algorithm
// for response writes.
Compression(b bool) Step3
}
Step3 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) Step4
}
Step4 interface {
// Timeout defines the http timeout, server read & write timeouts.
Timeout(requestResponseLife, read time.Duration, write time.Duration) Step5
}
Step5 interface {
// RouterMiddlewares registers one or more handlers to run before everything else.
RouterMiddlewares(handlers ...Handler) Step5
// Middlewares registers one or more handlers to run before the requested route's handler.
Middlewares(handlers ...Handler) Step6
}
Step6 interface {
// Services registers one or more dependencies that APIs can use.
Services(deps ...interface{}) Step7
}
Step7 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) Step7
// API registers a router which is responsible to serve the /api group.
API(pathPrefix string, c ...router.PartyConfigurator) Step7
// 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.
// 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.
Run(runner Runner, configurators ...Configurator) error
}
)
type step1 struct {
originLine string
}
func (s *step1) AllowOrigin(originLine string) Step2 {
s.originLine = originLine
return &step2{
step1: s,
}
}
type step2 struct {
step1 *step1
enableCompression bool
}
func (s *step2) Compression(b bool) Step3 {
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) Step4 {
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) Step5 {
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) Step5 {
s.routerMiddlewares = append(s.routerMiddlewares, handlers...)
return s
}
func (s *step5) Middlewares(handlers ...Handler) Step6 {
s.middlewares = handlers
return &step6{
step5: s,
}
}
type step6 struct {
step5 *step5
deps []interface{}
// derives from "deps".
closers []func()
// derives from "deps".
configuratorsAsDeps []Configurator
}
func (s *step6) Services(deps ...interface{}) Step7 {
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) Step7 {
s.handlers = append(s.handlers, step7SimpleRoute{method: method, path: path, handlers: handlers})
return s
}
func (s *step7) API(prefix string, c ...router.PartyConfigurator) Step7 {
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)
app.UseRouter(recover.New())
app.UseRouter(s.step6.step5.routerMiddlewares...)
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; allowOrigin != "" && allowOrigin != "none" {
app.UseRouter(cors.New().AllowOrigin(allowOrigin).Handler())
}
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...)
}
for prefix, c := range s.m {
app.PartyConfigure("/api"+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 {
cb()
}
}()
return app.Run(runner, configurators...)
}