Skip to content

Commit e031b8d

Browse files
author
cg33
authored
Merge pull request #449 from eavesmy/gear
feat(adapter): add Gear support
2 parents e187dcb + 8df6c91 commit e031b8d

File tree

7 files changed

+415
-98
lines changed

7 files changed

+415
-98
lines changed

adapter/gear/gear.go

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/***
2+
# File Name: ../../adapter/gear/gear.go
3+
# Author: eavesmy
4+
# Email: eavesmy@gmail.com
5+
# Created Time: 2021年06月03日 星期四 19时05分06秒
6+
***/
7+
8+
package gear
9+
10+
import (
11+
"bytes"
12+
"errors"
13+
"net/http"
14+
"net/url"
15+
"strings"
16+
17+
"github.com/GoAdminGroup/go-admin/adapter"
18+
"github.com/GoAdminGroup/go-admin/context"
19+
"github.com/GoAdminGroup/go-admin/engine"
20+
"github.com/GoAdminGroup/go-admin/modules/config"
21+
"github.com/GoAdminGroup/go-admin/plugins"
22+
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
23+
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/constant"
24+
"github.com/GoAdminGroup/go-admin/template/types"
25+
"github.com/teambition/gear"
26+
)
27+
28+
// Gear structure value is a Gin GoAdmin adapter.
29+
type Gear struct {
30+
adapter.BaseAdapter
31+
ctx *gear.Context
32+
app *gear.App
33+
router *gear.Router
34+
}
35+
36+
func init() {
37+
engine.Register(new(Gear))
38+
}
39+
40+
// User implements the method Adapter.User.
41+
func (gears *Gear) User(ctx interface{}) (models.UserModel, bool) {
42+
return gears.GetUser(ctx, gears)
43+
}
44+
45+
// Use implements the method Adapter.Use.
46+
func (gears *Gear) Use(app interface{}, plugs []plugins.Plugin) error {
47+
return gears.GetUse(app, plugs, gears)
48+
}
49+
50+
// Content implements the method Adapter.Content.
51+
func (gears *Gear) Content(ctx interface{}, getPanelFn types.GetPanelFn, fn context.NodeProcessor, btns ...types.Button) {
52+
gears.GetContent(ctx, getPanelFn, gears, btns, fn)
53+
}
54+
55+
type HandlerFunc func(ctx *gear.Context) (types.Panel, error)
56+
57+
func Content(handler HandlerFunc) gear.Middleware {
58+
return func(ctx *gear.Context) error {
59+
engine.Content(ctx, func(ctx interface{}) (types.Panel, error) {
60+
return handler(ctx.(*gear.Context))
61+
})
62+
return nil
63+
}
64+
}
65+
66+
func (gears *Gear) Run() error { panic("not implement") }
67+
func (gears *Gear) DisableLog() { panic("not implement") }
68+
func (gears *Gear) Static(prefix, path string) { panic("not implement") }
69+
70+
// SetApp implements the method Adapter.SetApp.
71+
func (gears *Gear) SetApp(app interface{}) error {
72+
gears.app = app.(*gear.App)
73+
gears.router = gear.NewRouter()
74+
var (
75+
eng *gear.App
76+
ok bool
77+
)
78+
if eng, ok = app.(*gear.App); !ok {
79+
return errors.New("beego adapter SetApp: wrong parameter")
80+
}
81+
gears.app = eng
82+
return nil
83+
}
84+
85+
// AddHandler implements the method Adapter.AddHandler.
86+
func (gears *Gear) AddHandler(method, path string, handlers context.Handlers) {
87+
88+
if gears.router == nil {
89+
gears.router = gear.NewRouter()
90+
}
91+
92+
gears.router.Handle(strings.ToUpper(method), path, func(c *gear.Context) error {
93+
94+
// gears.ctx = c
95+
ctx := context.NewContext(c.Req)
96+
97+
if res, err := c.Any(2); err == nil {
98+
for paramKey, paramValue := range res.(map[string]string) {
99+
if c.Req.URL.RawQuery == "" {
100+
c.Req.URL.RawQuery += strings.ReplaceAll(paramKey, ":", "") + "=" + paramValue
101+
} else {
102+
c.Req.URL.RawQuery += "&" + strings.ReplaceAll(paramKey, ":", "") + "=" + paramValue
103+
}
104+
}
105+
}
106+
107+
ctx.SetHandlers(handlers).Next()
108+
109+
for key, head := range ctx.Response.Header {
110+
c.Res.Header().Add(key, head[0])
111+
}
112+
113+
// fmt.Println("检查头", c.Res.Header(), "\n", ctx.Response.Header)
114+
115+
if ctx.Response.Body != nil {
116+
buf := new(bytes.Buffer)
117+
_, _ = buf.ReadFrom(ctx.Response.Body)
118+
119+
return c.End(ctx.Response.StatusCode, buf.Bytes())
120+
}
121+
122+
return nil
123+
})
124+
125+
gears.app.UseHandler(gears.router)
126+
}
127+
128+
// Name implements the method Adapter.Name.
129+
func (gears *Gear) Name() string {
130+
return "gear"
131+
}
132+
133+
// SetContext implements the method Adapter.SetContext.
134+
func (gears *Gear) SetContext(contextInterface interface{}) adapter.WebFrameWork {
135+
var (
136+
ctx *gear.Context
137+
ok bool
138+
)
139+
140+
if ctx, ok = contextInterface.(*gear.Context); !ok {
141+
panic("gear adapter SetContext: wrong parameter")
142+
}
143+
144+
return &Gear{ctx: ctx}
145+
}
146+
147+
// Redirect implements the method Adapter.Redirect.
148+
func (gears *Gear) Redirect() {
149+
gears.ctx.Redirect(config.Url(config.GetLoginUrl()))
150+
}
151+
152+
// SetContentType implements the method Adapter.SetContentType.
153+
func (gears *Gear) SetContentType() {
154+
gears.ctx.Res.Header().Set("Content-Type", gears.HTMLContentType())
155+
}
156+
157+
// Write implements the method Adapter.Write.
158+
func (gears *Gear) Write(body []byte) {
159+
gears.ctx.End(http.StatusOK, body)
160+
}
161+
162+
// GetCookie implements the method Adapter.GetCookie.
163+
func (gears *Gear) GetCookie() (string, error) {
164+
return gears.ctx.Cookies.Get(gears.CookieKey())
165+
}
166+
167+
// Lang implements the method Adapter.Lang.
168+
func (gears *Gear) Lang() string {
169+
return gears.ctx.Req.URL.Query().Get("__ga_lang")
170+
}
171+
172+
// Path implements the method Adapter.Path.
173+
func (gears *Gear) Path() string {
174+
return gears.ctx.Req.URL.Path
175+
}
176+
177+
// Method implements the method Adapter.Method.
178+
func (gears *Gear) Method() string {
179+
return gears.ctx.Req.Method
180+
}
181+
182+
// FormParam implements the method Adapter.FormParam.
183+
func (gears *Gear) FormParam() url.Values {
184+
_ = gears.ctx.Req.ParseMultipartForm(32 << 20)
185+
return gears.ctx.Req.PostForm
186+
}
187+
188+
// IsPjax implements the method Adapter.IsPjax.
189+
func (gears *Gear) IsPjax() bool {
190+
return gears.ctx.Req.Header.Get(constant.PjaxHeader) == "true"
191+
}
192+
193+
// Query implements the method Adapter.Query.
194+
func (gears *Gear) Query() url.Values {
195+
return gears.ctx.Req.URL.Query()
196+
}

examples/gear/main.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"os/signal"
7+
8+
_ "github.com/GoAdminGroup/go-admin/adapter/gear"
9+
_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql"
10+
_ "github.com/GoAdminGroup/themes/sword"
11+
"github.com/teambition/gear"
12+
13+
"github.com/GoAdminGroup/go-admin/engine"
14+
"github.com/GoAdminGroup/go-admin/examples/datamodel"
15+
"github.com/GoAdminGroup/go-admin/modules/config"
16+
"github.com/GoAdminGroup/go-admin/modules/language"
17+
"github.com/GoAdminGroup/go-admin/plugins/example"
18+
"github.com/GoAdminGroup/go-admin/template"
19+
"github.com/GoAdminGroup/go-admin/template/chartjs"
20+
"github.com/GoAdminGroup/themes/adminlte"
21+
"github.com/teambition/gear/middleware/static"
22+
)
23+
24+
func main() {
25+
26+
app := gear.New()
27+
28+
e := engine.Default()
29+
30+
cfg := config.Config{
31+
Env: config.EnvLocal,
32+
Databases: config.DatabaseList{
33+
"default": {
34+
Host: "127.0.0.1",
35+
Port: "3306",
36+
User: "root",
37+
Pwd: "root",
38+
Name: "godmin",
39+
MaxIdleCon: 50,
40+
MaxOpenCon: 150,
41+
Driver: config.DriverMysql,
42+
43+
//Driver: config.DriverSqlite,
44+
//File: "../datamodel/admin.db",
45+
},
46+
},
47+
Store: config.Store{
48+
Path: "./uploads",
49+
Prefix: "uploads",
50+
},
51+
UrlPrefix: "admin",
52+
Language: language.CN,
53+
IndexUrl: "/",
54+
Debug: true,
55+
AccessAssetsLogOff: true,
56+
Animation: config.PageAnimation{
57+
Type: "fadeInUp",
58+
},
59+
ColorScheme: adminlte.ColorschemeSkinBlack,
60+
BootstrapFilePath: "./../datamodel/bootstrap.go",
61+
}
62+
63+
template.AddComp(chartjs.NewChart())
64+
65+
// customize a plugin
66+
67+
examplePlugin := example.NewExample()
68+
69+
// load from golang.Plugin
70+
//
71+
// examplePlugin := plugins.LoadFromPlugin("../datamodel/example.so")
72+
73+
// customize the login page
74+
// example: https://github.com/GoAdminGroup/demo.go-admin.cn/blob/master/main.go#L39
75+
//
76+
// template.AddComp("login", datamodel.LoginPage)
77+
78+
// load config from json file
79+
//
80+
// e.AddConfigFromJSON("../datamodel/config.json")
81+
82+
app.Use(static.New(static.Options{Root: "./uploads", Prefix: "uploads"}))
83+
84+
if err := e.AddConfig(&cfg).
85+
AddGenerators(datamodel.Generators).
86+
// add generator, first parameter is the url prefix of table when visit.
87+
// example:
88+
//
89+
// "user" => http://localhost:9033/admin/info/user
90+
//
91+
AddGenerator("user", datamodel.GetUserTable).
92+
AddDisplayFilterXssJsFilter().
93+
AddPlugins(examplePlugin).
94+
Use(app); err != nil {
95+
panic(err)
96+
}
97+
98+
// customize your pages
99+
100+
e.HTML("GET", "/admin", datamodel.GetContent)
101+
102+
go func() {
103+
app.Start(":8099")
104+
}()
105+
106+
quit := make(chan os.Signal, 1)
107+
signal.Notify(quit, os.Interrupt)
108+
<-quit
109+
log.Print("closing database connection")
110+
e.MysqlConnection().Close()
111+
}

modules/db/postgresql.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package db
66

77
import (
88
"database/sql"
9+
"fmt"
910
"strconv"
1011
"strings"
1112

@@ -96,6 +97,8 @@ func (db *Postgresql) InitDB(cfgList map[string]config.Database) Connection {
9697
db.Once.Do(func() {
9798
for conn, cfg := range cfgList {
9899

100+
fmt.Println("检查 pg 配置", cfg.GetDSN())
101+
99102
sqlDB, err := sql.Open("postgres", cfg.GetDSN())
100103
if err != nil {
101104
if sqlDB != nil {

modules/db/statement.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ func (sql *SQL) Exec() (int64, error) {
544544
return res.LastInsertId()
545545
}
546546

547-
const postgresInsertCheckTableName = "goadmin_menu|goadmin_permissions|goadmin_roles|goadmin_users|goadmin_user_permissions|goadmin_role_users|goadmin_role_menu|goadmin_role_permissions|goadmin_role_menu"
547+
const postgresInsertCheckTableName = "goadmin_menu|goadmin_permissions|goadmin_roles|goadmin_users"
548548

549549
// Insert exec the insert method of given key/value pairs.
550550
func (sql *SQL) Insert(values dialect.H) (int64, error) {

0 commit comments

Comments
 (0)