forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gear.go
201 lines (161 loc) · 4.97 KB
/
gear.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
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
194
195
196
197
198
199
200
201
/***
# File Name: ../../adapter/gear/gear.go
# Author: eavesmy
# Email: eavesmy@gmail.com
# Created Time: 2021年06月03日 星期四 19时05分06秒
***/
package gear
import (
"bytes"
"errors"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/dikiharyadi19/go-admin/adapter"
"github.com/dikiharyadi19/go-admin/context"
"github.com/dikiharyadi19/go-admin/engine"
"github.com/dikiharyadi19/go-admin/modules/config"
"github.com/dikiharyadi19/go-admin/modules/utils"
"github.com/dikiharyadi19/go-admin/plugins"
"github.com/dikiharyadi19/go-admin/plugins/admin/models"
"github.com/dikiharyadi19/go-admin/plugins/admin/modules/constant"
"github.com/dikiharyadi19/go-admin/template/types"
"github.com/teambition/gear"
)
// Gear structure value is a Gin GoAdmin adapter.
type Gear struct {
adapter.BaseAdapter
ctx *gear.Context
app *gear.App
router *gear.Router
}
func init() {
engine.Register(new(Gear))
}
// User implements the method Adapter.User.
func (gears *Gear) User(ctx interface{}) (models.UserModel, bool) {
return gears.GetUser(ctx, gears)
}
// Use implements the method Adapter.Use.
func (gears *Gear) Use(app interface{}, plugs []plugins.Plugin) error {
return gears.GetUse(app, plugs, gears)
}
// Content implements the method Adapter.Content.
func (gears *Gear) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn context.NodeProcessor, btns ...types.Button) {
gears.GetContent(ctx, getPanelFn, gears, btns, fn)
}
type HandlerFunc func(ctx *gear.Context) (types.Panel, error)
func Content(handler HandlerFunc) gear.Middleware {
return func(ctx *gear.Context) error {
engine.Content(ctx, func(ctx interface{}) (types.Panel, error) {
return handler(ctx.(*gear.Context))
})
return nil
}
}
// SetApp implements the method Adapter.SetApp.
func (gears *Gear) SetApp(app interface{}) error {
gears.app = app.(*gear.App)
gears.router = gear.NewRouter()
var (
eng *gear.App
ok bool
)
if eng, ok = app.(*gear.App); !ok {
return errors.New("beego adapter SetApp: wrong parameter")
}
gears.app = eng
return nil
}
// AddHandler implements the method Adapter.AddHandler.
func (gears *Gear) AddHandler(method, path string, handlers context.Handlers) {
if gears.router == nil {
gears.router = gear.NewRouter()
}
gears.router.Handle(strings.ToUpper(method), path, func(c *gear.Context) error {
ctx := context.NewContext(c.Req)
newPath := path
reg1 := regexp.MustCompile(":(.*?)/")
reg2 := regexp.MustCompile(":(.*?)$")
params := reg1.FindAllString(newPath, -1)
newPath = reg1.ReplaceAllString(newPath, "")
params = append(params, reg2.FindAllString(newPath, -1)...)
for _, param := range params {
p := utils.ReplaceAll(param, ":", "", "/", "")
if c.Req.URL.RawQuery == "" {
c.Req.URL.RawQuery += p + "=" + c.Param(p)
} else {
c.Req.URL.RawQuery += "&" + p + "=" + c.Param(p)
}
}
ctx.SetHandlers(handlers).Next()
for key, head := range ctx.Response.Header {
c.Res.Header().Add(key, head[0])
}
if ctx.Response.Body != nil {
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(ctx.Response.Body)
return c.End(ctx.Response.StatusCode, buf.Bytes())
}
c.Status(ctx.Response.StatusCode)
return nil
})
gears.app.UseHandler(gears.router)
}
// Name implements the method Adapter.Name.
func (*Gear) Name() string {
return "gear"
}
// SetContext implements the method Adapter.SetContext.
func (*Gear) SetContext(contextInterface interface{}) adapter.WebFrameWork {
var (
ctx *gear.Context
ok bool
)
if ctx, ok = contextInterface.(*gear.Context); !ok {
panic("gear adapter SetContext: wrong parameter")
}
return &Gear{ctx: ctx}
}
// Redirect implements the method Adapter.Redirect.
func (gears *Gear) Redirect() {
gears.ctx.Redirect(config.Url(config.GetLoginUrl()))
}
// SetContentType implements the method Adapter.SetContentType.
func (gears *Gear) SetContentType() {
gears.ctx.Res.Header().Set("Content-Type", gears.HTMLContentType())
}
// Write implements the method Adapter.Write.
func (gears *Gear) Write(body []byte) {
gears.ctx.End(http.StatusOK, body)
}
// GetCookie implements the method Adapter.GetCookie.
func (gears *Gear) GetCookie() (string, error) {
return gears.ctx.Cookies.Get(gears.CookieKey())
}
// Lang implements the method Adapter.Lang.
func (gears *Gear) Lang() string {
return gears.ctx.Req.URL.Query().Get("__ga_lang")
}
// Path implements the method Adapter.Path.
func (gears *Gear) Path() string {
return gears.ctx.Req.URL.Path
}
// Method implements the method Adapter.Method.
func (gears *Gear) Method() string {
return gears.ctx.Req.Method
}
// FormParam implements the method Adapter.FormParam.
func (gears *Gear) FormParam() url.Values {
_ = gears.ctx.Req.ParseMultipartForm(32 << 20)
return gears.ctx.Req.PostForm
}
// IsPjax implements the method Adapter.IsPjax.
func (gears *Gear) IsPjax() bool {
return gears.ctx.Req.Header.Get(constant.PjaxHeader) == "true"
}
// Query implements the method Adapter.Query.
func (gears *Gear) Query() url.Values {
return gears.ctx.Req.URL.Query()
}