工具包调整

添加 jwt nanoid 配置 及 初始化
This commit is contained in:
Shikong 2023-02-26 23:48:03 +08:00
parent e694e66b85
commit 998ae4a9b9
14 changed files with 130 additions and 16 deletions

View File

@ -94,4 +94,4 @@ fabric.properties
# Android studio 3.1+ serialized cache file # Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser .idea/caches/build_file_checksums.ser
./config.toml config.toml

View File

@ -10,6 +10,8 @@ require (
github.com/go-sql-driver/mysql v1.7.0 github.com/go-sql-driver/mysql v1.7.0
github.com/gofiber/fiber/v2 v2.42.0 github.com/gofiber/fiber/v2 v2.42.0
github.com/gofiber/swagger v0.1.9 github.com/gofiber/swagger v0.1.9
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/matoous/go-nanoid v1.5.0
github.com/mitchellh/mapstructure v1.5.0 github.com/mitchellh/mapstructure v1.5.0
github.com/pelletier/go-toml/v2 v2.0.6 github.com/pelletier/go-toml/v2 v2.0.6
github.com/sony/sonyflake v1.1.0 github.com/sony/sonyflake v1.1.0

View File

@ -166,6 +166,8 @@ github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFG
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@ -362,6 +364,8 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matoous/go-nanoid v1.5.0 h1:VRorl6uCngneC4oUQqOYtO3S0H5QKFtKuKycFG3euek=
github.com/matoous/go-nanoid v1.5.0/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=

View File

@ -9,6 +9,7 @@ import (
"matrix-middle-service/pkg/database" "matrix-middle-service/pkg/database"
"matrix-middle-service/pkg/logger" "matrix-middle-service/pkg/logger"
fib "matrix-middle-service/pkg/utils/fiber" fib "matrix-middle-service/pkg/utils/fiber"
"matrix-middle-service/pkg/utils/nanoid"
"matrix-middle-service/pkg/utils/sonyflake" "matrix-middle-service/pkg/utils/sonyflake"
"time" "time"
) )
@ -63,7 +64,8 @@ func InitApp(conf *config.Conf) {
logger.Log().Fatal(err) logger.Log().Fatal(err)
} }
sonyflake.Init() sonyflake.Init(conf.SonyFlake)
nanoid.Init(conf.NanoIdConfig)
} }
func Shutdown() { func Shutdown() {

View File

@ -1,12 +1,19 @@
package config package config
import ( import (
"matrix-middle-service/pkg/config/database"
"matrix-middle-service/pkg/config/jwt"
"matrix-middle-service/pkg/config/nanoid"
"matrix-middle-service/pkg/config/sonyflake"
"matrix-middle-service/pkg/database/driver/mysql/conf" "matrix-middle-service/pkg/database/driver/mysql/conf"
) )
type Conf struct { type Conf struct {
Server *ServerConfig `yaml:"server"` Server *ServerConfig `comment:"服务器配置"`
DataSources []interface{} `yaml:"mysql"` DataSources []interface{} `comment:"数据源配置"`
NanoIdConfig *nanoid.Config `comment:"nanoId 配置"`
JwtConfig *jwt.Config `comment:"jwt 配置"`
SonyFlake *sonyflake.Config `comment:"sonyFlake 配置"`
} }
type ServerConfig struct { type ServerConfig struct {
@ -33,12 +40,23 @@ func DefaultConfig() *Conf {
EnableRoutesMsg: false, EnableRoutesMsg: false,
}, },
DataSources: []interface{}{ DataSources: []interface{}{
DataSourcesType[any]{ database.DataSourcesType[any]{
Type: "mysql", Type: "mysql",
Config: conf.NewMysqlConfig([]string{ Config: conf.NewMysqlConfig([]string{
"root:12341234@tcp(127.0.0.1:3306)/db?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai", "root:12341234@tcp(127.0.0.1:3306)/db?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai",
}, true), }, true),
}, },
}, },
NanoIdConfig: &nanoid.Config{
Sequence: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Length: 36,
},
JwtConfig: &jwt.Config{
Secret: "1234567890",
Expire: 7200,
},
SonyFlake: &sonyflake.Config{
StartTime: sonyflake.DefaultSonyFlakeStartTime,
},
} }
} }

View File

@ -1,4 +1,4 @@
package config package database
type DataSources = []DataSourcesType[any] type DataSources = []DataSourcesType[any]

View File

@ -0,0 +1,6 @@
package jwt
type Config struct {
Secret string `comment:"密钥"`
Expire int64 `comment:"超时时间(秒)"`
}

View File

@ -0,0 +1,6 @@
package nanoid
type Config struct {
Sequence string `comment:"字符序列"`
Length int `comment:"生成 id 长度"`
}

View File

@ -0,0 +1,7 @@
package sonyflake
const DefaultSonyFlakeStartTime = "2023-01-01"
type Config struct {
StartTime string `comment:"sonyFlake 起始生成时间 (格式: 2023-01-01)"`
}

View File

@ -2,14 +2,14 @@ package database
import ( import (
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"matrix-middle-service/pkg/config" "matrix-middle-service/pkg/config/database"
"matrix-middle-service/pkg/logger" "matrix-middle-service/pkg/logger"
) )
var SupportDataSources = make(map[string]InitDSFn) var SupportDataSources = make(map[string]InitDSFn)
var drivers []DataBase var drivers []DataBase
type InitDSFn = func(conf config.DataSourcesType[any]) DataBase type InitDSFn = func(conf database.DataSourcesType[any]) DataBase
type DataBase interface { type DataBase interface {
Open() error Open() error
@ -21,7 +21,7 @@ func Init(conf []interface{}) {
logger.Log().Debug("[数据源] 初始化") logger.Log().Debug("[数据源] 初始化")
for _, item := range conf { for _, item := range conf {
dst := config.DataSourcesType[any]{} dst := database.DataSourcesType[any]{}
_ = mapstructure.Decode(item, &dst) _ = mapstructure.Decode(item, &dst)
fn, ok := SupportDataSources[dst.Type] fn, ok := SupportDataSources[dst.Type]

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"matrix-middle-service/pkg/config" database2 "matrix-middle-service/pkg/config/database"
"matrix-middle-service/pkg/database" "matrix-middle-service/pkg/database"
"matrix-middle-service/pkg/database/driver/mysql/conf" "matrix-middle-service/pkg/database/driver/mysql/conf"
"matrix-middle-service/pkg/logger" "matrix-middle-service/pkg/logger"
@ -23,7 +23,7 @@ func init() {
logger.Log().Debug("添加 mysql 数据源支持") logger.Log().Debug("添加 mysql 数据源支持")
}) })
database.Register("mysql", func(c config.DataSourcesType[any]) database.DataBase { database.Register("mysql", func(c database2.DataSourcesType[any]) database.DataBase {
mc := &conf.Config{} mc := &conf.Config{}
_ = mapstructure.Decode(c.Config, &mc) _ = mapstructure.Decode(c.Config, &mc)

View File

@ -0,0 +1,51 @@
package jwt
import (
"errors"
"github.com/golang-jwt/jwt/v4"
jwt2 "matrix-middle-service/pkg/config/jwt"
"time"
)
var JwtConfig *jwt2.Config
// UserClaims
// @Param id body string true
// @Param account body string true
type UserClaims struct {
Id string `json:"id"`
Account string `json:"account"`
Identify string `json:"identify"`
jwt.RegisteredClaims `swaggerignore:"true"`
}
func GetToken(claims UserClaims) (string, error) {
iat := time.Now().Unix()
claims.IssuedAt = jwt.NewNumericDate(time.Unix(iat, 0))
expire := iat + JwtConfig.Expire
claims.ExpiresAt = jwt.NewNumericDate(time.Unix(expire, 0))
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = claims
return token.SignedString([]byte(JwtConfig.Secret))
}
func ParseToken(tokenStr string) (*UserClaims, error) {
token, err := jwt.ParseWithClaims(tokenStr, &UserClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(JwtConfig.Secret), nil
})
if err != nil {
return nil, err
}
if claims, ok := token.Claims.(*UserClaims); ok && token.Valid {
return claims, nil
}
return nil, errors.New("无效的令牌")
}

View File

@ -0,0 +1,18 @@
package nanoid
import (
gonanoid "github.com/matoous/go-nanoid"
"matrix-middle-service/pkg/config/nanoid"
"matrix-middle-service/pkg/logger"
)
var config *nanoid.Config
func Init(conf *nanoid.Config) {
logger.Log().Debug("[nanoid] 初始化")
config = conf
}
func GetNanoId() (string, error) {
return gonanoid.Generate(config.Sequence, config.Length)
}

View File

@ -2,17 +2,17 @@ package sonyflake
import ( import (
"github.com/sony/sonyflake" "github.com/sony/sonyflake"
sf "matrix-middle-service/pkg/config/sonyflake"
"matrix-middle-service/pkg/logger"
"time" "time"
) )
const DefaultSonyFlakeStartTime = "2023-01-01"
var generator *sonyflake.Sonyflake var generator *sonyflake.Sonyflake
func Init() { func Init(config *sf.Config) {
logger.Log().Debug("[sonyFlake] 初始化")
var s sonyflake.Settings var s sonyflake.Settings
s.StartTime, _ = time.Parse("2006-01-02", DefaultSonyFlakeStartTime) s.StartTime, _ = time.Parse("2006-01-02", config.StartTime)
generator = sonyflake.NewSonyflake(s) generator = sonyflake.NewSonyflake(s)
} }