Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Adapter for GoFiber V2 #357

Merged
merged 12 commits into from
Apr 11, 2021
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func main() {
// add component chartjs
template.AddComp(chartjs.NewChart())

_ = eng.AddConfig(cfg).
_ = eng.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
// add generator, first parameter is the url prefix of table when visit.
// example:
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func main() {
// 增加 chartjs 组件
template.AddComp(chartjs.NewChart())

_ = eng.AddConfig(cfg).
_ = eng.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
// 增加 generator, 第一个参数是对应的访问路由前缀
// 例子:
Expand Down
220 changes: 220 additions & 0 deletions adapter/gofiber/gofiber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// 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 gofiber

import (
"errors"
"io"
"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/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)

// Fasthttp structure value is a Fasthttp GoAdmin adapter.
type Gofiber struct {
adapter.BaseAdapter
ctx *fiber.Ctx
app *fiber.App
}

func init() {
engine.Register(new(Gofiber))
}

// User implements the method Adapter.User.
func (gof *Gofiber) User(ctx interface{}) (models.UserModel, bool) {
return gof.GetUser(ctx, gof)
}

// Use implements the method Adapter.Use.
func (gof *Gofiber) Use(app interface{}, plugs []plugins.Plugin) error {
return gof.GetUse(app, plugs, gof)
}

func (fagof *Gofiber) Run() error { panic("not implement") }
func (gof *Gofiber) DisableLog() { panic("not implement") }
func (gof *Gofiber) Static(prefix, path string) { panic("not implement") }

// Content implements the method Adapter.Content.
func (gof *Gofiber) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn context.NodeProcessor, btns ...types.Button) {
gof.GetContent(ctx, getPanelFn, gof, btns, fn)
}



// SetApp implements the method Adapter.SetApp.
func (gof *Gofiber) SetApp(app interface{}) error {
var (
eng *fiber.App
ok bool
)
if eng, ok = app.(*fiber.App); !ok {
return errors.New("fiber.App adapter SetApp: wrong parameter")
}

gof.app = eng
return nil
}

// AddHandler implements the method Adapter.AddHandler.
func (gof *Gofiber) AddHandler(method, path string, handlers context.Handlers) {

gof.app.Add(strings.ToUpper(method), path,func(c *fiber.Ctx) error {

httpreq := convertCtx(c.Context())
ctx := context.NewContext(httpreq)

for _, key := range c.Route().Params {
if httpreq.URL.RawQuery == "" {
httpreq.URL.RawQuery += strings.ReplaceAll(key, ":", "") + "=" + c.Params(key)
} else {
httpreq.URL.RawQuery += "&" + strings.ReplaceAll(key, ":", "") + "=" + c.Params(key)
}

}

ctx.SetHandlers(handlers).Next()
for key, head := range ctx.Response.Header {
c.Set(key, head[0])

}

return c.Status(ctx.Response.StatusCode).SendStream(ctx.Response.Body)

})

}

func convertCtx(ctx *fasthttp.RequestCtx) *http.Request {
var r http.Request

body := ctx.PostBody()
r.Method = string(ctx.Method())
r.Proto = "HTTP/1.1"
r.ProtoMajor = 1
r.ProtoMinor = 1
r.RequestURI = string(ctx.RequestURI())
r.ContentLength = int64(len(body))
r.Host = string(ctx.Host())
r.RemoteAddr = ctx.RemoteAddr().String()

hdr := make(http.Header)
ctx.Request.Header.VisitAll(func(k, v []byte) {
sk := string(k)
sv := string(v)
switch sk {
case "Transfer-Encoding":
r.TransferEncoding = append(r.TransferEncoding, sv)
default:
hdr.Set(sk, sv)
}
})
r.Header = hdr
r.Body = &netHTTPBody{body}
rURL, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
ctx.Logger().Printf("cannot parse requestURI %q: %s", r.RequestURI, err)
ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError)
return &r
}
r.URL = rURL
return &r
}

type netHTTPBody struct {
b []byte
}

func (r *netHTTPBody) Read(p []byte) (int, error) {
if len(r.b) == 0 {
return 0, io.EOF
}
n := copy(p, r.b)
r.b = r.b[n:]
return n, nil
}

func (r *netHTTPBody) Close() error {
r.b = r.b[:0]
return nil
}



// Name implements the method Adapter.Name.
func (gof *Gofiber) Name() string {
return "gofiber"
}

// SetContext implements the method Adapter.SetContext.
func (gof *Gofiber) SetContext(contextInterface interface{}) adapter.WebFrameWork {
var (
ctx *fiber.Ctx
ok bool
)
if ctx, ok = contextInterface.(*fiber.Ctx); !ok {
panic("gofiber adapter SetContext: wrong parameter")
}
return &Gofiber{ctx: ctx}
}

// Redirect implements the method Adapter.Redirect.
func (gof *Gofiber) Redirect() {
_=gof.ctx.Redirect(config.Url(config.GetLoginUrl()), http.StatusFound)
}

// SetContentType implements the method Adapter.SetContentType.
func (gof *Gofiber) SetContentType() {
gof.ctx.Response().Header.Set("Content-Type", gof.HTMLContentType())
}

// Write implements the method Adapter.Write.
func (gof *Gofiber) Write(body []byte) {
_, _ = gof.ctx.Write(body)
}

// GetCookie implements the method Adapter.GetCookie.
func (gof *Gofiber) GetCookie() (string, error) {
return string(gof.ctx.Cookies(gof.CookieKey())), nil
}

// Lang implements the method Adapter.Lang.
func (gof *Gofiber) Lang() string {
return string(gof.ctx.Request().URI().QueryArgs().Peek("__ga_lang"))
}

// Path implements the method Adapter.Path.
func (gof *Gofiber) Path() string {
return string(gof.ctx.Path())
}

// Method implements the method Adapter.Method.
func (gof *Gofiber) Method() string {
return string(gof.ctx.Method())
}

// FormParam implements the method Adapter.FormParam.
func (gof *Gofiber) FormParam() url.Values {
f, _ := gof.ctx.MultipartForm()
if f != nil {
return f.Value
}
return url.Values{}
}

// IsPjax implements the method Adapter.IsPjax.
func (gof *Gofiber) IsPjax() bool {
return string(gof.ctx.Request().Header.Peek(constant.PjaxHeader)) == "true"
}
4 changes: 2 additions & 2 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func (eng *Engine) announce() *Engine {
}

// AddConfig set the global config.
func (eng *Engine) AddConfig(cfg config.Config) *Engine {
return eng.setConfig(&cfg).announce().initDatabase()
func (eng *Engine) AddConfig(cfg *config.Config) *Engine {
return eng.setConfig(cfg).announce().initDatabase()
}

// setConfig set the config of engine.
Expand Down
2 changes: 1 addition & 1 deletion examples/beego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {

beego.SetStaticPath("/uploads", "uploads")

if err := eng.AddConfig(cfg).
if err := eng.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
AddDisplayFilterXssJsFilter().
// add generator, first parameter is the url prefix of table when visit.
Expand Down
2 changes: 1 addition & 1 deletion examples/buffalo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
//
// eng.AddConfigFromJSON("../datamodel/config.json")

if err := eng.AddConfig(cfg).
if err := eng.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
AddDisplayFilterXssJsFilter().
// add generator, first parameter is the url prefix of table when visit.
Expand Down
2 changes: 1 addition & 1 deletion examples/chi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {
//
// eng.AddConfigFromJSON("../datamodel/config.json")

if err := eng.AddConfig(cfg).
if err := eng.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
AddDisplayFilterXssJsFilter().
// add generator, first parameter is the url prefix of table when visit.
Expand Down
2 changes: 1 addition & 1 deletion examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func main() {
//
// eng.AddConfigFromJSON("../datamodel/config.json")

if err := eng.AddConfig(cfg).
if err := eng.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
AddDisplayFilterXssJsFilter().
// add generator, first parameter is the url prefix of table when visit.
Expand Down
2 changes: 1 addition & 1 deletion examples/fasthttp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func main() {
//
// eng.AddConfigFromJSON("../datamodel/config.json")

if err := eng.AddConfig(cfg).
if err := eng.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
AddDisplayFilterXssJsFilter().
// add generator, first parameter is the url prefix of table when visit.
Expand Down
2 changes: 1 addition & 1 deletion examples/gf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {
//
// eng.AddConfigFromJSON("../datamodel/config.json")

if err := eng.AddConfig(cfg).
if err := eng.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
AddDisplayFilterXssJsFilter().
// add generator, first parameter is the url prefix of table when visit.
Expand Down
2 changes: 1 addition & 1 deletion examples/gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func main() {
//
// e.AddConfigFromJSON("../datamodel/config.json")

if err := e.AddConfig(cfg).
if err := e.AddConfig(&cfg).
AddGenerators(datamodel.Generators).
// add generator, first parameter is the url prefix of table when visit.
// example:
Expand Down
Loading