结构调整添加cmd
This commit is contained in:
1 parent
f2829bfd98
commit
a2f4c00246
72 files changed
+838
-597
No files matched your search
@@ -0,0 +1,96 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go-admin/database"
|
||||
"go-admin/router"
|
||||
"go-admin/tools"
|
||||
config2 "go-admin/tools/config"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
config string
|
||||
port string
|
||||
mode string
|
||||
//auto tls
|
||||
//StartCmd : set up restful api server
|
||||
StartCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Short: "Start API server",
|
||||
Example: "go-admin server config/settings.yml",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
usage()
|
||||
setup()
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return run()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
StartCmd.PersistentFlags().StringVarP(&config, "config", "c", "config/settings.yml", "Start server with provided configuration file")
|
||||
StartCmd.PersistentFlags().StringVarP(&port, "port", "p", "8000", "Tcp port server listening on")
|
||||
StartCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "dev", "server mode ; eg:dev,test,prod")
|
||||
}
|
||||
|
||||
func usage() {
|
||||
usageStr := `starting api server`
|
||||
fmt.Printf("%s\n", usageStr)
|
||||
}
|
||||
|
||||
func setup() {
|
||||
|
||||
//1. 读取配置
|
||||
config2.ConfigSetup(config)
|
||||
//2. 设置日志
|
||||
tools.InitLogger()
|
||||
//3. 初始化数据库链接
|
||||
database.Setup()
|
||||
//4. 设置gin mode
|
||||
if viper.GetString("settings.application.mode") == string(tools.ModeProd) {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
r := router.InitRouter()
|
||||
|
||||
defer database.Eloquent.Close()
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: config2.ApplicationConfig.Host + ":" + config2.ApplicationConfig.Port,
|
||||
Handler: r,
|
||||
}
|
||||
|
||||
go func() {
|
||||
// 服务连接
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("listen: %s\n", err)
|
||||
}
|
||||
}()
|
||||
log.Println("Server Run ", config2.ApplicationConfig.Host+":"+config2.ApplicationConfig.Port)
|
||||
log.Println("Enter Control + C Shutdown Server")
|
||||
// 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
|
||||
quit := make(chan os.Signal)
|
||||
signal.Notify(quit, os.Interrupt)
|
||||
<-quit
|
||||
log.Println("Shutdown Server ...")
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
log.Fatal("Server Shutdown:", err)
|
||||
}
|
||||
log.Println("Server exiting")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"go-admin/cmd/api"
|
||||
"go-admin/cmd/migrate"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "go-admin",
|
||||
Short: "-v",
|
||||
SilenceUsage: true,
|
||||
DisableAutoGenTag: true,
|
||||
Long: `go-admin`,
|
||||
PersistentPreRunE: func(*cobra.Command, []string) error { return nil },
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
usageStr := `go-admin 0.0.1`
|
||||
log.Printf("%s\n", usageStr)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(api.StartCmd)
|
||||
rootCmd.AddCommand(migrate.StartCmd)
|
||||
}
|
||||
|
||||
//Execute : apply commands
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
orm "go-admin/database"
|
||||
"go-admin/models"
|
||||
"go-admin/models/gorm"
|
||||
tools2 "go-admin/models/tools"
|
||||
"go-admin/tools"
|
||||
config2 "go-admin/tools/config"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
config string
|
||||
mode string
|
||||
StartCmd = &cobra.Command{
|
||||
Use: "migrate",
|
||||
Short: "Migrate about gdb operator",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
run()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
StartCmd.PersistentFlags().StringVarP(&config, "config", "c", "config/app.yml", "Start server with provided configuration file")
|
||||
StartCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "dev", "server mode ; eg:dev,test,prod")
|
||||
}
|
||||
|
||||
func run() {
|
||||
usage := `start migrate`
|
||||
fmt.Println(usage)
|
||||
//1. 读取配置
|
||||
config2.ConfigSetup(config)
|
||||
//2. 设置日志
|
||||
tools.InitLogger()
|
||||
//3. 初始化数据库链接
|
||||
//dao.Setup()
|
||||
//4. 数据库迁移
|
||||
gorm.AutoMigrate(orm.Eloquent)
|
||||
//db := gdb.GetDB()
|
||||
//db.AutoMigrate(migrateModel()...)
|
||||
usage = `finish`
|
||||
fmt.Println(usage)
|
||||
}
|
||||
|
||||
func migrateModel() error {
|
||||
if config2.DatabaseConfig.Dbtype == "mysql" {
|
||||
orm.Eloquent = orm.Eloquent.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4")
|
||||
}
|
||||
orm.Eloquent.SingularTable(true)
|
||||
return orm.Eloquent.AutoMigrate(
|
||||
new(models.CasbinRule),
|
||||
new(tools2.SysTables),
|
||||
new(tools2.SysColumns),
|
||||
new(models.Dept),
|
||||
new(models.Menu),
|
||||
new(models.LoginLog),
|
||||
new(models.SysOperLog),
|
||||
new(models.RoleMenu),
|
||||
new(models.SysRoleDept),
|
||||
new(models.SysUser),
|
||||
new(models.SysRole),
|
||||
new(models.Post),
|
||||
new(models.DictData),
|
||||
new(models.SysConfig),
|
||||
new(models.DictType),
|
||||
).Error
|
||||
}
|
||||
Reference in new issue
Block a user