fix: start error

This commit is contained in:
vilet.yy 2021-03-24 11:28:57 +08:00
parent 5ae5ef7cc4
commit 2e78eff033
8 changed files with 39 additions and 41 deletions

6
.gitignore vendored
View File

@ -10,10 +10,12 @@
# Output of the go coverage tool, specifically when used with LiteIDE # Output of the go coverage tool, specifically when used with LiteIDE
*.out *.out
log/*
.vscode/* .vscode/*
conf/app.ini conf/app.ini
.idea/* .idea/*
runtime/logs/* runtime/logs/*
vendor/*
config.yaml config.yaml
go.sum go.sum
__debug_bin

View File

@ -1,5 +1,4 @@
app: app:
page_size: 10
jwt_secret: '23347$040412' jwt_secret: '23347$040412'
run_mode: 'debug' run_mode: 'debug'
server: server:

13
go.mod
View File

@ -3,33 +3,30 @@ module github.com/viletyy/potato
go 1.15 go 1.15
require ( require (
cloud.google.com/go/logging v1.3.0
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 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/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/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fsnotify/fsnotify v1.4.9 github.com/fsnotify/fsnotify v1.4.9
github.com/gin-gonic/gin v1.6.3 github.com/gin-gonic/gin v1.6.3
github.com/go-openapi/spec v0.20.3 // indirect 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/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/google/uuid v1.2.0
github.com/jinzhu/gorm v1.9.16 github.com/jinzhu/gorm v1.9.16
github.com/leodido/go-urn v1.2.1 // indirect github.com/leodido/go-urn v1.2.1 // indirect
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/lestrrat-go/strftime v1.0.4 // indirect github.com/lestrrat-go/strftime v1.0.4 // indirect
github.com/mailru/easyjson v0.7.7 // 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/spf13/viper v1.7.1
github.com/swaggo/gin-swagger v1.3.0 github.com/swaggo/gin-swagger v1.3.0
github.com/swaggo/swag v1.7.0 github.com/swaggo/swag v1.7.0
go.uber.org/zap v1.15.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/net v0.0.0-20210316092652-d523dce5a7f4 // indirect
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
golang.org/x/tools v0.1.0 // 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/go-playground/validator.v9 v9.31.0
gopkg.in/ini.v1 v1.62.0 gopkg.in/ini.v1 v1.62.0 // indirect
) )

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-22 10:12:38 * @Date: 2021-03-22 10:12:38
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-03-22 16:56:26 * @LastEditTime: 2021-03-23 09:49:41
* @FilePath: /potato/initialize/gorm.go * @FilePath: /potato/initialize/gorm.go
*/ */
package initialize package initialize
@ -10,13 +10,15 @@ import (
"fmt" "fmt"
"github.com/jinzhu/gorm" "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/global"
"github.com/viletyy/potato/models"
"github.com/viletyy/potato/models/basic"
) )
var dbConfig = global.GO_CONFIG.Database
func Gorm() *gorm.DB { func Gorm() *gorm.DB {
switch dbConfig.Type { switch global.GO_CONFIG.Database.Type {
case "mysql": case "mysql":
return GormMysql() return GormMysql()
case "postgresql": case "postgresql":
@ -27,7 +29,7 @@ func Gorm() *gorm.DB {
} }
func GormMysql() *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 { if err != nil {
global.GO_LOG.Error(fmt.Sprintf("Mysql Gorm Open Error: %v", err)) global.GO_LOG.Error(fmt.Sprintf("Mysql Gorm Open Error: %v", err))
} }
@ -36,7 +38,7 @@ func GormMysql() *gorm.DB {
} }
func GormPostgresql() *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 { if err != nil {
global.GO_LOG.Error(fmt.Sprintf("Postgresql Gorm Open Error: %v", err)) 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) { func GormSet(db *gorm.DB) {
// 设置表前缀 // 设置表前缀
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string { gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return dbConfig.TablePrefix + defaultTableName return global.GO_CONFIG.Database.TablePrefix + defaultTableName
} }
// 设置日志 // 设置日志
db.LogMode(true) db.LogMode(true)
// 设置迁移 // 设置迁移
db.AutoMigrate() db.AutoMigrate(
basic.Vendor{},
basic.Business{},
basic.MetaDatabase{},
models.User{},
)
// 设置空闲连接池中的最大连接数 // 设置空闲连接池中的最大连接数
db.DB().SetMaxIdleConns(10) db.DB().SetMaxIdleConns(10)

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-22 10:12:42 * @Date: 2021-03-22 10:12:42
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-03-22 17:02:17 * @LastEditTime: 2021-03-23 09:26:11
* @FilePath: /potato/initialize/redis.go * @FilePath: /potato/initialize/redis.go
*/ */
package initialize package initialize
@ -13,13 +13,11 @@ import (
"github.com/viletyy/potato/global" "github.com/viletyy/potato/global"
) )
var redisConfig = global.GO_CONFIG.Redis
func Redis() *redis.Client { func Redis() *redis.Client {
rdb := redis.NewClient(&redis.Options{ rdb := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", redisConfig.Host, redisConfig.Port), Addr: fmt.Sprintf("%s:%d", global.GO_CONFIG.Redis.Host, global.GO_CONFIG.Redis.Port),
Password: redisConfig.Password, Password: global.GO_CONFIG.Redis.Password,
DB: int(redisConfig.Db), DB: int(global.GO_CONFIG.Redis.Db),
}) })
RedisSet(rdb) RedisSet(rdb)

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-22 17:03:27 * @Date: 2021-03-22 17:03:27
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-03-22 23:55:32 * @LastEditTime: 2021-03-23 10:23:12
* @FilePath: /potato/initialize/server.go * @FilePath: /potato/initialize/server.go
*/ */
package initialize package initialize
@ -20,18 +20,15 @@ import (
"github.com/viletyy/potato/utils" "github.com/viletyy/potato/utils"
) )
var serverConfig = global.GO_CONFIG.Server
func RunServer() { func RunServer() {
binding.Validator = new(utils.DefaultValidator) binding.Validator = new(utils.DefaultValidator)
router := routers.InitRouter() router := routers.InitRouter()
server := &http.Server{ server := &http.Server{
Addr: fmt.Sprintf(":%d", serverConfig.HttpPort), Addr: fmt.Sprintf(":%d", global.GO_CONFIG.Server.HttpPort),
Handler: router, Handler: router,
ReadTimeout: time.Duration(serverConfig.ReadTimeout), ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(serverConfig.WriteTimeout), WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
MaxHeaderBytes: 1 << 20, MaxHeaderBytes: 1 << 20,
} }

View File

@ -1,15 +1,15 @@
/* /*
* @Date: 2021-03-22 10:45:37 * @Date: 2021-03-22 10:45:37
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-03-22 10:49:19 * @LastEditTime: 2021-03-23 09:29:06
* @FilePath: /potato/utils/directory.go * @FilePath: /potato/utils/directory.go
*/ */
package utils package utils
import ( import (
"fmt"
"os" "os"
"github.com/viletyy/potato/global"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -31,10 +31,10 @@ func CreateDir(dirs ...string) (err error) {
return err return err
} }
if !exist { if !exist {
global.GO_LOG.Debug("create directory" + v) fmt.Println("create directory" + v)
err = os.MkdirAll(v, os.ModePerm) err = os.MkdirAll(v, os.ModePerm)
if err != nil { if err != nil {
global.GO_LOG.Error("create directory"+v, zap.Any(" error:", err)) fmt.Println("create directory"+v, zap.Any(" error:", err))
} }
} }
} }

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-22 17:16:46 * @Date: 2021-03-22 17:16:46
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-03-23 00:54:22 * @LastEditTime: 2021-03-23 09:24:08
* @FilePath: /potato/utils/jwt.go * @FilePath: /potato/utils/jwt.go
*/ */
package utils package utils
@ -15,8 +15,6 @@ import (
"github.com/viletyy/potato/global" "github.com/viletyy/potato/global"
) )
var jwtSecret = []byte(global.GO_CONFIG.App.JwtSecret)
type CustomClaims struct { type CustomClaims struct {
UserId int64 UserId int64
jwt.StandardClaims jwt.StandardClaims
@ -37,7 +35,7 @@ func GenerateToken(userId int64) (string, error) {
} }
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := tokenClaims.SignedString(jwtSecret) tokenString, err := tokenClaims.SignedString([]byte(global.GO_CONFIG.App.JwtSecret))
if err != nil { if err != nil {
global.GO_LOG.Error(fmt.Sprintf("General Token Error: %v", err)) 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 { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) 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 { if tokenClaims != nil {