-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathchi.go
More file actions
193 lines (163 loc) · 4.48 KB
/
chi.go
File metadata and controls
193 lines (163 loc) · 4.48 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2019 GoAdmin Core Team. All rights reserved.
// Use of this source code is governed by a Apache-2.0 style
// license that can be found in the LICENSE file.
package chi
import (
"bytes"
"errors"
"github.com/GoAdminGroup/go-admin/adapter"
"github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/engine"
cfg "github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/plugins"
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/constant"
"github.com/GoAdminGroup/go-admin/template/types"
"github.com/go-chi/chi"
"net/http"
"regexp"
"strings"
)
// Chi structure value is a Chi GoAdmin adapter.
type Chi struct {
adapter.BaseAdapter
ctx Context
app *chi.Mux
}
func init() {
engine.Register(new(Chi))
}
func (ch *Chi) User(ci interface{}) (models.UserModel, bool) {
return ch.GetUser(ci, ch)
}
func (ch *Chi) Use(router interface{}, plugs []plugins.Plugin) error {
return ch.GetUse(router, plugs, ch)
}
func (ch *Chi) Content(ctx interface{}, getPanelFn types.GetPanelFn) {
ch.GetContent(ctx, getPanelFn, ch)
}
type HandlerFunc func(ctx Context) (types.Panel, error)
func Content(handler HandlerFunc) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
ctx := Context{
Request: request,
Response: writer,
}
engine.Content(ctx, func(ctx interface{}) (types.Panel, error) {
return handler(ctx.(Context))
})
}
}
func (ch *Chi) SetApp(app interface{}) error {
var (
eng *chi.Mux
ok bool
)
if eng, ok = app.(*chi.Mux); !ok {
return errors.New("wrong parameter")
}
ch.app = eng
return nil
}
func (ch *Chi) AddHandler(method, path string, plug plugins.Plugin) {
url := path
reg1 := regexp.MustCompile(":(.*?)/")
reg2 := regexp.MustCompile(":(.*?)$")
url = reg1.ReplaceAllString(url, "{$1}/")
url = reg2.ReplaceAllString(url, "{$1}")
if len(url) > 1 && url[0] == '/' && url[1] == '/' {
url = url[1:]
}
getHandleFunc(ch.app, strings.ToUpper(method))(url, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path[len(r.URL.Path)-1] == '/' {
r.URL.Path = r.URL.Path[:len(r.URL.Path)-1]
}
ctx := context.NewContext(r)
params := chi.RouteContext(r.Context()).URLParams
for i := 0; i < len(params.Values); i++ {
if r.URL.RawQuery == "" {
r.URL.RawQuery += strings.Replace(params.Keys[i], ":", "", -1) + "=" + params.Values[i]
} else {
r.URL.RawQuery += "&" + strings.Replace(params.Keys[i], ":", "", -1) + "=" + params.Values[i]
}
}
ctx.SetHandlers(plug.GetHandler(r.URL.Path, strings.ToLower(r.Method))).Next()
for key, head := range ctx.Response.Header {
w.Header().Set(key, head[0])
}
if ctx.Response.Body != nil {
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(ctx.Response.Body)
w.WriteHeader(ctx.Response.StatusCode)
_, _ = w.Write(buf.Bytes())
} else {
w.WriteHeader(ctx.Response.StatusCode)
}
})
}
// HandleFun is type of route methods of chi.
type HandleFun func(pattern string, handlerFn http.HandlerFunc)
func getHandleFunc(eng *chi.Mux, method string) HandleFun {
switch method {
case "GET":
return eng.Get
case "POST":
return eng.Post
case "PUT":
return eng.Put
case "DELETE":
return eng.Delete
case "HEAD":
return eng.Head
case "OPTIONS":
return eng.Options
case "PATCH":
return eng.Patch
default:
panic("wrong method")
}
}
// Context wraps the Request and Response object of Chi.
type Context struct {
Request *http.Request
Response http.ResponseWriter
}
func (ch *Chi) SetContext(contextInterface interface{}) adapter.WebFrameWork {
var (
ctx Context
ok bool
)
if ctx, ok = contextInterface.(Context); !ok {
panic("wrong parameter")
}
return &Chi{ctx: ctx}
}
func (ch *Chi) Name() string {
return "chi"
}
func (ch *Chi) Redirect() {
http.Redirect(ch.ctx.Response, ch.ctx.Request, cfg.Get().Url("/login"), http.StatusFound)
}
func (ch *Chi) SetContentType() {
ch.ctx.Response.Header().Set("Content-Type", ch.HTMLContentType())
}
func (ch *Chi) Write(body []byte) {
ch.ctx.Response.WriteHeader(http.StatusOK)
_, _ = ch.ctx.Response.Write(body)
}
func (ch *Chi) GetCookie() (string, error) {
cookie, err := ch.ctx.Request.Cookie(ch.CookieKey())
if err != nil {
return "", err
}
return cookie.Value, err
}
func (ch *Chi) Path() string {
return ch.ctx.Request.URL.Path
}
func (ch *Chi) Method() string {
return ch.ctx.Request.Method
}
func (ch *Chi) PjaxHeader() string {
return ch.ctx.Request.Header.Get(constant.PjaxHeader)
}