From 2e78eff03324261fde0f0410d72e8b48c7a7067f Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Wed, 24 Mar 2021 11:28:57 +0800 Subject: [PATCH] fix: start error --- .gitignore | 6 ++++-- config.yaml.example | 1 - go.mod | 13 +++++-------- initialize/gorm.go | 23 +++++++++++++++-------- initialize/redis.go | 10 ++++------ initialize/server.go | 11 ++++------- utils/directory.go | 8 ++++---- utils/jwt.go | 8 +++----- 8 files changed, 39 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index eee8ba2..62d64e6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,10 +10,12 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out - +log/* .vscode/* conf/app.ini .idea/* runtime/logs/* +vendor/* config.yaml -go.sum \ No newline at end of file +go.sum +__debug_bin \ No newline at end of file diff --git a/config.yaml.example b/config.yaml.example index 5f56fe5..863f229 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -1,5 +1,4 @@ app: - page_size: 10 jwt_secret: '23347$040412' run_mode: 'debug' server: diff --git a/go.mod b/go.mod index bd488f0..4315978 100644 --- a/go.mod +++ b/go.mod @@ -3,33 +3,30 @@ module github.com/viletyy/potato go 1.15 require ( - cloud.google.com/go/logging v1.3.0 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 - github.com/astaxie/beego v1.12.3 github.com/beego/beego/v2 v2.0.1 - github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/fsnotify/fsnotify v1.4.9 github.com/gin-gonic/gin v1.6.3 github.com/go-openapi/spec v0.20.3 // indirect - github.com/go-playground/validator/v10 v10.2.0 github.com/go-redis/redis v6.15.9+incompatible + github.com/golang/protobuf v1.4.3 // indirect + github.com/google/go-cmp v0.5.4 // indirect github.com/google/uuid v1.2.0 github.com/jinzhu/gorm v1.9.16 github.com/leodido/go-urn v1.2.1 // indirect github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/lestrrat-go/strftime v1.0.4 // indirect github.com/mailru/easyjson v0.7.7 // indirect - github.com/russross/blackfriday/v2 v2.1.0 // indirect - github.com/smartystreets/goconvey v1.6.4 // indirect github.com/spf13/viper v1.7.1 github.com/swaggo/gin-swagger v1.3.0 github.com/swaggo/swag v1.7.0 go.uber.org/zap v1.15.0 + golang.org/x/mod v0.4.1 // indirect golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4 // indirect golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect golang.org/x/tools v0.1.0 // indirect - gopkg.in/fsnotify.v1 v1.4.7 + google.golang.org/protobuf v1.25.0 // indirect gopkg.in/go-playground/validator.v9 v9.31.0 - gopkg.in/ini.v1 v1.62.0 + gopkg.in/ini.v1 v1.62.0 // indirect ) diff --git a/initialize/gorm.go b/initialize/gorm.go index 0c7d82d..d93f055 100644 --- a/initialize/gorm.go +++ b/initialize/gorm.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-22 10:12:38 * @LastEditors: viletyy - * @LastEditTime: 2021-03-22 16:56:26 + * @LastEditTime: 2021-03-23 09:49:41 * @FilePath: /potato/initialize/gorm.go */ package initialize @@ -10,13 +10,15 @@ import ( "fmt" "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/mysql" + _ "github.com/jinzhu/gorm/dialects/postgres" "github.com/viletyy/potato/global" + "github.com/viletyy/potato/models" + "github.com/viletyy/potato/models/basic" ) -var dbConfig = global.GO_CONFIG.Database - func Gorm() *gorm.DB { - switch dbConfig.Type { + switch global.GO_CONFIG.Database.Type { case "mysql": return GormMysql() case "postgresql": @@ -27,7 +29,7 @@ func Gorm() *gorm.DB { } func GormMysql() *gorm.DB { - db, err := gorm.Open("mysql", fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbConfig.User, dbConfig.Password, dbConfig.Host, dbConfig.Port, dbConfig.Name)) + db, err := gorm.Open("mysql", fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", global.GO_CONFIG.Database.User, global.GO_CONFIG.Database.Password, global.GO_CONFIG.Database.Host, global.GO_CONFIG.Database.Port, global.GO_CONFIG.Database.Name)) if err != nil { global.GO_LOG.Error(fmt.Sprintf("Mysql Gorm Open Error: %v", err)) } @@ -36,7 +38,7 @@ func GormMysql() *gorm.DB { } func GormPostgresql() *gorm.DB { - db, err := gorm.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s port=%d sslmode=disable password=%s", dbConfig.Host, dbConfig.User, dbConfig.Name, dbConfig.Port, dbConfig.Password)) + db, err := gorm.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s port=%d sslmode=disable password=%s", global.GO_CONFIG.Database.Host, global.GO_CONFIG.Database.User, global.GO_CONFIG.Database.Name, global.GO_CONFIG.Database.Port, global.GO_CONFIG.Database.Password)) if err != nil { global.GO_LOG.Error(fmt.Sprintf("Postgresql Gorm Open Error: %v", err)) } @@ -47,14 +49,19 @@ func GormPostgresql() *gorm.DB { func GormSet(db *gorm.DB) { // 设置表前缀 gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string { - return dbConfig.TablePrefix + defaultTableName + return global.GO_CONFIG.Database.TablePrefix + defaultTableName } // 设置日志 db.LogMode(true) // 设置迁移 - db.AutoMigrate() + db.AutoMigrate( + basic.Vendor{}, + basic.Business{}, + basic.MetaDatabase{}, + models.User{}, + ) // 设置空闲连接池中的最大连接数 db.DB().SetMaxIdleConns(10) diff --git a/initialize/redis.go b/initialize/redis.go index a52605d..ae2dedc 100644 --- a/initialize/redis.go +++ b/initialize/redis.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-22 10:12:42 * @LastEditors: viletyy - * @LastEditTime: 2021-03-22 17:02:17 + * @LastEditTime: 2021-03-23 09:26:11 * @FilePath: /potato/initialize/redis.go */ package initialize @@ -13,13 +13,11 @@ import ( "github.com/viletyy/potato/global" ) -var redisConfig = global.GO_CONFIG.Redis - func Redis() *redis.Client { rdb := redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%d", redisConfig.Host, redisConfig.Port), - Password: redisConfig.Password, - DB: int(redisConfig.Db), + Addr: fmt.Sprintf("%s:%d", global.GO_CONFIG.Redis.Host, global.GO_CONFIG.Redis.Port), + Password: global.GO_CONFIG.Redis.Password, + DB: int(global.GO_CONFIG.Redis.Db), }) RedisSet(rdb) diff --git a/initialize/server.go b/initialize/server.go index eef1e3e..799283e 100644 --- a/initialize/server.go +++ b/initialize/server.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-22 17:03:27 * @LastEditors: viletyy - * @LastEditTime: 2021-03-22 23:55:32 + * @LastEditTime: 2021-03-23 10:23:12 * @FilePath: /potato/initialize/server.go */ package initialize @@ -20,18 +20,15 @@ import ( "github.com/viletyy/potato/utils" ) -var serverConfig = global.GO_CONFIG.Server - func RunServer() { binding.Validator = new(utils.DefaultValidator) router := routers.InitRouter() - server := &http.Server{ - Addr: fmt.Sprintf(":%d", serverConfig.HttpPort), + Addr: fmt.Sprintf(":%d", global.GO_CONFIG.Server.HttpPort), Handler: router, - ReadTimeout: time.Duration(serverConfig.ReadTimeout), - WriteTimeout: time.Duration(serverConfig.WriteTimeout), + ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, + WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, MaxHeaderBytes: 1 << 20, } diff --git a/utils/directory.go b/utils/directory.go index 59a8fd9..8c07c43 100644 --- a/utils/directory.go +++ b/utils/directory.go @@ -1,15 +1,15 @@ /* * @Date: 2021-03-22 10:45:37 * @LastEditors: viletyy - * @LastEditTime: 2021-03-22 10:49:19 + * @LastEditTime: 2021-03-23 09:29:06 * @FilePath: /potato/utils/directory.go */ package utils import ( + "fmt" "os" - "github.com/viletyy/potato/global" "go.uber.org/zap" ) @@ -31,10 +31,10 @@ func CreateDir(dirs ...string) (err error) { return err } if !exist { - global.GO_LOG.Debug("create directory" + v) + fmt.Println("create directory" + v) err = os.MkdirAll(v, os.ModePerm) if err != nil { - global.GO_LOG.Error("create directory"+v, zap.Any(" error:", err)) + fmt.Println("create directory"+v, zap.Any(" error:", err)) } } } diff --git a/utils/jwt.go b/utils/jwt.go index 7fe31bc..74a3670 100644 --- a/utils/jwt.go +++ b/utils/jwt.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-22 17:16:46 * @LastEditors: viletyy - * @LastEditTime: 2021-03-23 00:54:22 + * @LastEditTime: 2021-03-23 09:24:08 * @FilePath: /potato/utils/jwt.go */ package utils @@ -15,8 +15,6 @@ import ( "github.com/viletyy/potato/global" ) -var jwtSecret = []byte(global.GO_CONFIG.App.JwtSecret) - type CustomClaims struct { UserId int64 jwt.StandardClaims @@ -37,7 +35,7 @@ func GenerateToken(userId int64) (string, error) { } tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - tokenString, err := tokenClaims.SignedString(jwtSecret) + tokenString, err := tokenClaims.SignedString([]byte(global.GO_CONFIG.App.JwtSecret)) if err != nil { global.GO_LOG.Error(fmt.Sprintf("General Token Error: %v", err)) } @@ -56,7 +54,7 @@ func ParseToken(tokenString string) (*CustomClaims, error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) } - return jwtSecret, nil + return []byte(global.GO_CONFIG.App.JwtSecret), nil }) if tokenClaims != nil {