Skip to content

Commit

Permalink
feat(cmfx/modules/system): 添加 Config.URLPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed May 6, 2024
1 parent 4dea6af commit bdc02fa
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 55 deletions.
9 changes: 9 additions & 0 deletions cmfx/modules/system/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (

// Config 配置项
type Config struct {
// URLPrefix 该模块下的地址前缀
URLPrefix string `yaml:"urlPrefix" json:"urlPrefix" xml:"urlPrefix"`

// Backup 备份数据的选项
Backup *Backup `yaml:"backup,omitempty" json:"backup,omitempty" xml:"backup,omitempty"`
}
Expand All @@ -36,6 +39,12 @@ type Backup struct {
}

func (c *Config) SanitizeConfig() *web.FieldError {
if c.URLPrefix == "" {
c.URLPrefix = "/system"
} else if c.URLPrefix[0] != '/' {
return web.NewFieldError("urlPrefix", locales.InvalidValue)
}

if c.Backup != nil {
if err := c.Backup.SanitizeConfig(); err != nil {
return err.AddFieldParent("backup")
Expand Down
104 changes: 52 additions & 52 deletions cmfx/modules/system/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,68 @@
package system

import (
"time"
"time"

"github.com/issue9/web"
"github.com/issue9/webuse/v7/handlers/monitor"
"github.com/issue9/webuse/v7/plugins/health"
"github.com/issue9/web"
"github.com/issue9/webuse/v7/handlers/monitor"
"github.com/issue9/webuse/v7/plugins/health"

"github.com/issue9/cmfx/cmfx"
"github.com/issue9/cmfx/cmfx/modules/admin"
"github.com/issue9/cmfx/cmfx"
"github.com/issue9/cmfx/cmfx/modules/admin"
)

type Loader struct {
mod *cmfx.Module
admin *admin.Loader
health *health.Health
monitor *monitor.Monitor
mod *cmfx.Module
admin *admin.Loader
health *health.Health
monitor *monitor.Monitor

// 可能为空
buildBackupFilename func(time.Time) string
// 可能为空
buildBackupFilename func(time.Time) string
}

// Load 加载当前模块
//
// conf 当前模块的配置项,需要调用者自先调用 [Config.SanitizeConfig] 对数据进行校正;
func Load(mod *cmfx.Module, conf *Config, adminL *admin.Loader) *Loader {
store, err := newHealthDBStore(mod)
if err != nil {
panic(web.SprintError(mod.Server().Locale().Printer(), true, err))
}

m := &Loader{
mod: mod,
admin: adminL,
health: health.New(store),
monitor: monitor.New(mod.Server(), 30*time.Second),
}

mod.Server().Use(m.health)
m.health.Fill(mod.Server())

g := adminL.ResourceGroup()
resGetInfo := g.New("get-info", web.Phrase("view system info"))
resGetServices := g.New("get-services", web.Phrase("view services"))
resGetAPIs := g.New("get-apis", web.Phrase("view apis"))
restBackup := g.New("backup", web.Phrase("backup database"))

adminRouter := mod.Router().Prefix(adminL.URLPrefix(), web.MiddlewareFunc(m.admin.AuthFilter)).
Get("/system/info", resGetInfo(m.adminGetInfo)).
Get("/system/services", resGetServices(m.adminGetServices)).
Get("/system/apis", resGetAPIs(m.adminGetAPIs)).
Get("/system/monitor", resGetInfo(m.adminGetMonitor))

mod.Router().Get("/system/problems", m.commonGetProblems)

if conf.Backup != nil {
m.buildBackupFilename = conf.Backup.buildFile
mod.Server().Services().AddCron(web.Phrase("backup database"), func(now time.Time) error {
return mod.DB().Backup(m.buildBackupFilename(now))
}, conf.Backup.Cron, true)

adminRouter.Get("/system/backup", restBackup(m.adminPostBackup))
}

return m
store, err := newHealthDBStore(mod)
if err != nil {
panic(web.SprintError(mod.Server().Locale().Printer(), true, err))
}

m := &Loader{
mod: mod,
admin: adminL,
health: health.New(store),
monitor: monitor.New(mod.Server(), 30*time.Second),
}

mod.Server().Use(m.health)
m.health.Fill(mod.Server())

g := adminL.ResourceGroup()
resGetInfo := g.New("get-info", web.Phrase("view system info"))
resGetServices := g.New("get-services", web.Phrase("view services"))
resGetAPIs := g.New("get-apis", web.Phrase("view apis"))
restBackup := g.New("backup", web.Phrase("backup database"))

adminRouter := mod.Router().Prefix(adminL.URLPrefix()+conf.URLPrefix, web.MiddlewareFunc(m.admin.AuthFilter))
adminRouter.Get("/info", resGetInfo(m.adminGetInfo)).
Get("/services", resGetServices(m.adminGetServices)).
Get("/apis", resGetAPIs(m.adminGetAPIs)).
Get("/monitor", resGetInfo(m.adminGetMonitor))

mod.Router().Prefix(conf.URLPrefix).Get("/problems", m.commonGetProblems)

if conf.Backup != nil {
m.buildBackupFilename = conf.Backup.buildFile

mod.Server().Services().AddCron(web.Phrase("backup database"), func(now time.Time) error {
return mod.DB().Backup(m.buildBackupFilename(now))
}, conf.Backup.Cron, true)

adminRouter.Get("/backup", restBackup(m.adminPostBackup))
}

return m
}
4 changes: 3 additions & 1 deletion cmfx/modules/system/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func newSystem(s *test.Suite) *Loader {
mod := s.NewModule("test")
Install(mod)

sys := Load(mod, &Config{}, adminM)
conf := &Config{}
s.Assertion().NotError(conf.SanitizeConfig())
sys := Load(mod, conf, adminM)
s.Assertion().NotNil(sys)

return sys
Expand Down
4 changes: 2 additions & 2 deletions cmfx/user/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *reqAccount) Filter(v *web.FilterContext) {

// SetState 设置用户状态
//
// 如果状态为非 [StateNormal],那么也将会被禁卡登录
// 如果状态为非 [StateNormal],那么也将会被禁止登录
//
// NOTE: 需要保证 u.ID、u.State 和 u.NO 是有效的。
func (m *Loader) SetState(tx *orm.Tx, u *User, s State) (err error) {
Expand Down Expand Up @@ -170,7 +170,7 @@ func (m *Loader) Middleware(next web.HandlerFunc) web.HandlerFunc { return m.tok

// CurrentUser 获取当前登录的用户信息
//
// 该信息由 AuthFilter 存储在 [web.Context.vars] 之中。
// 该信息由 [Loader.Middleware] 存储在 [web.Context.vars] 之中。
func (m *Loader) CurrentUser(ctx *web.Context) *User {
if u, found := m.token.GetInfo(ctx); found {
return u
Expand Down

0 comments on commit bdc02fa

Please sign in to comment.