forked from gin-contrib/timeout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
43 lines (35 loc) · 753 Bytes
/
option.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
package timeout
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
// Option for timeout
type Option func(*Timeout)
// WithTimeout set timeout
func WithTimeout(timeout time.Duration) Option {
return func(t *Timeout) {
t.timeout = timeout
}
}
// WithHandler add gin handler
func WithHandler(h gin.HandlerFunc) Option {
return func(t *Timeout) {
t.handler = h
}
}
// WithResponse add gin handler
func WithResponse(h gin.HandlerFunc) Option {
return func(t *Timeout) {
t.response = h
}
}
func defaultResponse(c *gin.Context) {
c.String(http.StatusRequestTimeout, http.StatusText(http.StatusRequestTimeout))
}
// Timeout struct
type Timeout struct {
timeout time.Duration
handler gin.HandlerFunc
response gin.HandlerFunc
}