forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
deego
authored and
cg33
committed
Aug 22, 2022
1 parent
6b661fa
commit 1c78854
Showing
6 changed files
with
1,733 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package beego2 | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"github.com/GoAdminGroup/go-admin/adapter" | ||
gctx "github.com/GoAdminGroup/go-admin/context" | ||
"github.com/GoAdminGroup/go-admin/engine" | ||
"github.com/GoAdminGroup/go-admin/modules/config" | ||
"github.com/GoAdminGroup/go-admin/modules/constant" | ||
"github.com/GoAdminGroup/go-admin/plugins" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/models" | ||
"github.com/GoAdminGroup/go-admin/template/types" | ||
"github.com/beego/beego/v2/server/web" | ||
"github.com/beego/beego/v2/server/web/context" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type Beego2 struct { | ||
adapter.BaseAdapter | ||
ctx *context.Context | ||
app *web.HttpServer | ||
} | ||
|
||
func init() { | ||
engine.Register(new(Beego2)) | ||
} | ||
|
||
func (bee2 *Beego2) Name() string { | ||
return "beego2" | ||
} | ||
|
||
func (bee2 *Beego2) Use(app interface{}, plugins []plugins.Plugin) error { | ||
return bee2.GetUse(app, plugins, bee2) | ||
} | ||
|
||
func (bee2 *Beego2) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn gctx.NodeProcessor, navButtons ...types.Button) { | ||
bee2.GetContent(ctx, getPanelFn, bee2, navButtons, fn) | ||
} | ||
|
||
func (bee2 *Beego2) User(ctx interface{}) (models.UserModel, bool) { | ||
return bee2.GetUser(ctx, bee2) | ||
} | ||
|
||
func (bee2 *Beego2) AddHandler(method, path string, handlers gctx.Handlers) { | ||
bee2.app.Handlers.AddMethod(method, path, func(c *context.Context) { | ||
for key, value := range c.Input.Params() { | ||
if c.Request.URL.RawQuery == "" { | ||
c.Request.URL.RawQuery += strings.ReplaceAll(key, ":", "") + "=" + value | ||
} else { | ||
c.Request.URL.RawQuery += "&" + strings.ReplaceAll(key, ":", "") + "=" + value | ||
} | ||
} | ||
ctx := gctx.NewContext(c.Request) | ||
ctx.SetHandlers(handlers).Next() | ||
for key, head := range ctx.Response.Header { | ||
c.ResponseWriter.Header().Add(key, head[0]) | ||
} | ||
c.ResponseWriter.WriteHeader(ctx.Response.StatusCode) | ||
if ctx.Response.Body != nil { | ||
buf := new(bytes.Buffer) | ||
_, _ = buf.ReadFrom(ctx.Response.Body) | ||
c.WriteString(buf.String()) | ||
} | ||
}) | ||
} | ||
|
||
func (bee2 *Beego2) DisableLog() { panic("implement me") } | ||
func (bee2 *Beego2) Static(prefix, path string) { panic("implement me") } | ||
func (bee2 *Beego2) Run() error { panic("implement me") } | ||
|
||
func (bee2 *Beego2) SetApp(app interface{}) error { | ||
var ( | ||
eng *web.HttpServer | ||
ok bool | ||
) | ||
if eng, ok = app.(*web.HttpServer); !ok { | ||
return errors.New("beego2 adapter SetApp: wrong parameter") | ||
} | ||
bee2.app = eng | ||
return nil | ||
} | ||
|
||
func (bee2 *Beego2) SetContext(contextInterface interface{}) adapter.WebFrameWork { | ||
var ( | ||
ctx *context.Context | ||
ok bool | ||
) | ||
if ctx, ok = contextInterface.(*context.Context); !ok { | ||
panic("beego2 adapter SetContext: wrong parameter") | ||
} | ||
return &Beego2{ctx: ctx} | ||
} | ||
|
||
func (bee2 *Beego2) GetCookie() (string, error) { | ||
return bee2.ctx.GetCookie(bee2.CookieKey()), nil | ||
} | ||
|
||
func (bee2 *Beego2) Lang() string { | ||
return bee2.ctx.Request.URL.Query().Get("__ga_lang") | ||
} | ||
|
||
func (bee2 *Beego2) Path() string { | ||
return bee2.ctx.Request.URL.Path | ||
} | ||
|
||
func (bee2 *Beego2) Method() string { | ||
return bee2.ctx.Request.Method | ||
} | ||
|
||
func (bee2 *Beego2) FormParam() url.Values { | ||
_ = bee2.ctx.Request.ParseMultipartForm(32 << 20) | ||
return bee2.ctx.Request.PostForm | ||
} | ||
|
||
func (bee2 *Beego2) Query() url.Values { | ||
return bee2.ctx.Request.URL.Query() | ||
} | ||
|
||
func (bee2 *Beego2) IsPjax() bool { | ||
return bee2.ctx.Request.Header.Get(constant.PjaxHeader) == "true" | ||
} | ||
|
||
func (bee2 *Beego2) Redirect() { | ||
bee2.ctx.Redirect(http.StatusFound, config.Url(config.GetLoginUrl())) | ||
} | ||
|
||
func (bee2 *Beego2) SetContentType() { | ||
bee2.ctx.ResponseWriter.Header().Set("Content-Type", bee2.HTMLContentType()) | ||
} | ||
|
||
func (bee2 *Beego2) Write(body []byte) { | ||
_, _ = bee2.ctx.ResponseWriter.Write(body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package gf2 | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"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/modules/constant" | ||
"github.com/GoAdminGroup/go-admin/modules/utils" | ||
"github.com/GoAdminGroup/go-admin/plugins" | ||
"github.com/GoAdminGroup/go-admin/plugins/admin/models" | ||
"github.com/GoAdminGroup/go-admin/template/types" | ||
"github.com/gogf/gf/v2/net/ghttp" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type GF2 struct { | ||
adapter.BaseAdapter | ||
ctx *ghttp.Request | ||
app *ghttp.Server | ||
} | ||
|
||
func init() { | ||
engine.Register(new(GF2)) | ||
} | ||
|
||
func (gf2 *GF2) Name() string { | ||
return "gf2" | ||
} | ||
|
||
func (gf2 *GF2) Use(app interface{}, plugins []plugins.Plugin) error { | ||
return gf2.GetUse(app, plugins, gf2) | ||
} | ||
|
||
func (gf2 *GF2) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn context.NodeProcessor, btns ...types.Button) { | ||
gf2.GetContent(ctx, getPanelFn, gf2, btns, fn) | ||
} | ||
|
||
func (gf2 *GF2) User(ctx interface{}) (models.UserModel, bool) { | ||
return gf2.GetUser(ctx, gf2) | ||
} | ||
|
||
func (gf2 *GF2) AddHandler(method, path string, handlers context.Handlers) { | ||
gf2.app.BindHandler(strings.ToUpper(method)+":"+path, func(c *ghttp.Request) { | ||
ctx := context.NewContext(c.Request) | ||
|
||
newPath := path | ||
|
||
reg1 := regexp.MustCompile(":(.*?)/") | ||
reg2 := regexp.MustCompile(":(.*?)$") | ||
|
||
params := reg1.FindAllString(newPath, -1) | ||
newPath = reg1.ReplaceAllString(newPath, "") | ||
params = append(params, reg2.FindAllString(newPath, -1)...) | ||
|
||
for _, param := range params { | ||
p := utils.ReplaceAll(param, ":", "", "/", "") | ||
|
||
if c.Request.URL.RawQuery == "" { | ||
c.Request.URL.RawQuery += p + "=" + c.GetRequest(p).String() | ||
} else { | ||
c.Request.URL.RawQuery += "&" + p + "=" + c.GetRequest(p).String() | ||
} | ||
} | ||
|
||
ctx.SetHandlers(handlers).Next() | ||
for key, head := range ctx.Response.Header { | ||
c.Response.Header().Add(key, head[0]) | ||
} | ||
|
||
if ctx.Response.Body != nil { | ||
buf := new(bytes.Buffer) | ||
_, _ = buf.ReadFrom(ctx.Response.Body) | ||
c.Response.WriteStatus(ctx.Response.StatusCode, buf.Bytes()) | ||
} else { | ||
c.Response.WriteStatus(ctx.Response.StatusCode) | ||
} | ||
}) | ||
} | ||
|
||
func (gf2 *GF2) DisableLog() { panic("implement me") } | ||
func (gf2 *GF2) Static(prefix, path string) { panic("implement me") } | ||
func (gf2 *GF2) Run() error { panic("implement me") } | ||
|
||
func (gf2 *GF2) SetApp(app interface{}) error { | ||
var ( | ||
eng *ghttp.Server | ||
ok bool | ||
) | ||
if eng, ok = app.(*ghttp.Server); !ok { | ||
return errors.New("gf2 adapter SetApp: wrong parameter") | ||
} | ||
gf2.app = eng | ||
return nil | ||
} | ||
|
||
func (gf2 *GF2) SetContext(contextInterface interface{}) adapter.WebFrameWork { | ||
var ( | ||
ctx *ghttp.Request | ||
ok bool | ||
) | ||
if ctx, ok = contextInterface.(*ghttp.Request); !ok { | ||
panic("gf2 adapter SetContext: wrong parameter") | ||
} | ||
return &GF2{ctx: ctx} | ||
} | ||
|
||
func (gf2 *GF2) GetCookie() (string, error) { | ||
return gf2.ctx.Cookie.Get(gf2.CookieKey()).String(), nil | ||
} | ||
|
||
func (gf2 *GF2) Lang() string { | ||
return gf2.ctx.Request.URL.Query().Get("__ga_lang") | ||
} | ||
|
||
func (gf2 *GF2) Path() string { | ||
return gf2.ctx.URL.Path | ||
} | ||
|
||
func (gf2 *GF2) Method() string { | ||
return gf2.ctx.Method | ||
} | ||
|
||
func (gf2 *GF2) FormParam() url.Values { | ||
return gf2.ctx.Form | ||
} | ||
|
||
func (gf2 *GF2) Query() url.Values { | ||
return gf2.ctx.Request.URL.Query() | ||
} | ||
|
||
func (gf2 *GF2) IsPjax() bool { | ||
return gf2.ctx.Header.Get(constant.PjaxHeader) == "true" | ||
} | ||
|
||
func (gf2 *GF2) Redirect() { | ||
gf2.ctx.Response.RedirectTo(config.Url(config.GetLoginUrl())) | ||
} | ||
|
||
func (gf2 *GF2) SetContentType() { | ||
gf2.ctx.Response.Header().Add("Content-Type", gf2.HTMLContentType()) | ||
} | ||
|
||
func (gf2 *GF2) Write(body []byte) { | ||
gf2.ctx.Response.WriteStatus(http.StatusOK, body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
_ "github.com/GoAdminGroup/go-admin/adapter/beego2" | ||
_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql" | ||
|
||
"github.com/GoAdminGroup/go-admin/engine" | ||
"github.com/GoAdminGroup/go-admin/examples/datamodel" | ||
"github.com/GoAdminGroup/go-admin/modules/config" | ||
"github.com/GoAdminGroup/go-admin/modules/language" | ||
"github.com/GoAdminGroup/go-admin/plugins/example" | ||
"github.com/GoAdminGroup/go-admin/template" | ||
"github.com/GoAdminGroup/go-admin/template/chartjs" | ||
"github.com/GoAdminGroup/themes/adminlte" | ||
"github.com/beego/beego/v2/server/web" | ||
"log" | ||
"os" | ||
"os/signal" | ||
) | ||
|
||
func main() { | ||
app := web.NewHttpSever() | ||
|
||
eng := engine.Default() | ||
|
||
cfg := config.Config{ | ||
Env: config.EnvLocal, | ||
Databases: config.DatabaseList{ | ||
"default": { | ||
Host: "127.0.0.1", | ||
Port: "3306", | ||
User: "root", | ||
Pwd: "123456", | ||
Name: "godmin", | ||
MaxIdleCon: 50, | ||
MaxOpenCon: 150, | ||
Driver: config.DriverMysql, | ||
}, | ||
}, | ||
Store: config.Store{ | ||
Path: "./uploads", | ||
Prefix: "uploads", | ||
}, | ||
UrlPrefix: "admin", | ||
IndexUrl: "/", | ||
Debug: true, | ||
Language: language.CN, | ||
ColorScheme: adminlte.ColorschemeSkinBlack, | ||
} | ||
|
||
template.AddComp(chartjs.NewChart()) | ||
|
||
examplePlugin := example.NewExample() | ||
|
||
web.SetStaticPath("/uploads", "uploads") | ||
|
||
if err := eng.AddConfig(&cfg). | ||
AddGenerators(datamodel.Generators). | ||
AddDisplayFilterXssJsFilter(). | ||
AddGenerator("user", datamodel.GetUserTable). | ||
AddPlugins(examplePlugin). | ||
Use(app); err != nil { | ||
panic(err) | ||
} | ||
|
||
eng.HTML("GET", "/admin", datamodel.GetContent) | ||
|
||
app.Cfg.Listen.HTTPSAddr = "127.0.0.1" | ||
app.Cfg.Listen.HTTPPort = 9087 | ||
go app.Run("") | ||
|
||
quit := make(chan os.Signal, 1) | ||
signal.Notify(quit, os.Interrupt) | ||
<-quit | ||
log.Print("closing database connection") | ||
eng.MysqlConnection().Close() | ||
} |
Oops, something went wrong.