-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
174 lines (148 loc) · 4.61 KB
/
echo.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
// Copyright 2019 GoAdmin Core Team. All rights reserved.
// Use of this source code is governed by a Apache-2.0 style
// license that can be found in the LICENSE file.
package echo
import (
"bytes"
"errors"
"net/http"
"net/url"
"strings"
"github.com/alesjr/go-admin/go-admin/adapter"
"github.com/alesjr/go-admin/go-admin/context"
"github.com/alesjr/go-admin/go-admin/engine"
"github.com/alesjr/go-admin/go-admin/modules/config"
"github.com/alesjr/go-admin/go-admin/plugins"
"github.com/alesjr/go-admin/go-admin/plugins/admin/models"
"github.com/alesjr/go-admin/go-admin/plugins/admin/modules/constant"
"github.com/alesjr/go-admin/go-admin/template/types"
"github.com/labstack/echo/v4"
)
// Echo structure value is an Echo GoAdmin adapter.
type Echo struct {
adapter.BaseAdapter
ctx echo.Context
app *echo.Echo
}
func init() {
engine.Register(new(Echo))
}
// User implements the method Adapter.User.
func (e *Echo) User(ctx interface{}) (models.UserModel, bool) {
return e.GetUser(ctx, e)
}
// Use implements the method Adapter.Use.
func (e *Echo) Use(app interface{}, plugs []plugins.Plugin) error {
return e.GetUse(app, plugs, e)
}
// Content implements the method Adapter.Content.
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
}
}
// SetApp implements the method Adapter.SetApp.
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
}
// AddHandler implements the method Adapter.AddHandler.
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
})
}
// Name implements the method Adapter.Name.
func (*Echo) Name() string {
return "echo"
}
// SetContext implements the method Adapter.SetContext.
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}
}
// Redirect implements the method Adapter.Redirect.
func (e *Echo) Redirect() {
_ = e.ctx.Redirect(http.StatusFound, config.Url(config.GetLoginUrl()))
}
// SetContentType implements the method Adapter.SetContentType.
func (e *Echo) SetContentType() {
e.ctx.Response().Header().Set("Content-Type", e.HTMLContentType())
}
// Write implements the method Adapter.Write.
func (e *Echo) Write(body []byte) {
e.ctx.Response().WriteHeader(http.StatusOK)
_, _ = e.ctx.Response().Write(body)
}
// GetCookie implements the method Adapter.GetCookie.
func (e *Echo) GetCookie() (string, error) {
cookie, err := e.ctx.Cookie(e.CookieKey())
if err != nil {
return "", err
}
return cookie.Value, err
}
// Lang implements the method Adapter.Lang.
func (e *Echo) Lang() string {
return e.ctx.Request().URL.Query().Get("__ga_lang")
}
// Path implements the method Adapter.Path.
func (e *Echo) Path() string {
return e.ctx.Request().URL.Path
}
// Method implements the method Adapter.Method.
func (e *Echo) Method() string {
return e.ctx.Request().Method
}
// FormParam implements the method Adapter.FormParam.
func (e *Echo) FormParam() url.Values {
_ = e.ctx.Request().ParseMultipartForm(32 << 20)
return e.ctx.Request().PostForm
}
// IsPjax implements the method Adapter.IsPjax.
func (e *Echo) IsPjax() bool {
return e.ctx.Request().Header.Get(constant.PjaxHeader) == "true"
}
// Query implements the method Adapter.Query.
func (e *Echo) Query() url.Values {
return e.ctx.Request().URL.Query()
}