|
|
|
|
|
|
|
|
|
package beego |
|
|
|
import ( |
|
"bytes" |
|
"errors" |
|
"net/http" |
|
"net/url" |
|
"strings" |
|
|
|
"github.com/GoAdminGroup/go-admin/adapter" |
|
gctx "github.com/GoAdminGroup/go-admin/context" |
|
"github.com/GoAdminGroup/go-admin/engine" |
|
"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/astaxie/beego" |
|
"github.com/astaxie/beego/context" |
|
) |
|
|
|
|
|
type Beego struct { |
|
adapter.BaseAdapter |
|
ctx *context.Context |
|
app *beego.App |
|
} |
|
|
|
func init() { |
|
engine.Register(new(Beego)) |
|
} |
|
|
|
|
|
func (bee *Beego) User(ctx interface{}) (models.UserModel, bool) { |
|
return bee.GetUser(ctx, bee) |
|
} |
|
|
|
|
|
func (bee *Beego) Use(app interface{}, plugs []plugins.Plugin) error { |
|
return bee.GetUse(app, plugs, bee) |
|
} |
|
|
|
|
|
func (bee *Beego) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn gctx.NodeProcessor, navButtons ...types.Button) { |
|
bee.GetContent(ctx, getPanelFn, bee, navButtons, fn) |
|
} |
|
|
|
type HandlerFunc func(ctx *context.Context) (types.Panel, error) |
|
|
|
func Content(handler HandlerFunc) beego.FilterFunc { |
|
return func(ctx *context.Context) { |
|
engine.Content(ctx, func(ctx interface{}) (types.Panel, error) { |
|
return handler(ctx.(*context.Context)) |
|
}) |
|
} |
|
} |
|
|
|
|
|
func (bee *Beego) SetApp(app interface{}) error { |
|
var ( |
|
eng *beego.App |
|
ok bool |
|
) |
|
if eng, ok = app.(*beego.App); !ok { |
|
return errors.New("beego adapter SetApp: wrong parameter") |
|
} |
|
bee.app = eng |
|
return nil |
|
} |
|
|
|
|
|
func (bee *Beego) AddHandler(method, path string, handlers gctx.Handlers) { |
|
bee.app.Handlers.AddMethod(method, path, func(c *context.Context) { |
|
for key, value := range c.Input.Params() { |
|
if c.Request.URL.RawQuery == "" { |
|
c.Request.URL.RawQuery += strings.ReplaceAll(key, ":", "") + "=" + value |
|
} else { |
|
c.Request.URL.RawQuery += "&" + strings.ReplaceAll(key, ":", "") + "=" + value |
|
} |
|
} |
|
ctx := gctx.NewContext(c.Request) |
|
ctx.SetHandlers(handlers).Next() |
|
for key, head := range ctx.Response.Header { |
|
c.ResponseWriter.Header().Add(key, head[0]) |
|
} |
|
c.ResponseWriter.WriteHeader(ctx.Response.StatusCode) |
|
if ctx.Response.Body != nil { |
|
buf := new(bytes.Buffer) |
|
_, _ = buf.ReadFrom(ctx.Response.Body) |
|
c.WriteString(buf.String()) |
|
} |
|
}) |
|
} |
|
|
|
|
|
func (*Beego) Name() string { |
|
return "beego" |
|
} |
|
|
|
|
|
func (*Beego) SetContext(contextInterface interface{}) adapter.WebFrameWork { |
|
var ( |
|
ctx *context.Context |
|
ok bool |
|
) |
|
if ctx, ok = contextInterface.(*context.Context); !ok { |
|
panic("beego adapter SetContext: wrong parameter") |
|
} |
|
return &Beego{ctx: ctx} |
|
} |
|
|
|
|
|
func (bee *Beego) Redirect() { |
|
bee.ctx.Redirect(http.StatusFound, config.Url(config.GetLoginUrl())) |
|
} |
|
|
|
|
|
func (bee *Beego) SetContentType() { |
|
bee.ctx.ResponseWriter.Header().Set("Content-Type", bee.HTMLContentType()) |
|
} |
|
|
|
|
|
func (bee *Beego) Write(body []byte) { |
|
_, _ = bee.ctx.ResponseWriter.Write(body) |
|
} |
|
|
|
|
|
func (bee *Beego) GetCookie() (string, error) { |
|
return bee.ctx.GetCookie(bee.CookieKey()), nil |
|
} |
|
|
|
|
|
func (bee *Beego) Lang() string { |
|
return bee.ctx.Request.URL.Query().Get("__ga_lang") |
|
} |
|
|
|
|
|
func (bee *Beego) Path() string { |
|
return bee.ctx.Request.URL.Path |
|
} |
|
|
|
|
|
func (bee *Beego) Method() string { |
|
return bee.ctx.Request.Method |
|
} |
|
|
|
|
|
func (bee *Beego) FormParam() url.Values { |
|
_ = bee.ctx.Request.ParseMultipartForm(32 << 20) |
|
return bee.ctx.Request.PostForm |
|
} |
|
|
|
|
|
func (bee *Beego) IsPjax() bool { |
|
return bee.ctx.Request.Header.Get(constant.PjaxHeader) == "true" |
|
} |
|
|
|
|
|
func (bee *Beego) Query() url.Values { |
|
return bee.ctx.Request.URL.Query() |
|
} |
|
|
|
|
|
func (bee *Beego) Request() *http.Request { |
|
return bee.ctx.Request |
|
} |