|
| 1 | +/*** |
| 2 | +# File Name: ../../adapter/gear/gear.go |
| 3 | +# Author: eavesmy |
| 4 | +# Email: eavesmy@gmail.com |
| 5 | +# Created Time: 2021年06月03日 星期四 19时05分06秒 |
| 6 | +***/ |
| 7 | + |
| 8 | +package gear |
| 9 | + |
| 10 | +import ( |
| 11 | + "bytes" |
| 12 | + "errors" |
| 13 | + "net/http" |
| 14 | + "net/url" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/GoAdminGroup/go-admin/adapter" |
| 18 | + "github.com/GoAdminGroup/go-admin/context" |
| 19 | + "github.com/GoAdminGroup/go-admin/engine" |
| 20 | + "github.com/GoAdminGroup/go-admin/modules/config" |
| 21 | + "github.com/GoAdminGroup/go-admin/plugins" |
| 22 | + "github.com/GoAdminGroup/go-admin/plugins/admin/models" |
| 23 | + "github.com/GoAdminGroup/go-admin/plugins/admin/modules/constant" |
| 24 | + "github.com/GoAdminGroup/go-admin/template/types" |
| 25 | + "github.com/teambition/gear" |
| 26 | +) |
| 27 | + |
| 28 | +// Gear structure value is a Gin GoAdmin adapter. |
| 29 | +type Gear struct { |
| 30 | + adapter.BaseAdapter |
| 31 | + ctx *gear.Context |
| 32 | + app *gear.App |
| 33 | + router *gear.Router |
| 34 | +} |
| 35 | + |
| 36 | +func init() { |
| 37 | + engine.Register(new(Gear)) |
| 38 | +} |
| 39 | + |
| 40 | +// User implements the method Adapter.User. |
| 41 | +func (gears *Gear) User(ctx interface{}) (models.UserModel, bool) { |
| 42 | + return gears.GetUser(ctx, gears) |
| 43 | +} |
| 44 | + |
| 45 | +// Use implements the method Adapter.Use. |
| 46 | +func (gears *Gear) Use(app interface{}, plugs []plugins.Plugin) error { |
| 47 | + return gears.GetUse(app, plugs, gears) |
| 48 | +} |
| 49 | + |
| 50 | +// Content implements the method Adapter.Content. |
| 51 | +func (gears *Gear) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn context.NodeProcessor, btns ...types.Button) { |
| 52 | + gears.GetContent(ctx, getPanelFn, gears, btns, fn) |
| 53 | +} |
| 54 | + |
| 55 | +type HandlerFunc func(ctx *gear.Context) (types.Panel, error) |
| 56 | + |
| 57 | +func Content(handler HandlerFunc) gear.Middleware { |
| 58 | + return func(ctx *gear.Context) error { |
| 59 | + engine.Content(ctx, func(ctx interface{}) (types.Panel, error) { |
| 60 | + return handler(ctx.(*gear.Context)) |
| 61 | + }) |
| 62 | + return nil |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (gears *Gear) Run() error { panic("not implement") } |
| 67 | +func (gears *Gear) DisableLog() { panic("not implement") } |
| 68 | +func (gears *Gear) Static(prefix, path string) { panic("not implement") } |
| 69 | + |
| 70 | +// SetApp implements the method Adapter.SetApp. |
| 71 | +func (gears *Gear) SetApp(app interface{}) error { |
| 72 | + gears.app = app.(*gear.App) |
| 73 | + gears.router = gear.NewRouter() |
| 74 | + var ( |
| 75 | + eng *gear.App |
| 76 | + ok bool |
| 77 | + ) |
| 78 | + if eng, ok = app.(*gear.App); !ok { |
| 79 | + return errors.New("beego adapter SetApp: wrong parameter") |
| 80 | + } |
| 81 | + gears.app = eng |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// AddHandler implements the method Adapter.AddHandler. |
| 86 | +func (gears *Gear) AddHandler(method, path string, handlers context.Handlers) { |
| 87 | + |
| 88 | + if gears.router == nil { |
| 89 | + gears.router = gear.NewRouter() |
| 90 | + } |
| 91 | + |
| 92 | + gears.router.Handle(strings.ToUpper(method), path, func(c *gear.Context) error { |
| 93 | + |
| 94 | + // gears.ctx = c |
| 95 | + ctx := context.NewContext(c.Req) |
| 96 | + |
| 97 | + if res, err := c.Any(2); err == nil { |
| 98 | + for paramKey, paramValue := range res.(map[string]string) { |
| 99 | + if c.Req.URL.RawQuery == "" { |
| 100 | + c.Req.URL.RawQuery += strings.ReplaceAll(paramKey, ":", "") + "=" + paramValue |
| 101 | + } else { |
| 102 | + c.Req.URL.RawQuery += "&" + strings.ReplaceAll(paramKey, ":", "") + "=" + paramValue |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + ctx.SetHandlers(handlers).Next() |
| 108 | + |
| 109 | + for key, head := range ctx.Response.Header { |
| 110 | + c.Res.Header().Add(key, head[0]) |
| 111 | + } |
| 112 | + |
| 113 | + // fmt.Println("检查头", c.Res.Header(), "\n", ctx.Response.Header) |
| 114 | + |
| 115 | + if ctx.Response.Body != nil { |
| 116 | + buf := new(bytes.Buffer) |
| 117 | + _, _ = buf.ReadFrom(ctx.Response.Body) |
| 118 | + |
| 119 | + return c.End(ctx.Response.StatusCode, buf.Bytes()) |
| 120 | + } |
| 121 | + |
| 122 | + return nil |
| 123 | + }) |
| 124 | + |
| 125 | + gears.app.UseHandler(gears.router) |
| 126 | +} |
| 127 | + |
| 128 | +// Name implements the method Adapter.Name. |
| 129 | +func (gears *Gear) Name() string { |
| 130 | + return "gear" |
| 131 | +} |
| 132 | + |
| 133 | +// SetContext implements the method Adapter.SetContext. |
| 134 | +func (gears *Gear) SetContext(contextInterface interface{}) adapter.WebFrameWork { |
| 135 | + var ( |
| 136 | + ctx *gear.Context |
| 137 | + ok bool |
| 138 | + ) |
| 139 | + |
| 140 | + if ctx, ok = contextInterface.(*gear.Context); !ok { |
| 141 | + panic("gear adapter SetContext: wrong parameter") |
| 142 | + } |
| 143 | + |
| 144 | + return &Gear{ctx: ctx} |
| 145 | +} |
| 146 | + |
| 147 | +// Redirect implements the method Adapter.Redirect. |
| 148 | +func (gears *Gear) Redirect() { |
| 149 | + gears.ctx.Redirect(config.Url(config.GetLoginUrl())) |
| 150 | +} |
| 151 | + |
| 152 | +// SetContentType implements the method Adapter.SetContentType. |
| 153 | +func (gears *Gear) SetContentType() { |
| 154 | + gears.ctx.Res.Header().Set("Content-Type", gears.HTMLContentType()) |
| 155 | +} |
| 156 | + |
| 157 | +// Write implements the method Adapter.Write. |
| 158 | +func (gears *Gear) Write(body []byte) { |
| 159 | + gears.ctx.End(http.StatusOK, body) |
| 160 | +} |
| 161 | + |
| 162 | +// GetCookie implements the method Adapter.GetCookie. |
| 163 | +func (gears *Gear) GetCookie() (string, error) { |
| 164 | + return gears.ctx.Cookies.Get(gears.CookieKey()) |
| 165 | +} |
| 166 | + |
| 167 | +// Lang implements the method Adapter.Lang. |
| 168 | +func (gears *Gear) Lang() string { |
| 169 | + return gears.ctx.Req.URL.Query().Get("__ga_lang") |
| 170 | +} |
| 171 | + |
| 172 | +// Path implements the method Adapter.Path. |
| 173 | +func (gears *Gear) Path() string { |
| 174 | + return gears.ctx.Req.URL.Path |
| 175 | +} |
| 176 | + |
| 177 | +// Method implements the method Adapter.Method. |
| 178 | +func (gears *Gear) Method() string { |
| 179 | + return gears.ctx.Req.Method |
| 180 | +} |
| 181 | + |
| 182 | +// FormParam implements the method Adapter.FormParam. |
| 183 | +func (gears *Gear) FormParam() url.Values { |
| 184 | + _ = gears.ctx.Req.ParseMultipartForm(32 << 20) |
| 185 | + return gears.ctx.Req.PostForm |
| 186 | +} |
| 187 | + |
| 188 | +// IsPjax implements the method Adapter.IsPjax. |
| 189 | +func (gears *Gear) IsPjax() bool { |
| 190 | + return gears.ctx.Req.Header.Get(constant.PjaxHeader) == "true" |
| 191 | +} |
| 192 | + |
| 193 | +// Query implements the method Adapter.Query. |
| 194 | +func (gears *Gear) Query() url.Values { |
| 195 | + return gears.ctx.Req.URL.Query() |
| 196 | +} |
0 commit comments