forked from ariandi/gocom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
134 lines (97 loc) · 2.66 KB
/
http.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
package gocom
import (
"sync"
"github.com/frizkey/gocom/config"
)
// ----------------------------------------
var controllerList []Controller = []Controller{}
var app App
var appOnce sync.Once
var appCreatorMap map[string]HttpAppCreatorFunc = map[string]HttpAppCreatorFunc{}
type HttpAppCreatorFunc func() App
func RegAppCreator(name string, creator HttpAppCreatorFunc) {
appCreatorMap[name] = creator
}
// ---------------------------------------
type App interface {
Get(path string, handlers ...HandlerFunc)
Post(path string, handlers ...HandlerFunc)
Put(path string, handlers ...HandlerFunc)
Patch(path string, handlers ...HandlerFunc)
Delete(path string, handlers ...HandlerFunc)
Start()
}
type Context interface {
Status(code int) Context
Body() []byte
Param(key string, defaulVal ...string) string
Query(key string, defaulVal ...string) string
FormValue(key string, defaulVal ...string) string
Bind(target interface{}) error
SetHeader(key string, value string)
GetHeader(key string) string
Set(key string, value string)
Get(key string) string
SendString(data string) error
SendJSON(data interface{}) error
SendPaged(data interface{}, currPage, totalPage int) error
SendFile(filePath string, fileName string) error
SendFileBytes(data []byte, fileName string) error
SendResult(data interface{}) error
SendError(err *CodedError) error
Next() error
InvokeNativeCtx(handlerFunc interface{}) error
}
type Result struct {
Code int `json:"code"`
Messages string `json:"message"`
Data interface{} `json:"data"`
}
type ResultPaged struct {
Result
CurrPage int `json:"currPage"`
TotalPage int `json:"totalPage"`
}
type HandlerFunc func(ctx Context) error
type Controller interface {
Init()
}
// Funcs ----------------------------------------
func init() {
config.SetDefault("app.http.port", 9494)
config.SetDefault("app.http.address", "")
}
func getApp() App {
appOnce.Do(func() {
appType := config.Get("app.http.type", "fiber")
creator := appCreatorMap[appType]
if creator != nil {
app = creator()
}
})
return app
}
func Start() {
for _, ctrl := range controllerList {
ctrl.Init()
}
getApp().Start()
}
func GET(path string, handlers ...HandlerFunc) {
getApp().Get(path, handlers...)
}
func POST(path string, handlers ...HandlerFunc) {
getApp().Post(path, handlers...)
}
func PUT(path string, handlers ...HandlerFunc) {
getApp().Put(path, handlers...)
}
func PATCH(path string, handlers ...HandlerFunc) {
getApp().Patch(path, handlers...)
}
func DELETE(path string, handlers ...HandlerFunc) {
getApp().Delete(path, handlers...)
}
func AddCtrl(ctrl Controller) {
controllerList = append(controllerList, ctrl)
}