|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package gear |
|
|
|
import ( |
|
"bytes" |
|
"errors" |
|
"net/http" |
|
"net/url" |
|
"regexp" |
|
"strings" |
|
|
|
"github.com/GoAdminGroup/go-admin/adapter" |
|
"github.com/GoAdminGroup/go-admin/context" |
|
"github.com/GoAdminGroup/go-admin/engine" |
|
"github.com/GoAdminGroup/go-admin/modules/config" |
|
"github.com/GoAdminGroup/go-admin/modules/utils" |
|
"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/teambition/gear" |
|
) |
|
|
|
|
|
type Gear struct { |
|
adapter.BaseAdapter |
|
ctx *gear.Context |
|
app *gear.App |
|
router *gear.Router |
|
} |
|
|
|
func init() { |
|
engine.Register(new(Gear)) |
|
} |
|
|
|
|
|
func (gears *Gear) User(ctx interface{}) (models.UserModel, bool) { |
|
return gears.GetUser(ctx, gears) |
|
} |
|
|
|
|
|
func (gears *Gear) Use(app interface{}, plugs []plugins.Plugin) error { |
|
return gears.GetUse(app, plugs, gears) |
|
} |
|
|
|
|
|
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 |
|
} |
|
} |
|
|
|
|
|
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 |
|
} |
|
|
|
|
|
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) |
|
} |
|
|
|
|
|
func (*Gear) Name() string { |
|
return "gear" |
|
} |
|
|
|
|
|
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} |
|
} |
|
|
|
|
|
func (gears *Gear) Redirect() { |
|
gears.ctx.Redirect(config.Url(config.GetLoginUrl())) |
|
} |
|
|
|
|
|
func (gears *Gear) SetContentType() { |
|
gears.ctx.Res.Header().Set("Content-Type", gears.HTMLContentType()) |
|
} |
|
|
|
|
|
func (gears *Gear) Write(body []byte) { |
|
gears.ctx.End(http.StatusOK, body) |
|
} |
|
|
|
|
|
func (gears *Gear) GetCookie() (string, error) { |
|
return gears.ctx.Cookies.Get(gears.CookieKey()) |
|
} |
|
|
|
|
|
func (gears *Gear) Lang() string { |
|
return gears.ctx.Req.URL.Query().Get("__ga_lang") |
|
} |
|
|
|
|
|
func (gears *Gear) Path() string { |
|
return gears.ctx.Req.URL.Path |
|
} |
|
|
|
|
|
func (gears *Gear) Method() string { |
|
return gears.ctx.Req.Method |
|
} |
|
|
|
|
|
func (gears *Gear) FormParam() url.Values { |
|
_ = gears.ctx.Req.ParseMultipartForm(32 << 20) |
|
return gears.ctx.Req.PostForm |
|
} |
|
|
|
|
|
func (gears *Gear) IsPjax() bool { |
|
return gears.ctx.Req.Header.Get(constant.PjaxHeader) == "true" |
|
} |
|
|
|
|
|
func (gears *Gear) Query() url.Values { |
|
return gears.ctx.Req.URL.Query() |
|
} |
|
|
|
|
|
func (gears *Gear) Request() *http.Request { |
|
return gears.ctx.Req |
|
} |