Skip to content

Commit 88eef93

Browse files
author
cg33
committed
add modules introduction
1 parent c9ad812 commit 88eef93

File tree

4 files changed

+502
-0
lines changed

4 files changed

+502
-0
lines changed

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [Form Components](en/admin/form/components.md)
1414
* [Menus](en/admin/menus.md)
1515
* [Permissions](en/admin/rbac.md)
16+
- [Modules](en/admin/module.md)
1617
* [New Theme](en/admin/theme.md)
1718
* [File Upload](en/admin/file.md)
1819
* [Cli](en/admin/cli.md)

en/admin/module.md

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Modules
2+
---
3+
4+
When GoAdmin engine is building, some modules are built. Such as: database module, user authentication module. And this chapter tells you how to reuse the modules.
5+
6+
## Database Module
7+
8+
Database module can be used afrer the engine set the global config. For example:
9+
10+
```go
11+
12+
import (
13+
...
14+
"github.com/GoAdminGroup/go-admin/engine"
15+
...
16+
)
17+
18+
func main() {
19+
20+
...
21+
22+
eng := engine.Default()
23+
24+
cfg := config.Config{
25+
...
26+
Databases: config.DatabaseList{
27+
"default": {
28+
Host: "127.0.0.1",
29+
Port: "3306",
30+
User: "root",
31+
Pwd: "root",
32+
Name: "godmin",
33+
MaxIdleCon: 50,
34+
MaxOpenCon: 150,
35+
Driver: config.DriverMysql,
36+
},
37+
},
38+
...
39+
}
40+
41+
_ = eng.AddConfig(cfg). // Here the database module built.
42+
AddPlugins(adminPlugin).
43+
Use(r)
44+
45+
// get mysql connection
46+
conn := eng.MysqlConnection()
47+
48+
// get postgresql connection
49+
conn := eng.PostgresqlConnection()
50+
51+
// get sqlite connection
52+
conn := eng.SqliteConnection()
53+
54+
// Note: you get a pointer here which is point to the global database connection object.
55+
// If you want to reuse in you table models file, then you should call before the .Use(r) method.
56+
// Or it will panic. Like:
57+
//
58+
// _ = eng.AddConfig(cfg).
59+
// ResolveMysqlConnection(tables.SetConn)
60+
// AddPlugins(adminPlugin).
61+
// Use(r)
62+
//
63+
// In your tables.go:
64+
//
65+
// var conn db.Connection
66+
//
67+
// func SetConn(c db.Connection) {
68+
// conn = c
69+
// }
70+
//
71+
// And then you call the conn in your table model file.
72+
73+
// get connection by setter function
74+
eng.ResolveMysqlConnection(SetConn)
75+
76+
...
77+
}
78+
79+
var globalConn db.Connection
80+
81+
func SetConn(conn db.Connection) {
82+
globalConn = conn
83+
}
84+
```
85+
86+
When you receive ```connection``` object, you can call the built-in sql helper methods:
87+
88+
```go
89+
90+
import (
91+
...
92+
"github.com/GoAdminGroup/go-admin/modules/db"
93+
"github.com/GoAdminGroup/go-admin/modules/db/dialect"
94+
...
95+
)
96+
97+
func main() {
98+
99+
// query
100+
db.WithDriver(globalConn).Table("users").Select("id", "name").First()
101+
102+
// update
103+
db.WithDriver(globalConn).Table("users").Where("id", "=", 10).
104+
Update(dialect.H{
105+
"name": "jack",
106+
})
107+
108+
// insert
109+
db.WithDriver(globalConn).Table("users").
110+
Insert(dialect.H{
111+
"name": "jack",
112+
})
113+
114+
// delete
115+
db.WithDriver(globalConn).Table("users").Where("id", "=", 10).Delete()
116+
117+
// and so on...
118+
}
119+
120+
```
121+
122+
## User Authentication Module
123+
124+
When we write the page content, need to get the corresponding login user, and to validate its information, need to use user authentication module.
125+
126+
```go
127+
128+
import (
129+
...
130+
adapter "github.com/GoAdminGroup/go-admin/adapter/gin"
131+
"github.com/GoAdminGroup/go-admin/engine"
132+
...
133+
)
134+
135+
func main() {
136+
137+
...
138+
139+
eng := engine.Default()
140+
141+
cfg := config.Config{
142+
...
143+
}
144+
145+
if err := eng.AddConfig(cfg).
146+
AddPlugins(adminPlugin, examplePlugin).
147+
Use(r); err != nil {
148+
panic(err)
149+
}
150+
151+
r.GET("/admin", adapter.Content(func(ctx *gin.Context) (types.Panel, error) {
152+
// get the auth user
153+
user, _ := engine.User(ctx)
154+
155+
// Verify the permissions
156+
if !user.CheckPermission("dashboard") {
157+
return types.Panel{}, errors.New("没有权限")
158+
}
159+
160+
// Verify the roles
161+
if !user.CheckRole("operator") {
162+
return types.Panel{}, errors.New("没有权限")
163+
}
164+
})
165+
...
166+
}
167+
```

fr/admin/module.md

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Modules
2+
---
3+
4+
When GoAdmin engine is building, some modules are built. Such as: database module, user authentication module. And this chapter tells you how to reuse the modules.
5+
6+
## Database Module
7+
8+
Database module can be used afrer the engine set the global config. For example:
9+
10+
```go
11+
12+
import (
13+
...
14+
"github.com/GoAdminGroup/go-admin/engine"
15+
...
16+
)
17+
18+
func main() {
19+
20+
...
21+
22+
eng := engine.Default()
23+
24+
cfg := config.Config{
25+
...
26+
Databases: config.DatabaseList{
27+
"default": {
28+
Host: "127.0.0.1",
29+
Port: "3306",
30+
User: "root",
31+
Pwd: "root",
32+
Name: "godmin",
33+
MaxIdleCon: 50,
34+
MaxOpenCon: 150,
35+
Driver: config.DriverMysql,
36+
},
37+
},
38+
...
39+
}
40+
41+
_ = eng.AddConfig(cfg). // Here the database module built.
42+
AddPlugins(adminPlugin).
43+
Use(r)
44+
45+
// get mysql connection
46+
conn := eng.MysqlConnection()
47+
48+
// get postgresql connection
49+
conn := eng.PostgresqlConnection()
50+
51+
// get sqlite connection
52+
conn := eng.SqliteConnection()
53+
54+
// Note: you get a pointer here which is point to the global database connection object.
55+
// If you want to reuse in you table models file, then you should call before the .Use(r) method.
56+
// Or it will panic. Like:
57+
//
58+
// _ = eng.AddConfig(cfg).
59+
// ResolveMysqlConnection(tables.SetConn)
60+
// AddPlugins(adminPlugin).
61+
// Use(r)
62+
//
63+
// In your tables.go:
64+
//
65+
// var conn db.Connection
66+
//
67+
// func SetConn(c db.Connection) {
68+
// conn = c
69+
// }
70+
//
71+
// And then you call the conn in your table model file.
72+
73+
// get connection by setter function
74+
eng.ResolveMysqlConnection(SetConn)
75+
76+
...
77+
}
78+
79+
var globalConn db.Connection
80+
81+
func SetConn(conn db.Connection) {
82+
globalConn = conn
83+
}
84+
```
85+
86+
When you receive ```connection``` object, you can call the built-in sql helper methods:
87+
88+
```go
89+
90+
import (
91+
...
92+
"github.com/GoAdminGroup/go-admin/modules/db"
93+
"github.com/GoAdminGroup/go-admin/modules/db/dialect"
94+
...
95+
)
96+
97+
func main() {
98+
99+
// query
100+
db.WithDriver(globalConn).Table("users").Select("id", "name").First()
101+
102+
// update
103+
db.WithDriver(globalConn).Table("users").Where("id", "=", 10).
104+
Update(dialect.H{
105+
"name": "jack",
106+
})
107+
108+
// insert
109+
db.WithDriver(globalConn).Table("users").
110+
Insert(dialect.H{
111+
"name": "jack",
112+
})
113+
114+
// delete
115+
db.WithDriver(globalConn).Table("users").Where("id", "=", 10).Delete()
116+
117+
// and so on...
118+
}
119+
120+
```
121+
122+
## User Authentication Module
123+
124+
When we write the page content, need to get the corresponding login user, and to validate its information, need to use user authentication module.
125+
126+
```go
127+
128+
import (
129+
...
130+
adapter "github.com/GoAdminGroup/go-admin/adapter/gin"
131+
"github.com/GoAdminGroup/go-admin/engine"
132+
...
133+
)
134+
135+
func main() {
136+
137+
...
138+
139+
eng := engine.Default()
140+
141+
cfg := config.Config{
142+
...
143+
}
144+
145+
if err := eng.AddConfig(cfg).
146+
AddPlugins(adminPlugin, examplePlugin).
147+
Use(r); err != nil {
148+
panic(err)
149+
}
150+
151+
r.GET("/admin", adapter.Content(func(ctx *gin.Context) (types.Panel, error) {
152+
// get the auth user
153+
user, _ := engine.User(ctx)
154+
155+
// Verify the permissions
156+
if !user.CheckPermission("dashboard") {
157+
return types.Panel{}, errors.New("没有权限")
158+
}
159+
160+
// Verify the roles
161+
if !user.CheckRole("operator") {
162+
return types.Panel{}, errors.New("没有权限")
163+
}
164+
})
165+
...
166+
}
167+
```

0 commit comments

Comments
 (0)