Skip to content

Commit 6ff7787

Browse files
committed
refactor: add database constants and refactor sys_menu and sys_user_menu
- Add database constants (MYSQL, POSTGRES, SQLITE) in pkg/gormx/db.go - Refactor sys_menu and sys_user_menu initialization in repository- Update config and user API structures with comments - Simplify log level setting in pkg/log/log.go
1 parent 741d5eb commit 6ff7787

File tree

7 files changed

+119
-113
lines changed

7 files changed

+119
-113
lines changed

api/v1/config.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@ package v1
88
import "mime/multipart"
99

1010
type ConfigResp struct {
11-
ID int `json:"id"`
12-
AboutSite string `json:"about_site"`
13-
AboutAuthor string `json:"about_author"`
14-
IsAbout bool `json:"is_about"`
15-
SiteTitle string `json:"site_title"`
16-
SiteKeyword string `json:"site_keyword"`
17-
SiteDesc string `json:"site_desc"`
18-
SiteRecord string `json:"site_record"`
19-
SiteLogo string `json:"site_logo"`
20-
SiteFavicon string `json:"site_favicon"`
11+
ID int `json:"id"` // 主键ID
12+
AboutSite string `json:"about_site"` // 关于网站的信息
13+
AboutAuthor string `json:"about_author"` // 关于作者的信息
14+
IsAbout bool `json:"is_about"` // 是否显示关于信息
15+
SiteTitle string `json:"site_title"` // 网站标题
16+
SiteKeyword string `json:"site_keyword"` // 网站关键词
17+
SiteDesc string `json:"site_desc"` // 网站描述
18+
SiteRecord string `json:"site_record"` // 网站备案号
19+
SiteLogo string `json:"site_logo"` // 网站Logo URL
20+
SiteFavicon string `json:"site_favicon"` // 网站Favicon URL
2121
}
2222

2323
type (
2424
ConfigUpdateReq struct {
25-
AboutSite *string `json:"about_site" form:"about_site" `
26-
AboutAuthor *string `json:"about_author" form:"about_author"`
27-
IsAbout *bool `json:"is_about" form:"is_about"`
28-
SiteTitle *string `json:"site_title" form:"site_title"`
29-
SiteKeyword *string `json:"site_keyword" form:"site_keyword"`
30-
SiteDesc *string `json:"site_desc" form:"site_desc"`
31-
SiteRecord *string `json:"site_record" form:"site_record"`
32-
LogFile *multipart.FileHeader `json:"log"` // 上传 logo 图片
33-
FaviconFile *multipart.FileHeader `json:"favicon"` // 上传 favicon 图片
25+
AboutSite *string `json:"about_site" form:"about_site"` // 关于网站的信息
26+
AboutAuthor *string `json:"about_author" form:"about_author"` // 关于作者的信息
27+
IsAbout *bool `json:"is_about" form:"is_about"` // 是否显示关于信息
28+
SiteTitle *string `json:"site_title" form:"site_title"` // 网站标题
29+
SiteKeyword *string `json:"site_keyword" form:"site_keyword"` // 网站关键词
30+
SiteDesc *string `json:"site_desc" form:"site_desc"` // 网站描述
31+
SiteRecord *string `json:"site_record" form:"site_record"` // 网站备案号
32+
LogFile *multipart.FileHeader `json:"log"` // 上传 logo 图片
33+
FaviconFile *multipart.FileHeader `json:"favicon"` // 上传 favicon 图片
3434
}
3535

3636
ConfigUpdateResp struct {

api/v1/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package v1
22

33
type (
44
LoginReq struct {
5-
Username string `form:"username" json:"username" binding:"required" example:"admin"`
6-
Password string `form:"password,default=value" json:"password,default=123456" example:"123456"`
5+
Username string `form:"username" json:"username" binding:"required" example:"admin"` // 用户名
6+
Password string `form:"password,default=value" json:"password,default=123456" example:"123456"` // 密码
77
}
88

99
LoginResp struct {

internal/dal/repository/repository.go

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/ch3nnn/webstack-go/internal/dal/model"
2121
"github.com/ch3nnn/webstack-go/internal/dal/query"
22+
"github.com/ch3nnn/webstack-go/pkg/gormx"
2223
"github.com/ch3nnn/webstack-go/pkg/log"
2324
"github.com/ch3nnn/webstack-go/pkg/zapgorm2"
2425
)
@@ -28,10 +29,7 @@ type Repository struct {
2829
db *gorm.DB
2930
}
3031

31-
func NewRepository(
32-
logger *log.Logger,
33-
db *gorm.DB,
34-
) *Repository {
32+
func NewRepository(logger *log.Logger, db *gorm.DB) *Repository {
3533
return &Repository{
3634
logger: logger,
3735
db: db,
@@ -47,11 +45,11 @@ func NewDB(conf *viper.Viper, l *log.Logger) *gorm.DB {
4745

4846
dsn := conf.GetString("data.db.user.dsn")
4947
switch conf.GetString("data.db.user.driver") {
50-
case "mysql":
48+
case gormx.MYSQL:
5149
dialector = mysql.Open(dsn)
52-
case "postgres":
50+
case gormx.POSTGRES:
5351
dialector = postgres.Open(dsn)
54-
case "sqlite":
52+
case gormx.SQLITE:
5553
dialector = sqlite.Open(dsn)
5654
default:
5755
panic("unknown db driver")
@@ -138,82 +136,12 @@ func autoMigrateAndInitialize(db *gorm.DB) {
138136
os.Exit(0)
139137
}
140138

141-
err = query.SysMenu.WithContext(ctx).
142-
Create(
143-
&model.SysMenu{
144-
ID: 1,
145-
Pid: 0,
146-
Name: "网站管理",
147-
Icon: "users",
148-
Level: 1,
149-
Sort: 500,
150-
IsUsed: true,
151-
},
152-
&model.SysMenu{
153-
ID: 2,
154-
Pid: 1,
155-
Name: "网站分类",
156-
Link: "/admin/category",
157-
Level: 1,
158-
Sort: 501,
159-
IsUsed: true,
160-
},
161-
&model.SysMenu{
162-
ID: 3,
163-
Pid: 1,
164-
Name: "网站信息",
165-
Link: "/admin/site",
166-
Level: 1,
167-
Sort: 502,
168-
IsUsed: true,
169-
},
170-
&model.SysMenu{
171-
ID: 4,
172-
Pid: 0,
173-
Name: "系统管理",
174-
Level: 1,
175-
Sort: 600,
176-
IsUsed: true,
177-
},
178-
&model.SysMenu{
179-
ID: 5,
180-
Pid: 4,
181-
Name: "网站配置",
182-
Link: "/admin/config",
183-
Level: 1,
184-
Sort: 601,
185-
IsUsed: true,
186-
},
187-
)
188-
if err != nil {
139+
if err = query.SysMenu.WithContext(ctx).Create(DefaultSysMenuAdmin...); err != nil {
189140
fmt.Println("menu migrate error")
190141
os.Exit(0)
191142
}
192143

193-
err = query.SysUserMenu.WithContext(ctx).
194-
Create(
195-
&model.SysUserMenu{
196-
UserID: 1,
197-
MenuID: 1,
198-
},
199-
&model.SysUserMenu{
200-
UserID: 1,
201-
MenuID: 2,
202-
},
203-
&model.SysUserMenu{
204-
UserID: 1,
205-
MenuID: 3,
206-
},
207-
&model.SysUserMenu{
208-
UserID: 1,
209-
MenuID: 4,
210-
},
211-
&model.SysUserMenu{
212-
UserID: 1,
213-
MenuID: 5,
214-
},
215-
)
216-
if err != nil {
144+
if err = query.SysUserMenu.WithContext(ctx).Create(DefaultSysUserMenuAdmin...); err != nil {
217145
fmt.Println("user menu migrate error")
218146
os.Exit(0)
219147
}

internal/dal/repository/sys_menu.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,57 @@ package repository
33
import (
44
"context"
55

6+
"github.com/ch3nnn/webstack-go/internal/dal/model"
67
"github.com/ch3nnn/webstack-go/internal/dal/query"
78
)
89

10+
var DefaultSysMenuAdmin = []*model.SysMenu{
11+
{
12+
ID: 1,
13+
Pid: 0,
14+
Name: "网站管理",
15+
Icon: "users",
16+
Level: 1,
17+
Sort: 500,
18+
IsUsed: true,
19+
},
20+
{
21+
ID: 2,
22+
Pid: 1,
23+
Name: "网站分类",
24+
Link: "/admin/category",
25+
Level: 1,
26+
Sort: 501,
27+
IsUsed: true,
28+
},
29+
{
30+
ID: 3,
31+
Pid: 1,
32+
Name: "网站信息",
33+
Link: "/admin/site",
34+
Level: 1,
35+
Sort: 502,
36+
IsUsed: true,
37+
},
38+
{
39+
ID: 4,
40+
Pid: 0,
41+
Name: "系统管理",
42+
Level: 1,
43+
Sort: 600,
44+
IsUsed: true,
45+
},
46+
{
47+
ID: 5,
48+
Pid: 4,
49+
Name: "网站配置",
50+
Link: "/admin/config",
51+
Level: 1,
52+
Sort: 601,
53+
IsUsed: true,
54+
},
55+
}
56+
957
var _ iCustomGenSysMenuFunc = (*customSysMenuDao)(nil)
1058

1159
type (

internal/dal/repository/sys_user_menu.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,34 @@ package repository
33
import (
44
"context"
55

6+
"github.com/ch3nnn/webstack-go/internal/dal/model"
67
"github.com/ch3nnn/webstack-go/internal/dal/query"
78
)
89

10+
// DefaultSysUserMenuAdmin is used to store the menu information of the admin user DefaultSysMenuAdmin
11+
var DefaultSysUserMenuAdmin = []*model.SysUserMenu{
12+
{
13+
UserID: 1,
14+
MenuID: 1,
15+
},
16+
{
17+
UserID: 1,
18+
MenuID: 2,
19+
},
20+
{
21+
UserID: 1,
22+
MenuID: 3,
23+
},
24+
{
25+
UserID: 1,
26+
MenuID: 4,
27+
},
28+
{
29+
UserID: 1,
30+
MenuID: 5,
31+
},
32+
}
33+
934
var _ iCustomGenSysUserMenuFunc = (*customSysUserMenuDao)(nil)
1035

1136
type (

pkg/gormx/db.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @Author: chentong
3+
* @Date: 2025/02/03 11:33
4+
*/
5+
6+
package gormx
7+
8+
const (
9+
MYSQL = "mysql"
10+
POSTGRES = "postgres"
11+
SQLITE = "sqlite"
12+
)

pkg/log/log.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,13 @@ func NewLog(conf *viper.Viper) *Logger {
2222
// log address "out.log" User-defined
2323
lp := conf.GetString("log.log_file_name")
2424
lv := conf.GetString("log.log_level")
25-
var level zapcore.Level
26-
// debug<info<warn<error<fatal<panic
27-
switch lv {
28-
case "debug":
29-
level = zap.DebugLevel
30-
case "info":
31-
level = zap.InfoLevel
32-
case "warn":
33-
level = zap.WarnLevel
34-
case "error":
35-
level = zap.ErrorLevel
36-
default:
37-
level = zap.InfoLevel
25+
26+
// debug < info < warn < error < fatal < panic
27+
level := zap.NewAtomicLevel()
28+
if err := level.UnmarshalText([]byte(lv)); err != nil {
29+
panic("set config log level error. (debug info warn error fatal panic)")
3830
}
31+
3932
hook := lumberjack.Logger{
4033
Filename: lp, // Log file path
4134
MaxSize: conf.GetInt("log.max_size"), // Maximum size unit for each log file: M

0 commit comments

Comments
 (0)