-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
path.go
95 lines (72 loc) · 2.75 KB
/
path.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
package atreugo
import (
"time"
"github.com/valyala/fasthttp"
)
// Middlewares defines the middlewares (before, after and skip) in the order in which you want to execute them
// only for the view
//
// WARNING: The previous middlewares configuration could be overridden.
func (p *Path) Middlewares(middlewares Middlewares) *Path {
p.middlewares = middlewares
p.router.handlePath(p)
return p
}
// UseBefore registers the middlewares in the order in which you want to execute them
// only before the execution of the view.
func (p *Path) UseBefore(fns ...Middleware) *Path {
p.middlewares.Before = append(p.middlewares.Before, fns...)
p.router.handlePath(p)
return p
}
// UseAfter registers the middlewares in the order in which you want to execute them
// only after the execution of the view.
func (p *Path) UseAfter(fns ...Middleware) *Path {
p.middlewares.After = append(p.middlewares.After, fns...)
p.router.handlePath(p)
return p
}
// UseFinal registers the given middlewares to be executed in the order in which they are added,
// after the view or group has been executed. These middlewares will always be executed,
// even if a previous middleware or the view/group returned a response.
func (p *Path) UseFinal(fns ...FinalMiddleware) *Path {
p.middlewares.Final = append(p.middlewares.Final, fns...)
p.router.handlePath(p)
return p
}
// SkipMiddlewares registers the middlewares that you want to skip only when executing the view.
func (p *Path) SkipMiddlewares(fns ...Middleware) *Path {
p.middlewares.Skip = append(p.middlewares.Skip, fns...)
p.router.handlePath(p)
return p
}
// Timeout sets the timeout and the error message to the view, which returns StatusRequestTimeout
// error with the given msg to the client if view didn't return during
// the given duration.
//
// The returned view may return StatusTooManyRequests error with the given
// msg to the client if there are more concurrent views are running
// at the moment than Server.Concurrency.
func (p *Path) Timeout(timeout time.Duration, msg string) *Path {
p.withTimeout = true
p.timeout = timeout
p.timeoutMsg = msg
p.timeoutCode = fasthttp.StatusRequestTimeout
p.router.handlePath(p)
return p
}
// TimeoutCode sets the timeout and the error message to the view, which returns an error with
// the given msg and status code to the client if view didn't return during
// the given duration.
//
// The returned view may return StatusTooManyRequests error with the given
// msg to the client if there are more concurrent views are running
// at the moment than Server.Concurrency.
func (p *Path) TimeoutCode(timeout time.Duration, msg string, statusCode int) *Path {
p.withTimeout = true
p.timeout = timeout
p.timeoutMsg = msg
p.timeoutCode = statusCode
p.router.handlePath(p)
return p
}