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