File size: 5,303 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 |
// 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 gorilla
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/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/gorilla/mux"
)
// Gorilla structure value is a Gorilla GoAdmin adapter.
type Gorilla struct {
adapter.BaseAdapter
ctx Context
app *mux.Router
}
func init() {
engine.Register(new(Gorilla))
}
// User implements the method Adapter.User.
func (g *Gorilla) User(ctx interface{}) (models.UserModel, bool) {
return g.GetUser(ctx, g)
}
// Use implements the method Adapter.Use.
func (g *Gorilla) Use(app interface{}, plugs []plugins.Plugin) error {
return g.GetUse(app, plugs, g)
}
// Content implements the method Adapter.Content.
func (g *Gorilla) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn context.NodeProcessor, btns ...types.Button) {
g.GetContent(ctx, getPanelFn, g, 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 (g *Gorilla) SetApp(app interface{}) error {
var (
eng *mux.Router
ok bool
)
if eng, ok = app.(*mux.Router); !ok {
return errors.New("gorilla adapter SetApp: wrong parameter")
}
g.app = eng
return nil
}
// AddHandler implements the method Adapter.AddHandler.
func (g *Gorilla) AddHandler(method, path string, handlers context.Handlers) {
reg1 := regexp.MustCompile(":(.*?)/")
reg2 := regexp.MustCompile(":(.*?)$")
u := path
u = reg1.ReplaceAllString(u, "{$1}/")
u = reg2.ReplaceAllString(u, "{$1}")
g.app.HandleFunc(u, func(w http.ResponseWriter, r *http.Request) {
ctx := context.NewContext(r)
params := mux.Vars(r)
for key, param := range params {
if r.URL.RawQuery == "" {
r.URL.RawQuery += strings.ReplaceAll(key, ":", "") + "=" + param
} else {
r.URL.RawQuery += "&" + strings.ReplaceAll(key, ":", "") + "=" + param
}
}
ctx.SetHandlers(handlers).Next()
for key, head := range ctx.Response.Header {
w.Header().Add(key, head[0])
}
if ctx.Response.Body == nil {
w.WriteHeader(ctx.Response.StatusCode)
return
}
w.WriteHeader(ctx.Response.StatusCode)
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(ctx.Response.Body)
_, err := w.Write(buf.Bytes())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}).Methods(strings.ToUpper(method))
}
// Context wraps the Request and Response object of Gorilla.
type Context struct {
Request *http.Request
Response http.ResponseWriter
}
// Name implements the method Adapter.Name.
func (*Gorilla) Name() string {
return "gorilla"
}
// SetContext implements the method Adapter.SetContext.
func (*Gorilla) SetContext(contextInterface interface{}) adapter.WebFrameWork {
var (
ctx Context
ok bool
)
if ctx, ok = contextInterface.(Context); !ok {
panic("gorilla adapter SetContext: wrong parameter")
}
return &Gorilla{ctx: ctx}
}
// Redirect implements the method Adapter.Redirect.
func (g *Gorilla) Redirect() {
http.Redirect(g.ctx.Response, g.ctx.Request, config.Url(config.GetLoginUrl()), http.StatusFound)
}
// SetContentType implements the method Adapter.SetContentType.
func (g *Gorilla) SetContentType() {
g.ctx.Response.Header().Set("Content-Type", g.HTMLContentType())
}
// Write implements the method Adapter.Write.
func (g *Gorilla) Write(body []byte) {
_, _ = g.ctx.Response.Write(body)
}
// GetCookie implements the method Adapter.GetCookie.
func (g *Gorilla) GetCookie() (string, error) {
cookie, err := g.ctx.Request.Cookie(g.CookieKey())
if err != nil {
return "", err
}
return cookie.Value, err
}
// Lang implements the method Adapter.Lang.
func (g *Gorilla) Lang() string {
return g.ctx.Request.URL.Query().Get("__ga_lang")
}
// Path implements the method Adapter.Path.
func (g *Gorilla) Path() string {
return g.ctx.Request.RequestURI
}
// Method implements the method Adapter.Method.
func (g *Gorilla) Method() string {
return g.ctx.Request.Method
}
// FormParam implements the method Adapter.FormParam.
func (g *Gorilla) FormParam() url.Values {
_ = g.ctx.Request.ParseMultipartForm(32 << 20)
return g.ctx.Request.PostForm
}
// IsPjax implements the method Adapter.IsPjax.
func (g *Gorilla) IsPjax() bool {
return g.ctx.Request.Header.Get(constant.PjaxHeader) == "true"
}
// Query implements the method Adapter.Query.
func (g *Gorilla) Query() url.Values {
return g.ctx.Request.URL.Query()
}
// Request implements the method Adapter.Request.
func (g *Gorilla) Request() *http.Request {
return g.ctx.Request
} |