Skip to content

Commit

Permalink
fix: Fix table name prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Sep 1, 2023
1 parent 8e72200 commit cf8080d
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 74 deletions.
15 changes: 5 additions & 10 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// The function defines a CLI command to start a server with various flags and options, including the
// ability to run as a daemon.
func StartCmd(ver string) *cli.Command {
func StartCmd() *cli.Command {
return &cli.Command{
Name: "start",
Usage: "Start server",
Expand Down Expand Up @@ -53,37 +53,32 @@ func StartCmd(ver string) *cli.Command {
if c.Bool("daemon") {
bin, err := filepath.Abs(os.Args[0])
if err != nil {
fmt.Printf("Failed to get absolute path for command: %s \n", err.Error())
return err
}

if err := c.Set("daemon", "false"); err != nil {
fmt.Printf("failed to get absolute path for command: %s \n", err.Error())
return err
}

args := []string{"start"}
args = append(args, "-d", workDir)
args = append(args, "-c", configs)
args = append(args, "-s", staticDir)
fmt.Printf("execute command: %s %s\n", bin, strings.Join(args, " "))
fmt.Printf("execute command: %s %s \n", bin, strings.Join(args, " "))
command := exec.Command(bin, args...)
err = command.Start()
if err != nil {
fmt.Printf("Failed to start daemon thread: %s \n", err.Error())
fmt.Printf("failed to start daemon thread: %s \n", err.Error())
return err
}

pid := command.Process.Pid
_ = os.WriteFile(fmt.Sprintf("%s.lock", c.App.Name), []byte(fmt.Sprintf("%d", pid)), 0666)
fmt.Printf("Service %s daemon thread started with pid %d \n", config.C.General.AppName, pid)
fmt.Printf("service %s daemon thread started with pid %d \n", config.C.General.AppName, pid)
os.Exit(0)
}

err := bootstrap.Run(context.Background(), bootstrap.RunConfig{
WorkDir: workDir,
Configs: configs,
StaticDir: staticDir,
Version: ver,
})
if err != nil {
panic(err)
Expand Down
5 changes: 1 addition & 4 deletions internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type RunConfig struct {
WorkDir string // Working directory
Configs string // Directory or files (multiple separated by commas)
StaticDir string // Static files directory
Version string
}

// The Run function initializes and starts a service with configuration and logging, and handles
Expand All @@ -37,7 +36,6 @@ func Run(ctx context.Context, runCfg RunConfig) error {
config.MustLoad(workDir, strings.Split(runCfg.Configs, ",")...)
config.C.General.WorkDir = workDir
config.C.Middleware.Static.Dir = staticDir
config.C.General.Version = runCfg.Version
config.C.Print()

cleanLoggerFn, err := logging.InitWithConfig(ctx, &config.C.Logger, initLoggerHook)
Expand All @@ -47,7 +45,7 @@ func Run(ctx context.Context, runCfg RunConfig) error {
ctx = logging.NewTag(ctx, logging.TagKeyMain)

logging.Context(ctx).Info("starting service ...",
zap.String("version", runCfg.Version),
zap.String("version", config.C.General.Version),
zap.Int("pid", os.Getpid()),
zap.String("work_dir", workDir),
zap.String("config", runCfg.Configs),
Expand All @@ -74,7 +72,6 @@ func Run(ctx context.Context, runCfg RunConfig) error {
}

return util.Run(ctx, func(ctx context.Context) (func(), error) {
// Start HTTP server
cleanHTTPServerFn, err := startHTTPServer(ctx, injector)
if err != nil {
return cleanInjectorFn, err
Expand Down
6 changes: 4 additions & 2 deletions internal/bootstrap/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ func startHTTPServer(ctx context.Context, injector *wirex.Injector) (func(), err
Skip: config.C.Middleware.Recovery.Skip,
}))
e.NoMethod(func(c *gin.Context) {
util.ResError(c, errors.MethodNotAllowed("", "Method not allowed"))
util.ResError(c, errors.MethodNotAllowed("", "Method Not Allowed"))
})
e.NoRoute(func(c *gin.Context) {
util.ResError(c, errors.NotFound("", "Not found"))
util.ResError(c, errors.NotFound("", "Not Found"))
})

allowedPrefixes := injector.M.RouterPrefixes()

// Register middlewares
if err := useHTTPMiddlewares(ctx, e, injector, allowedPrefixes); err != nil {
return nil, err
Expand All @@ -53,6 +54,7 @@ func startHTTPServer(ctx context.Context, injector *wirex.Injector) (func(), err
return nil, err
}

// Register swagger
if !config.C.General.DisableSwagger {
e.StaticFile("/openapi.json", filepath.Join(config.C.General.WorkDir, "openapi.json"))
e.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
Expand Down
7 changes: 7 additions & 0 deletions internal/mods/rbac/dal/menu.dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ func GetMenuDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
return util.GetDB(ctx, defDB).Model(new(schema.Menu))
}

// Get menu table name
func GetMenuTableName(defDB *gorm.DB) string {
stat := gorm.Statement{DB: defDB}
stat.Parse(&schema.Menu{})
return stat.Table
}

// Menu management for RBAC
type Menu struct {
DB *gorm.DB
Expand Down
7 changes: 7 additions & 0 deletions internal/mods/rbac/dal/menu_resource.dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ func GetMenuResourceDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
return util.GetDB(ctx, defDB).Model(new(schema.MenuResource))
}

// Get menu resource table name
func GetMenuResourceTableName(defDB *gorm.DB) string {
stat := gorm.Statement{DB: defDB}
stat.Parse(&schema.MenuResource{})
return stat.Table
}

// Menu resource management for RBAC
type MenuResource struct {
DB *gorm.DB
Expand Down
7 changes: 7 additions & 0 deletions internal/mods/rbac/dal/role.dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ func GetRoleDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
return util.GetDB(ctx, defDB).Model(new(schema.Role))
}

// Get role table name
func GetRoleTableName(defDB *gorm.DB) string {
stat := gorm.Statement{DB: defDB}
stat.Parse(&schema.Role{})
return stat.Table
}

// Role management for RBAC
type Role struct {
DB *gorm.DB
Expand Down
7 changes: 7 additions & 0 deletions internal/mods/rbac/dal/role_menu.dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ func GetRoleMenuDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
return util.GetDB(ctx, defDB).Model(new(schema.RoleMenu))
}

// Get role menu table name
func GetRoleMenuTableName(defDB *gorm.DB) string {
stat := gorm.Statement{DB: defDB}
stat.Parse(&schema.RoleMenu{})
return stat.Table
}

// Role permissions for RBAC
type RoleMenu struct {
DB *gorm.DB
Expand Down
7 changes: 7 additions & 0 deletions internal/mods/rbac/dal/user.dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ func GetUserDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
return util.GetDB(ctx, defDB).Model(new(schema.User))
}

// Get user table name
func GetUserTableName(defDB *gorm.DB) string {
stat := gorm.Statement{DB: defDB}
stat.Parse(&schema.User{})
return stat.Table
}

// User management for RBAC
type User struct {
DB *gorm.DB
Expand Down
11 changes: 9 additions & 2 deletions internal/mods/rbac/dal/user_role.dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ func GetUserRoleDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
return util.GetDB(ctx, defDB).Model(new(schema.UserRole))
}

// Get user role table name
func GetUserRoleTableName(defDB *gorm.DB) string {
stat := gorm.Statement{DB: defDB}
stat.Parse(&schema.UserRole{})
return stat.Table
}

// User roles for RBAC
type UserRole struct {
DB *gorm.DB
Expand All @@ -27,9 +34,9 @@ func (a *UserRole) Query(ctx context.Context, params schema.UserRoleQueryParam,
opt = opts[0]
}

db := a.DB.Table(fmt.Sprintf("%s AS a", schema.UserRole{}.TableName()))
db := a.DB.Table(fmt.Sprintf("%s AS a", GetUserRoleTableName(a.DB)))
if opt.JoinRole {
db = db.Joins(fmt.Sprintf("left join %s b on a.role_id=b.id", schema.Role{}.TableName()))
db = db.Joins(fmt.Sprintf("left join %s b on a.role_id=b.id", GetRoleTableName(a.DB)))
db = db.Select("a.*,b.name as role_name")
}

Expand Down
1 change: 1 addition & 0 deletions internal/mods/rbac/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (a *RBAC) Init(ctx context.Context) error {
if err := a.AutoMigrate(ctx); err != nil {
return err
}

if err := a.Casbinx.Load(ctx); err != nil {
return err
}
Expand Down
4 changes: 0 additions & 4 deletions internal/mods/rbac/schema/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ type Menu struct {
Resources MenuResources `json:"resources" gorm:"-"` // Resources of menu
}

func (a Menu) TableName() string {
return "menu"
}

// Defining the query parameters for the `Menu` struct.
type MenuQueryParam struct {
util.PaginationParam
Expand Down
4 changes: 0 additions & 4 deletions internal/mods/rbac/schema/menu_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ type MenuResource struct {
UpdatedAt time.Time `json:"updated_at" gorm:"index;"` // Update time
}

func (a MenuResource) TableName() string {
return "menu_resource"
}

// Defining the query parameters for the `MenuResource` struct.
type MenuResourceQueryParam struct {
util.PaginationParam
Expand Down
4 changes: 0 additions & 4 deletions internal/mods/rbac/schema/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ type Role struct {
Menus RoleMenus `json:"menus" gorm:"-"` // Role menu list
}

func (a Role) TableName() string {
return "role"
}

// Defining the query parameters for the `Role` struct.
type RoleQueryParam struct {
util.PaginationParam
Expand Down
4 changes: 0 additions & 4 deletions internal/mods/rbac/schema/role_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ type RoleMenu struct {
UpdatedAt time.Time `json:"updated_at" gorm:"index;"` // Update time
}

func (a RoleMenu) TableName() string {
return "role_menu"
}

// Defining the query parameters for the `RoleMenu` struct.
type RoleMenuQueryParam struct {
util.PaginationParam
Expand Down
4 changes: 0 additions & 4 deletions internal/mods/rbac/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ type User struct {
Roles UserRoles `json:"roles" gorm:"-"` // Roles of user
}

func (a User) TableName() string {
return "user"
}

// Defining the query parameters for the `User` struct.
type UserQueryParam struct {
util.PaginationParam
Expand Down
4 changes: 0 additions & 4 deletions internal/mods/rbac/schema/user_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ type UserRole struct {
RoleName string `json:"role_name" gorm:"<-:false"` // From Role.Name
}

func (a UserRole) TableName() string {
return "user_role"
}

// Defining the query parameters for the `UserRole` struct.
type UserRoleQueryParam struct {
util.PaginationParam
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
app.Version = VERSION
app.Usage = "A lightweight, flexible, elegant and full-featured RBAC scaffolding based on GIN + GORM 2.0 + Casbin 2.0 + Wire DI."
app.Commands = []*cli.Command{
cmd.StartCmd(VERSION),
cmd.StartCmd(),
cmd.StopCmd(),
cmd.VersionCmd(VERSION),
}
Expand Down
Loading

0 comments on commit cf8080d

Please sign in to comment.