98 lines
1.9 KiB
Go
98 lines
1.9 KiB
Go
package rest
|
|
|
|
import (
|
|
"git.nobla.cn/golang/rest/types"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type options struct {
|
|
urlPrefix string
|
|
moduleName string
|
|
disableDomain bool
|
|
db *gorm.DB
|
|
router types.HttpRouter
|
|
writer types.HttpWriter
|
|
permissionChecker types.PermissionChecker
|
|
formatter *Formatter
|
|
resourceDirectory string
|
|
}
|
|
|
|
type Option func(o *options)
|
|
|
|
func (o *options) Clone() *options {
|
|
return &options{
|
|
urlPrefix: o.urlPrefix,
|
|
moduleName: o.moduleName,
|
|
disableDomain: o.disableDomain,
|
|
db: o.db,
|
|
router: o.router,
|
|
writer: o.writer,
|
|
permissionChecker: o.permissionChecker,
|
|
formatter: o.formatter,
|
|
resourceDirectory: o.resourceDirectory,
|
|
}
|
|
}
|
|
|
|
// WithDB 设置DB
|
|
func WithDB(db *gorm.DB) Option {
|
|
return func(o *options) {
|
|
o.db = db
|
|
}
|
|
}
|
|
|
|
// WithUriPrefix 模块前缀
|
|
func WithUriPrefix(s string) Option {
|
|
return func(o *options) {
|
|
o.urlPrefix = s
|
|
}
|
|
}
|
|
|
|
// WithModuleName 模块名称
|
|
func WithModuleName(s string) Option {
|
|
return func(o *options) {
|
|
o.moduleName = s
|
|
}
|
|
}
|
|
|
|
// WithoutDomain 禁用域
|
|
func WithoutDomain() Option {
|
|
return func(o *options) {
|
|
o.disableDomain = true
|
|
}
|
|
}
|
|
|
|
// WithHttpRouter 设置HttpRouter
|
|
func WithHttpRouter(s types.HttpRouter) Option {
|
|
return func(o *options) {
|
|
o.router = s
|
|
}
|
|
}
|
|
|
|
// WithHttpWriter 配置HttpWriter
|
|
func WithHttpWriter(s types.HttpWriter) Option {
|
|
return func(o *options) {
|
|
o.writer = s
|
|
}
|
|
}
|
|
|
|
// WithFormatter 配置Formatter
|
|
func WithFormatter(s *Formatter) Option {
|
|
return func(o *options) {
|
|
o.formatter = s
|
|
}
|
|
}
|
|
|
|
// WithPermissionChecker 配置PermissionChecker
|
|
func WithPermissionChecker(s types.PermissionChecker) Option {
|
|
return func(o *options) {
|
|
o.permissionChecker = s
|
|
}
|
|
}
|
|
|
|
// WithResourceDirectory 配置资源目录
|
|
func WithResourceDirectory(s string) Option {
|
|
return func(o *options) {
|
|
o.resourceDirectory = s
|
|
}
|
|
}
|