-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine.go
128 lines (103 loc) · 3.25 KB
/
engine.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
package fox
import (
"embed"
"io"
"net/http"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
const (
// DebugMode indicates gin mode is debug.
DebugMode = gin.DebugMode
// ReleaseMode indicates gin mode is release.
ReleaseMode = gin.ReleaseMode
// TestMode indicates gin mode is test.
TestMode = gin.TestMode
)
var foxMode = DebugMode
// SetMode sets gin mode according to input string.
func SetMode(value string) {
gin.SetMode(value)
foxMode = value
}
// Mode returns current fox mode.
func Mode() string {
return foxMode
}
// DefaultWriter is the default io.Writer used by Gin for debug output and
// middleware output like Logger() or Recovery().
// Note that both Logger and Recovery provides custom ways to configure their
// output io.Writer.
// To support coloring in Windows use:
//
// import "github.com/mattn/go-colorable"
// gin.DefaultWriter = colorable.NewColorableStdout()
var DefaultWriter io.Writer = os.Stdout
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
var DefaultErrorWriter io.Writer = os.Stderr
// HandlerFunc is a function that can be registered to a route to handle HTTP
// requests. Like http.HandlerFunc, but has a third parameter for the values of
// wildcards (path variables).
// func(){}
// func(ctx *Context) any { ... }
// func(ctx *Context) (any, err) { ... }
// func(ctx *Context, args *AutoBindingArgType) (any) { ... }
// func(ctx *Context, args *AutoBindingArgType) (any, err) { ... }
type HandlerFunc interface{}
// HandlersChain defines a HandlerFunc slice.
type HandlersChain []HandlerFunc
// Last returns the last handler in the chain. i.e. the last handler is the main one.
func (c HandlersChain) Last() HandlerFunc {
if length := len(c); length > 0 {
return c[length-1]
}
return nil
}
// Engine for server
type Engine struct {
*gin.Engine
RouterGroup
// DefaultRenderErrorStatusCode is the default http status code used for automatic rendering
DefaultRenderErrorStatusCode int
}
// New return engine instance
func New() *Engine {
// Change gin default validator
binding.Validator = new(DefaultValidator)
engine := &Engine{
Engine: gin.New(),
DefaultRenderErrorStatusCode: http.StatusBadRequest,
}
engine.RouterGroup.router = &engine.Engine.RouterGroup
engine.RouterGroup.engine = engine
return engine
}
// Default return an Engine instance with Logger and Recovery middleware already attached
func Default() *Engine {
engine := New()
engine.Use(NewXResponseTimer(), Logger(), gin.Recovery())
return engine
}
// Use middleware
func (engine *Engine) Use(middleware ...HandlerFunc) {
engine.RouterGroup.Use(middleware...)
}
// NotFound adds handlers for NoRoute. It returns a 404 code by default.
func (engine *Engine) NotFound(handlers ...HandlerFunc) {
handlersChain := engine.RouterGroup.handleWrapper(handlers...)
engine.Engine.NoRoute(handlersChain...)
}
// CORS config
func (engine *Engine) CORS(config cors.Config) {
if config.Validate() == nil {
engine.Engine.Use(cors.New(config))
}
}
// RouterConfigFunc engine load router config func
type RouterConfigFunc func(router *Engine, embedFS ...embed.FS)
// Load router config
func (engine *Engine) Load(f RouterConfigFunc, fs ...embed.FS) {
f(engine, fs...)
}