File size: 5,893 Bytes
530729e |
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
// 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 chi
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"
cfg "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/go-chi/chi"
)
// Chi structure value is a Chi GoAdmin adapter.
type Chi struct {
adapter.BaseAdapter
ctx Context
app *chi.Mux
}
func init() {
engine.Register(new(Chi))
}
// User implements the method Adapter.User.
func (ch *Chi) User(ctx interface{}) (models.UserModel, bool) {
return ch.GetUser(ctx, ch)
}
// Use implements the method Adapter.Use.
func (ch *Chi) Use(app interface{}, plugs []plugins.Plugin) error {
return ch.GetUse(app, plugs, ch)
}
// Content implements the method Adapter.Content.
func (ch *Chi) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn context.NodeProcessor, btns ...types.Button) {
ch.GetContent(ctx, getPanelFn, ch, btns, fn)
}
type HandlerFunc func(ctx Context) (types.Panel, error)
func Content(handler HandlerFunc) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
ctx := Context{
Request: request,
Response: writer,
}
engine.Content(ctx, func(ctx interface{}) (types.Panel, error) {
return handler(ctx.(Context))
})
}
}
// SetApp implements the method Adapter.SetApp.
func (ch *Chi) SetApp(app interface{}) error {
var (
eng *chi.Mux
ok bool
)
if eng, ok = app.(*chi.Mux); !ok {
return errors.New("chi adapter SetApp: wrong parameter")
}
ch.app = eng
return nil
}
// AddHandler implements the method Adapter.AddHandler.
func (ch *Chi) AddHandler(method, path string, handlers context.Handlers) {
url := path
reg1 := regexp.MustCompile(":(.*?)/")
reg2 := regexp.MustCompile(":(.*?)$")
url = reg1.ReplaceAllString(url, "{$1}/")
url = reg2.ReplaceAllString(url, "{$1}")
if len(url) > 1 && url[0] == '/' && url[1] == '/' {
url = url[1:]
}
getHandleFunc(ch.app, strings.ToUpper(method))(url, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path[len(r.URL.Path)-1] == '/' {
r.URL.Path = r.URL.Path[:len(r.URL.Path)-1]
}
ctx := context.NewContext(r)
params := chi.RouteContext(r.Context()).URLParams
for i := 0; i < len(params.Values); i++ {
if r.URL.RawQuery == "" {
r.URL.RawQuery += strings.ReplaceAll(params.Keys[i], ":", "") + "=" + params.Values[i]
} else {
r.URL.RawQuery += "&" + strings.ReplaceAll(params.Keys[i], ":", "") + "=" + params.Values[i]
}
}
ctx.SetHandlers(handlers).Next()
for key, head := range ctx.Response.Header {
w.Header().Set(key, head[0])
}
if ctx.Response.Body != nil {
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(ctx.Response.Body)
w.WriteHeader(ctx.Response.StatusCode)
_, _ = w.Write(buf.Bytes())
} else {
w.WriteHeader(ctx.Response.StatusCode)
}
})
}
// HandleFun is type of route methods of chi.
type HandleFun func(pattern string, handlerFn http.HandlerFunc)
func getHandleFunc(eng *chi.Mux, method string) HandleFun {
switch method {
case "GET":
return eng.Get
case "POST":
return eng.Post
case "PUT":
return eng.Put
case "DELETE":
return eng.Delete
case "HEAD":
return eng.Head
case "OPTIONS":
return eng.Options
case "PATCH":
return eng.Patch
default:
panic("wrong method")
}
}
// Context wraps the Request and Response object of Chi.
type Context struct {
Request *http.Request
Response http.ResponseWriter
}
// SetContext implements the method Adapter.SetContext.
func (*Chi) SetContext(contextInterface interface{}) adapter.WebFrameWork {
var (
ctx Context
ok bool
)
if ctx, ok = contextInterface.(Context); !ok {
panic("chi adapter SetContext: wrong parameter")
}
return &Chi{ctx: ctx}
}
// Name implements the method Adapter.Name.
func (*Chi) Name() string {
return "chi"
}
// Redirect implements the method Adapter.Redirect.
func (ch *Chi) Redirect() {
http.Redirect(ch.ctx.Response, ch.ctx.Request, cfg.Url(cfg.GetLoginUrl()), http.StatusFound)
}
// SetContentType implements the method Adapter.SetContentType.
func (ch *Chi) SetContentType() {
ch.ctx.Response.Header().Set("Content-Type", ch.HTMLContentType())
}
// Write implements the method Adapter.Write.
func (ch *Chi) Write(body []byte) {
ch.ctx.Response.WriteHeader(http.StatusOK)
_, _ = ch.ctx.Response.Write(body)
}
// GetCookie implements the method Adapter.GetCookie.
func (ch *Chi) GetCookie() (string, error) {
cookie, err := ch.ctx.Request.Cookie(ch.CookieKey())
if err != nil {
return "", err
}
return cookie.Value, err
}
// Lang implements the method Adapter.Lang.
func (ch *Chi) Lang() string {
return ch.ctx.Request.URL.Query().Get("__ga_lang")
}
// Path implements the method Adapter.Path.
func (ch *Chi) Path() string {
return ch.ctx.Request.URL.Path
}
// Method implements the method Adapter.Method.
func (ch *Chi) Method() string {
return ch.ctx.Request.Method
}
// FormParam implements the method Adapter.FormParam.
func (ch *Chi) FormParam() url.Values {
_ = ch.ctx.Request.ParseMultipartForm(32 << 20)
return ch.ctx.Request.PostForm
}
// IsPjax implements the method Adapter.IsPjax.
func (ch *Chi) IsPjax() bool {
return ch.ctx.Request.Header.Get(constant.PjaxHeader) == "true"
}
// Query implements the method Adapter.Query.
func (ch *Chi) Query() url.Values {
return ch.ctx.Request.URL.Query()
}
// Request implements the method Adapter.Request.
func (ch *Chi) Request() *http.Request {
return ch.ctx.Request
} |