optimization module init
This commit is contained in:
parent
8de2f0e3a5
commit
5264ee005e
35
options.go
35
options.go
|
@ -1,52 +1,67 @@
|
||||||
package rest
|
package rest
|
||||||
|
|
||||||
import "git.nobla.cn/golang/rest/types"
|
import (
|
||||||
|
"git.nobla.cn/golang/rest/types"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
type Options struct {
|
type options struct {
|
||||||
urlPrefix string
|
urlPrefix string
|
||||||
moduleName string
|
moduleName string
|
||||||
disableDomain bool
|
disableDomain bool
|
||||||
|
db *gorm.DB
|
||||||
router types.HttpRouter
|
router types.HttpRouter
|
||||||
writer types.HttpWriter
|
writer types.HttpWriter
|
||||||
formatter *Formatter
|
formatter *Formatter
|
||||||
dirname string //文件目录
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(o *Options)
|
type Option func(o *options)
|
||||||
|
|
||||||
|
// WithDB 设置DB
|
||||||
|
func WithDB(db *gorm.DB) Option {
|
||||||
|
return func(o *options) {
|
||||||
|
o.db = db
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithUriPrefix 模块前缀
|
||||||
func WithUriPrefix(s string) Option {
|
func WithUriPrefix(s string) Option {
|
||||||
return func(o *Options) {
|
return func(o *options) {
|
||||||
o.urlPrefix = s
|
o.urlPrefix = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithModuleName 模块名称
|
||||||
func WithModuleName(s string) Option {
|
func WithModuleName(s string) Option {
|
||||||
return func(o *Options) {
|
return func(o *options) {
|
||||||
o.moduleName = s
|
o.moduleName = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithoutDomain 禁用域
|
// WithoutDomain 禁用域
|
||||||
func WithoutDomain() Option {
|
func WithoutDomain() Option {
|
||||||
return func(o *Options) {
|
return func(o *options) {
|
||||||
o.disableDomain = true
|
o.disableDomain = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithHttpRouter 设置HttpRouter
|
||||||
func WithHttpRouter(s types.HttpRouter) Option {
|
func WithHttpRouter(s types.HttpRouter) Option {
|
||||||
return func(o *Options) {
|
return func(o *options) {
|
||||||
o.router = s
|
o.router = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithHttpWriter 配置HttpWriter
|
||||||
func WithHttpWriter(s types.HttpWriter) Option {
|
func WithHttpWriter(s types.HttpWriter) Option {
|
||||||
return func(o *Options) {
|
return func(o *options) {
|
||||||
o.writer = s
|
o.writer = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithFormatter 配置Formatter
|
||||||
func WithFormatter(s *Formatter) Option {
|
func WithFormatter(s *Formatter) Option {
|
||||||
return func(o *Options) {
|
return func(o *options) {
|
||||||
o.formatter = s
|
o.formatter = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
37
rest.go
37
rest.go
|
@ -21,7 +21,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
modelEntities []*Model
|
modelEntities []*Model
|
||||||
httpRouter types.HttpRouter
|
globalOpts *options
|
||||||
hookMgr *hookManager
|
hookMgr *hookManager
|
||||||
timeKind = reflect.TypeOf(time.Time{}).Kind()
|
timeKind = reflect.TypeOf(time.Time{}).Kind()
|
||||||
timePtrKind = reflect.TypeOf(&time.Time{}).Kind()
|
timePtrKind = reflect.TypeOf(&time.Time{}).Kind()
|
||||||
|
@ -34,6 +34,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
globalOpts = &options{}
|
||||||
hookMgr = &hookManager{}
|
hookMgr = &hookManager{}
|
||||||
modelEntities = make([]*Model, 0)
|
modelEntities = make([]*Model, 0)
|
||||||
}
|
}
|
||||||
|
@ -479,27 +480,41 @@ func autoMigrate(ctx context.Context, db *gorm.DB, module string, model any) (na
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHttpRouter 设置HTTP路由
|
// Init 初始化
|
||||||
func SetHttpRouter(router types.HttpRouter) {
|
func Init(cbs ...Option) (err error) {
|
||||||
httpRouter = router
|
for _, cb := range cbs {
|
||||||
|
cb(globalOpts)
|
||||||
|
}
|
||||||
|
if globalOpts.db != nil {
|
||||||
|
err = globalOpts.db.AutoMigrate(&types.Schema{})
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// AutoMigrate 自动合并表的schema
|
// AutoMigrate 自动合并表的schema
|
||||||
func AutoMigrate(ctx context.Context, db *gorm.DB, model any, cbs ...Option) (err error) {
|
func AutoMigrate(ctx context.Context, model any, cbs ...Option) (err error) {
|
||||||
var (
|
var (
|
||||||
opts *Options
|
opts *options
|
||||||
table string
|
table string
|
||||||
router types.HttpRouter
|
router types.HttpRouter
|
||||||
)
|
)
|
||||||
opts = &Options{}
|
opts = &options{
|
||||||
|
db: globalOpts.db,
|
||||||
|
router: globalOpts.router,
|
||||||
|
writer: globalOpts.writer,
|
||||||
|
formatter: globalOpts.formatter,
|
||||||
|
moduleName: globalOpts.moduleName,
|
||||||
|
urlPrefix: globalOpts.urlPrefix,
|
||||||
|
disableDomain: globalOpts.disableDomain,
|
||||||
|
}
|
||||||
for _, cb := range cbs {
|
for _, cb := range cbs {
|
||||||
cb(opts)
|
cb(opts)
|
||||||
}
|
}
|
||||||
if table, err = autoMigrate(ctx, db, opts.moduleName, model); err != nil {
|
if table, err = autoMigrate(ctx, opts.db, opts.moduleName, model); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//路由模块处理
|
//路由模块处理
|
||||||
modelValue := newModel(model, db, types.Naming{
|
modelValue := newModel(model, opts.db, types.Naming{
|
||||||
Pluralize: inflector.Pluralize(table),
|
Pluralize: inflector.Pluralize(table),
|
||||||
Singular: inflector.Singularize(table),
|
Singular: inflector.Singularize(table),
|
||||||
ModuleName: opts.moduleName,
|
ModuleName: opts.moduleName,
|
||||||
|
@ -510,9 +525,6 @@ func AutoMigrate(ctx context.Context, db *gorm.DB, model any, cbs ...Option) (er
|
||||||
if opts.router != nil {
|
if opts.router != nil {
|
||||||
router = opts.router
|
router = opts.router
|
||||||
}
|
}
|
||||||
if router == nil && httpRouter != nil {
|
|
||||||
router = httpRouter
|
|
||||||
}
|
|
||||||
if opts.urlPrefix != "" {
|
if opts.urlPrefix != "" {
|
||||||
modelValue.urlPrefix = opts.urlPrefix
|
modelValue.urlPrefix = opts.urlPrefix
|
||||||
}
|
}
|
||||||
|
@ -699,6 +711,7 @@ func SetFieldValue(stmt *gorm.Statement, refValue reflect.Value, column string,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SafeSetFileValue 安全设置模型某个字段的值
|
||||||
func SafeSetFileValue(stmt *gorm.Statement, refValue reflect.Value, column string, value any) {
|
func SafeSetFileValue(stmt *gorm.Statement, refValue reflect.Value, column string, value any) {
|
||||||
var (
|
var (
|
||||||
idx = -1
|
idx = -1
|
||||||
|
|
Loading…
Reference in New Issue