44 lines
753 B
Go
44 lines
753 B
Go
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
|
|
}
|