update
This commit is contained in:
1 parent
e174f1e740
commit
6bb6c69700
6 files changed
+51
-57
No files matched your search
@@ -71,29 +71,27 @@
|
|||||||
settings:
|
settings:
|
||||||
application:
|
application:
|
||||||
# 项目启动环境
|
# 项目启动环境
|
||||||
env: dev
|
mode: dev # dev开发环境 test测试环境 prod线上环境;当 mode:demo 时,GET以外的请求操作提示
|
||||||
# 当 env:demo 时,GET以外的请求操作提示
|
demomsg: "谢谢您的参与,但为了大家更好的体验,所以本次提交就算了吧!"
|
||||||
envmsg: "谢谢您的参与,但为了大家更好的体验,所以本次提交就算了吧!"
|
host: 0.0.0.0 # 主机ip 或者域名,默认0.0.0.0
|
||||||
# 主机ip 或者域名,默认0.0.0.0
|
isinit: false # 是否需要初始化数据库结构以及基本数据;true:需要;false:不需要
|
||||||
host: 0.0.0.0
|
|
||||||
# 是否需要初始化数据库结构以及基本数据;true:需要;false:不需要
|
|
||||||
isinit: false
|
|
||||||
# log存放路径
|
|
||||||
logpath: temp/logs/log.log
|
|
||||||
# 服务名称
|
# 服务名称
|
||||||
name: go-admin
|
name: go-admin
|
||||||
# 服务端口
|
# 服务端口
|
||||||
port: 8000
|
port: 8000
|
||||||
readtimeout: 1
|
readtimeout: 1
|
||||||
writertimeout: 2
|
writertimeout: 2
|
||||||
|
log:
|
||||||
|
# 日志文件存放路径
|
||||||
|
dir: temp/logs
|
||||||
jwt:
|
jwt:
|
||||||
# JWT加密字符串
|
# JWT加密字符串
|
||||||
jwtsecret: go-admin
|
secret: go-admin
|
||||||
# 过期时间单位:秒
|
# 过期时间单位:秒
|
||||||
timeout: 3600
|
timeout: 3600
|
||||||
database:
|
database:
|
||||||
# 数据库名称
|
# 数据库名称
|
||||||
database: dbname
|
name: dbname
|
||||||
# 数据库类型
|
# 数据库类型
|
||||||
dbtype: mysql
|
dbtype: mysql
|
||||||
# 数据库地址
|
# 数据库地址
|
||||||
@@ -134,7 +132,8 @@ vi ./config/setting.yml (更改isinit和数据库连接)
|
|||||||
|
|
||||||
|
|
||||||
# 启动项目,也可以用IDE进行调试
|
# 启动项目,也可以用IDE进行调试
|
||||||
./go-admin
|
./go-admin server -c=config/settings.yml -p=8000 -m=dev
|
||||||
|
c
|
||||||
|
|
||||||
```
|
```
|
||||||
[在线文档](https://wenjianzhang.github.io/go-admin-site)
|
[在线文档](https://wenjianzhang.github.io/go-admin-site)
|
||||||
|
|||||||
+24
-29
@@ -41,11 +41,6 @@ func init() {
|
|||||||
StartCmd.PersistentFlags().StringVarP(&config, "config", "c", "config/settings.yml", "Start server with provided configuration file")
|
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(&port, "port", "p", "8000", "Tcp port server listening on")
|
||||||
StartCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "dev", "server mode ; eg:dev,test,prod")
|
StartCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "dev", "server mode ; eg:dev,test,prod")
|
||||||
//if mode=="dev"{
|
|
||||||
// config="config/settings.{dev.}yml"
|
|
||||||
//}else if mode=="test"{
|
|
||||||
// config="config/settings.{test.}yml"
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
@@ -70,32 +65,32 @@ func setup() {
|
|||||||
func run() error {
|
func run() error {
|
||||||
r := router.InitRouter()
|
r := router.InitRouter()
|
||||||
|
|
||||||
defer database.Eloquent.Close()
|
defer database.Eloquent.Close()
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: config2.ApplicationConfig.Host + ":" + config2.ApplicationConfig.Port,
|
Addr: config2.ApplicationConfig.Host + ":" + config2.ApplicationConfig.Port,
|
||||||
Handler: r,
|
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 ...")
|
||||||
|
|
||||||
go func() {
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
// 服务连接
|
defer cancel()
|
||||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := srv.Shutdown(ctx); err != nil {
|
||||||
log.Fatalf("listen: %s\n", err)
|
log.Fatal("Server Shutdown:", err)
|
||||||
}
|
}
|
||||||
}()
|
log.Println("Server exiting")
|
||||||
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
|
return nil
|
||||||
}
|
}
|
||||||
+8
-1
@@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"go-admin/cmd/api"
|
"go-admin/cmd/api"
|
||||||
"go-admin/cmd/migrate"
|
"go-admin/cmd/migrate"
|
||||||
@@ -14,6 +15,12 @@ var rootCmd = &cobra.Command{
|
|||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Long: `go-admin`,
|
Long: `go-admin`,
|
||||||
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return errors.New("requires at least one arg")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
PersistentPreRunE: func(*cobra.Command, []string) error { return nil },
|
PersistentPreRunE: func(*cobra.Command, []string) error { return nil },
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
usageStr := `go-admin 0.0.1`
|
usageStr := `go-admin 0.0.1`
|
||||||
@@ -31,4 +38,4 @@ func Execute() {
|
|||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package migrate
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go-admin/database"
|
||||||
orm "go-admin/database"
|
orm "go-admin/database"
|
||||||
"go-admin/models"
|
"go-admin/models"
|
||||||
"go-admin/models/gorm"
|
"go-admin/models/gorm"
|
||||||
@@ -17,7 +18,7 @@ var (
|
|||||||
mode string
|
mode string
|
||||||
StartCmd = &cobra.Command{
|
StartCmd = &cobra.Command{
|
||||||
Use: "migrate",
|
Use: "migrate",
|
||||||
Short: "Migrate about gdb operator",
|
Short: "migrate from struct to database",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
run()
|
run()
|
||||||
},
|
},
|
||||||
@@ -37,7 +38,7 @@ func run() {
|
|||||||
//2. 设置日志
|
//2. 设置日志
|
||||||
tools.InitLogger()
|
tools.InitLogger()
|
||||||
//3. 初始化数据库链接
|
//3. 初始化数据库链接
|
||||||
//dao.Setup()
|
database.Setup()
|
||||||
//4. 数据库迁移
|
//4. 数据库迁移
|
||||||
gorm.AutoMigrate(orm.Eloquent)
|
gorm.AutoMigrate(orm.Eloquent)
|
||||||
//db := gdb.GetDB()
|
//db := gdb.GetDB()
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ require (
|
|||||||
github.com/shirou/gopsutil v2.20.3+incompatible
|
github.com/shirou/gopsutil v2.20.3+incompatible
|
||||||
github.com/sirupsen/logrus v1.2.0
|
github.com/sirupsen/logrus v1.2.0
|
||||||
github.com/spf13/cobra v1.0.0
|
github.com/spf13/cobra v1.0.0
|
||||||
|
github.com/spf13/pflag v1.0.3
|
||||||
github.com/spf13/viper v1.6.2
|
github.com/spf13/viper v1.6.2
|
||||||
github.com/swaggo/gin-swagger v1.2.0
|
github.com/swaggo/gin-swagger v1.2.0
|
||||||
github.com/swaggo/swag v1.6.5
|
github.com/swaggo/swag v1.6.5
|
||||||
|
|||||||
+4
-13
@@ -1,7 +1,6 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"go-admin/models"
|
"go-admin/models"
|
||||||
@@ -57,24 +56,16 @@ func LoggerToFile() gin.HandlerFunc {
|
|||||||
clientIP := c.ClientIP()
|
clientIP := c.ClientIP()
|
||||||
|
|
||||||
// 日志格式
|
// 日志格式
|
||||||
logger.Infof(" %3d %13v %15s %s %s \r\n",
|
logger.Infof("%s [%s] %3d %13v %15s %s %s \r\n",
|
||||||
|
startTime.Format("2006-01-02 15:04:05.9999"),
|
||||||
|
strings.ToUpper(logger.Level.String()),
|
||||||
statusCode,
|
statusCode,
|
||||||
latencyTime,
|
latencyTime,
|
||||||
clientIP,
|
clientIP,
|
||||||
reqMethod,
|
reqMethod,
|
||||||
reqUri,
|
reqUri,
|
||||||
)
|
)
|
||||||
logger.Infoln("-")
|
|
||||||
fmt.Println(
|
|
||||||
startTime.Format("\n2006-01-02 15:04:05.9999"),
|
|
||||||
"[INFO]",
|
|
||||||
reqMethod,
|
|
||||||
reqUri,
|
|
||||||
statusCode,
|
|
||||||
latencyTime,
|
|
||||||
reqUri,
|
|
||||||
clientIP,
|
|
||||||
)
|
|
||||||
if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" {
|
if c.Request.Method != "GET" && c.Request.Method != "OPTIONS" {
|
||||||
menu := models.Menu{}
|
menu := models.Menu{}
|
||||||
menu.Path = reqUri
|
menu.Path = reqUri
|
||||||
|
|||||||
Reference in new issue
Block a user