Skip to content

Commit

Permalink
refactor(cmfx): 调整项目的初始化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Dec 3, 2024
1 parent be14262 commit dbe12b1
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 288 deletions.
12 changes: 11 additions & 1 deletion cmd/server/web.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<web>
<http port=":8080" />
<http port=":8080">
<url>http://localhost:8080</url>
<cors maxAge="3600">
<origins>
<origin>*</origin>
</origins>
<allowHeaders>
<header>*</header>
</allowHeaders>
</cors>
</http>
<logs created="15:04:05.000" caller="true">
<handlers>
<handler type="file">
Expand Down
14 changes: 0 additions & 14 deletions cmfx/cmfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@
// Package cmfx 基于 https://github.com/issue9/web 框架的一些通用模块
package cmfx

// # restdoc cmfx 文档
//
// @media application/json application/xml application/cbor
// @version [Version]
// @tag admin 管理员端
// @tag rbac RBAC
// @tag auth 登录凭证
// @tag system 系统相关
// @tag settings 设置项
// @tag common 公共接口
// @resp 4XX * github.com/issue9/web.Problem
// @resp 5XX * github.com/issue9/web.Problem desc
// TODO 删除!

import (
"github.com/issue9/web"
"github.com/issue9/web/locales"
Expand Down
90 changes: 0 additions & 90 deletions cmfx/init.go

This file was deleted.

14 changes: 0 additions & 14 deletions cmfx/init_test.go

This file was deleted.

55 changes: 36 additions & 19 deletions cmfx/initial/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import (

"github.com/issue9/upload/v3"
"github.com/issue9/web"
"github.com/issue9/web/mimetype/cbor"
"github.com/issue9/web/mimetype/json"
"github.com/issue9/web/openapi"
"github.com/issue9/web/server"
"github.com/issue9/web/server/app"
"github.com/issue9/webuse/v7/handlers/debug"
"github.com/issue9/webuse/v7/middlewares/acl/ratelimit"
"github.com/issue9/webuse/v7/middlewares/auth/token"
"github.com/issue9/webuse/v7/plugins/openapi/swagger"

"github.com/issue9/cmfx/cmfx"
"github.com/issue9/cmfx/cmfx/initial/test"
"github.com/issue9/cmfx/cmfx/modules/admin"
"github.com/issue9/cmfx/cmfx/modules/system"
"github.com/issue9/cmfx/cmfx/user/passport/otp/totp"
Expand All @@ -41,34 +45,47 @@ func initServer(id, ver string, o *server.Options, user *Config, action string)
return nil, err
}

cmfx.Init(s, user.Ratelimit, web.PluginFunc(swagger.Install))
doc := test.NewDocument(s)
c := web.NewCache(user.Ratelimit.Prefix, s.Cache())
limit := ratelimit.New(c, user.Ratelimit.Capacity, user.Ratelimit.Rate.Duration(), nil)

router := s.Routers().New("default", nil,
web.WithAllowedCORS(3600),
web.WithURLDomain(user.URL),
web.WithAnyInterceptor("any"), web.WithDigitInterceptor("digit"),
router := s.Routers().New("main", nil,
web.WithAnyInterceptor("any"),
web.WithDigitInterceptor("digit"),
)
debug.RegisterDev(router, "/debug")
router.Get("/openapi", doc.Handler())

db := user.DB.DB()
s.OnClose(func() error { return db.Close() })
doc := openapi.New(s, web.Phrase("The api doc of %s", s.ID()),
openapi.WithMediaType(json.Mimetype, cbor.Mimetype),
openapi.WithClassicResponse(),
openapi.WithContact("caixw", "", "https://github.com/caixw"),
openapi.WithDescription(
nil,
web.Phrase("problems response:\n\n%s\n", openapi.MarkdownProblems(s, 0)),
),
openapi.WithSecurityScheme(token.SecurityScheme("token", web.Phrase("token auth"))),
swagger.WithCDN(""),
)
s.Use(web.PluginFunc(swagger.Install))
router.Get("/openapi", doc.Handler())

mod := cmfx.Init(s, limit, user.DB.DB(), router, doc)
var (
adminMod = cmfx.NewModule("admin", web.Phrase("admin"), s, db, router, doc)
systemMod = cmfx.NewModule("system", web.Phrase("system"), s, db, router, doc)
adminMod = mod.New("admin", web.Phrase("admin"))
systemMod = mod.New("system", web.Phrase("system"))
)

uploadSaver, err := upload.NewLocalSaver("./upload", user.URL+"/admin/upload", upload.Day, func(dir, filename, ext string) string {
return filepath.Join(dir, s.UniqueID()+ext) // filename 可能带非英文字符
})
if err != nil {
return nil, err
}

switch action {
case "serve":
url, err := mod.Router().URL(false, user.Admin.User.URLPrefix+"/upload", nil)
if err != nil {
return nil, err
}
uploadSaver, err := upload.NewLocalSaver("./upload", url, upload.Day, func(dir, filename, ext string) string {
return filepath.Join(dir, s.UniqueID()+ext) // filename 可能带非英文字符
})
if err != nil {
return nil, err
}
adminL := admin.Load(adminMod, user.Admin, uploadSaver)
totp.Init(adminL.UserModule(), "totp", web.Phrase("TOTP passport"))

Expand Down
73 changes: 42 additions & 31 deletions cmfx/initial/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import (
"github.com/issue9/orm/v6"
"github.com/issue9/orm/v6/dialect"
"github.com/issue9/web"
"github.com/issue9/web/filter"
"github.com/issue9/web/server/config"

"github.com/issue9/cmfx/cmfx"
"github.com/issue9/cmfx/cmfx/filters"
"github.com/issue9/cmfx/cmfx/locales"
"github.com/issue9/cmfx/cmfx/modules/admin"
"github.com/issue9/cmfx/cmfx/modules/system"
)

// Config 配置文件的自定义部分内容
type Config struct {
// URL 路由的基地址
URL string `yaml:"url" xml:"url" json:"url"`

// Ratelimit API 限速的配置
Ratelimit *cmfx.Ratelimit `yaml:"ratelimit" xml:"ratelimit" json:"ratelimit"`
Ratelimit *Ratelimit `yaml:"ratelimit" xml:"ratelimit" json:"ratelimit"`

// DB 数据库配置
DB *DB `yaml:"db" xml:"db" json:"db"`
Expand All @@ -33,29 +32,6 @@ type Config struct {
System *system.Config `yaml:"system,omitempty" xml:"system,omitempty" json:"system,omitempty"`
}

// DB 数据库的配置项
type DB struct {
// Prefix 表名前缀
//
// 可以为空。
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty" xml:"prefix,attr,omitempty"`

// 表示数据库的类型
//
// 目前支持以下几种类型:
// - sqlite3
// - sqlite 纯 Go
// - mysql
// - mariadb
// - postgres
Type string `yaml:"type" json:"type" xml:"type,attr"`

// 连接数据库的参数
DSN string `yaml:"dsn" json:"dsn" xml:"dsn"`

db *orm.DB
}

func (c *Config) SanitizeConfig() *web.FieldError {
if c.Ratelimit == nil {
return web.NewFieldError("ratelimit", locales.Required)
Expand Down Expand Up @@ -87,6 +63,29 @@ func (c *Config) SanitizeConfig() *web.FieldError {
return nil
}

// DB 数据库的配置项
type DB struct {
// 表名前缀
//
// 可以为空。
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty" xml:"prefix,attr,omitempty"`

// 表示数据库的类型
//
// 目前支持以下几种类型:
// - sqlite3
// - sqlite 纯 Go
// - mysql
// - mariadb
// - postgres
Type string `yaml:"type" json:"type" xml:"type,attr"`

// 连接数据库的参数
DSN string `yaml:"dsn" json:"dsn" xml:"dsn"`

db *orm.DB
}

func (conf *DB) SanitizeConfig() *web.FieldError {
var d orm.Dialect
switch conf.Type {
Expand Down Expand Up @@ -115,7 +114,19 @@ func (conf *DB) SanitizeConfig() *web.FieldError {
return nil
}

// DB 返回根据配置项生成的 [orm.DB] 实例
//
// 调用 [DB.SanitizeConfig] 之后生成。
func (conf *DB) DB() *orm.DB { return conf.db }

// Ratelimit API 访问限制
type Ratelimit struct {
Prefix string `yaml:"prefix" json:"prefix" xml:"prefix"` // 在缓存系统中前缀,保证数据的唯一性
Rate config.Duration `yaml:"rate" json:"rate" xml:"rate"` // 发放令牌的频率
Capacity uint64 `yaml:"capacity" json:"capacity" xml:"capacity,attr"` // 令牌桶的最高容量
}

func (r *Ratelimit) SanitizeConfig() *web.FieldError {
return filter.ToFieldError(
filters.NotEmpty("prefix", &r.Prefix),
filters.GreatEqual[config.Duration](0)("rate", &r.Rate),
filters.GreatEqual[uint64](10)("capacity", &r.Capacity),
)
}
3 changes: 2 additions & 1 deletion cmfx/initial/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cmd
import "github.com/issue9/config"

var (
_ config.Sanitizer = &DB{}
_ config.Sanitizer = &Config{}
_ config.Sanitizer = &Ratelimit{}
_ config.Sanitizer = &DB{}
)
28 changes: 0 additions & 28 deletions cmfx/initial/test/doc.go

This file was deleted.

Loading

0 comments on commit dbe12b1

Please sign in to comment.