mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-24 07:42:15 +08:00
添加 sonyflake && nanoid
This commit is contained in:
parent
0755485d84
commit
362b1ebc39
@ -26,6 +26,8 @@ func Run() {
|
|||||||
reloadRedis(global.Config)
|
reloadRedis(global.Config)
|
||||||
reloadDataSources(global.Config)
|
reloadDataSources(global.Config)
|
||||||
reloadJwt(global.Config)
|
reloadJwt(global.Config)
|
||||||
|
reloadSonyFlake(global.Config)
|
||||||
|
reloadNanoId(global.Config)
|
||||||
|
|
||||||
global.App = reloadApp(global.Config)
|
global.App = reloadApp(global.Config)
|
||||||
|
|
||||||
|
15
app/nanoid.go
Normal file
15
app/nanoid.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gofiber.study.skcks.cn/common/config"
|
||||||
|
"gofiber.study.skcks.cn/common/logger"
|
||||||
|
"gofiber.study.skcks.cn/common/utils"
|
||||||
|
"gofiber.study.skcks.cn/global"
|
||||||
|
)
|
||||||
|
|
||||||
|
func reloadNanoId(c *config.BasicConfig) {
|
||||||
|
global.NanoIdConfig = &c.NanoId
|
||||||
|
utils.MainAppExec(func() {
|
||||||
|
logger.Log.Infof("[√] [nanoid] 配置完成")
|
||||||
|
})
|
||||||
|
}
|
26
app/sonyflake.go
Normal file
26
app/sonyflake.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/sony/sonyflake"
|
||||||
|
"gofiber.study.skcks.cn/common/config"
|
||||||
|
"gofiber.study.skcks.cn/common/logger"
|
||||||
|
"gofiber.study.skcks.cn/common/utils"
|
||||||
|
"gofiber.study.skcks.cn/global"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const DefaultSonyFlakeStartTime = "2022-01-01"
|
||||||
|
|
||||||
|
func reloadSonyFlake(c *config.BasicConfig) {
|
||||||
|
var s sonyflake.Settings
|
||||||
|
s.StartTime, _ = time.Parse("2006-01-02", DefaultSonyFlakeStartTime)
|
||||||
|
global.SonyFlake = sonyflake.NewSonyflake(s)
|
||||||
|
|
||||||
|
if global.SonyFlake == nil {
|
||||||
|
logger.Log.Fatalf("[x] [sonyFlake] id 生成器 初始化失败")
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.MainAppExec(func() {
|
||||||
|
logger.Log.Infof("[√] [sonyFlake] id 生成器 初始化 完成")
|
||||||
|
})
|
||||||
|
}
|
@ -5,6 +5,12 @@ type BasicConfig struct {
|
|||||||
Mysql MysqlConfig `yaml:"mysql"`
|
Mysql MysqlConfig `yaml:"mysql"`
|
||||||
Redis RedisConfig `yaml:"redis"`
|
Redis RedisConfig `yaml:"redis"`
|
||||||
Jwt JwtConfig `yaml:"jwt"`
|
Jwt JwtConfig `yaml:"jwt"`
|
||||||
|
NanoId NanoIdConfig `yaml:"nanoId"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type NanoIdConfig struct {
|
||||||
|
Sequence string `yaml:"sequence"`
|
||||||
|
Length int `yaml:"length"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type JwtConfig struct {
|
type JwtConfig struct {
|
||||||
|
@ -29,3 +29,7 @@ redis:
|
|||||||
addr: "10.10.10.100:16379"
|
addr: "10.10.10.100:16379"
|
||||||
pass: "12341234"
|
pass: "12341234"
|
||||||
db: 0
|
db: 0
|
||||||
|
|
||||||
|
nanoId:
|
||||||
|
sequence: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||||
|
length: 36
|
@ -5,7 +5,7 @@ import (
|
|||||||
"gofiber.study.skcks.cn/common/errorx"
|
"gofiber.study.skcks.cn/common/errorx"
|
||||||
"gofiber.study.skcks.cn/common/response"
|
"gofiber.study.skcks.cn/common/response"
|
||||||
"gofiber.study.skcks.cn/controller/types"
|
"gofiber.study.skcks.cn/controller/types"
|
||||||
"gofiber.study.skcks.cn/dto"
|
"gofiber.study.skcks.cn/model/dto"
|
||||||
"gofiber.study.skcks.cn/services/auth"
|
"gofiber.study.skcks.cn/services/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -75,3 +75,47 @@ func (c *Controller) JwtDecode() {
|
|||||||
return ctx.JSON(response.NewResponse(claims))
|
return ctx.JSON(response.NewResponse(claims))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SonyFlakeTest sonyFlake id 生成测试
|
||||||
|
//
|
||||||
|
// @Summary sonyFlake id 生成测试
|
||||||
|
// @Description sonyFlake id 生成测试
|
||||||
|
// @Tags Test
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} response.Response{data=int}
|
||||||
|
// @Failure default {object} errorx.CodeErrorResponse
|
||||||
|
// @Router /test/sonyFlake [get]
|
||||||
|
func (c *Controller) SonyFlakeTest() {
|
||||||
|
c.Router.Add(fiber.MethodGet, "/sonyFlake", func(ctx *fiber.Ctx) error {
|
||||||
|
id, err := global.SonyFlake.NextID()
|
||||||
|
|
||||||
|
if err = errorx.ParseError(err); err != nil {
|
||||||
|
return ctx.JSON(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(response.NewResponse(id))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// NanoIdTest nanoid 生成测试
|
||||||
|
//
|
||||||
|
// @Summary nanoid 生成测试
|
||||||
|
// @Description nanoid 生成测试
|
||||||
|
// @Tags Test
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} response.Response{data=string}
|
||||||
|
// @Failure default {object} errorx.CodeErrorResponse
|
||||||
|
// @Router /test/nanoid [get]
|
||||||
|
func (c *Controller) NanoIdTest() {
|
||||||
|
c.Router.Add(fiber.MethodGet, "/nanoid", func(ctx *fiber.Ctx) error {
|
||||||
|
id, err := global.GetNanoId()
|
||||||
|
|
||||||
|
if err = errorx.ParseError(err); err != nil {
|
||||||
|
return ctx.JSON(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(response.NewResponse(id))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
82
docs/docs.go
82
docs/docs.go
@ -490,6 +490,88 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/test/nanoid": {
|
||||||
|
"get": {
|
||||||
|
"description": "nanoid 生成测试",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Test"
|
||||||
|
],
|
||||||
|
"summary": "nanoid 生成测试",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/response.Response"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"description": "",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/test/sonyFlake": {
|
||||||
|
"get": {
|
||||||
|
"description": "sonyFlake id 生成测试",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Test"
|
||||||
|
],
|
||||||
|
"summary": "sonyFlake id 生成测试",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/response.Response"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"description": "",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/user/account": {
|
"/user/account": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"security": [
|
||||||
|
@ -482,6 +482,88 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/test/nanoid": {
|
||||||
|
"get": {
|
||||||
|
"description": "nanoid 生成测试",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Test"
|
||||||
|
],
|
||||||
|
"summary": "nanoid 生成测试",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/response.Response"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"description": "",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/test/sonyFlake": {
|
||||||
|
"get": {
|
||||||
|
"description": "sonyFlake id 生成测试",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Test"
|
||||||
|
],
|
||||||
|
"summary": "sonyFlake id 生成测试",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/response.Response"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"description": "",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/user/account": {
|
"/user/account": {
|
||||||
"get": {
|
"get": {
|
||||||
"security": [
|
"security": [
|
||||||
|
@ -378,6 +378,54 @@ paths:
|
|||||||
summary: jwt token 生成测试
|
summary: jwt token 生成测试
|
||||||
tags:
|
tags:
|
||||||
- Test
|
- Test
|
||||||
|
/test/nanoid:
|
||||||
|
get:
|
||||||
|
consumes:
|
||||||
|
- application/json
|
||||||
|
description: nanoid 生成测试
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: OK
|
||||||
|
schema:
|
||||||
|
allOf:
|
||||||
|
- $ref: '#/definitions/response.Response'
|
||||||
|
- properties:
|
||||||
|
data:
|
||||||
|
type: string
|
||||||
|
type: object
|
||||||
|
default:
|
||||||
|
description: ""
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/errorx.CodeErrorResponse'
|
||||||
|
summary: nanoid 生成测试
|
||||||
|
tags:
|
||||||
|
- Test
|
||||||
|
/test/sonyFlake:
|
||||||
|
get:
|
||||||
|
consumes:
|
||||||
|
- application/json
|
||||||
|
description: sonyFlake id 生成测试
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: OK
|
||||||
|
schema:
|
||||||
|
allOf:
|
||||||
|
- $ref: '#/definitions/response.Response'
|
||||||
|
- properties:
|
||||||
|
data:
|
||||||
|
type: integer
|
||||||
|
type: object
|
||||||
|
default:
|
||||||
|
description: ""
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/errorx.CodeErrorResponse'
|
||||||
|
summary: sonyFlake id 生成测试
|
||||||
|
tags:
|
||||||
|
- Test
|
||||||
/user/account:
|
/user/account:
|
||||||
get:
|
get:
|
||||||
consumes:
|
consumes:
|
||||||
|
12
global/nanoid.go
Normal file
12
global/nanoid.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
gonanoid "github.com/matoous/go-nanoid"
|
||||||
|
"gofiber.study.skcks.cn/common/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
var NanoIdConfig *config.NanoIdConfig
|
||||||
|
|
||||||
|
func GetNanoId() (string, error) {
|
||||||
|
return gonanoid.Generate(NanoIdConfig.Sequence, NanoIdConfig.Length)
|
||||||
|
}
|
5
global/sonyflake.go
Normal file
5
global/sonyflake.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import "github.com/sony/sonyflake"
|
||||||
|
|
||||||
|
var SonyFlake *sonyflake.Sonyflake
|
4
go.mod
4
go.mod
@ -11,6 +11,9 @@ require (
|
|||||||
github.com/goccy/go-json v0.8.1
|
github.com/goccy/go-json v0.8.1
|
||||||
github.com/gofiber/fiber/v2 v2.38.1
|
github.com/gofiber/fiber/v2 v2.38.1
|
||||||
github.com/gofiber/swagger v0.1.2
|
github.com/gofiber/swagger v0.1.2
|
||||||
|
github.com/golang-jwt/jwt/v4 v4.4.2
|
||||||
|
github.com/matoous/go-nanoid v1.5.0
|
||||||
|
github.com/sony/sonyflake v1.1.0
|
||||||
github.com/spf13/viper v1.13.0
|
github.com/spf13/viper v1.13.0
|
||||||
github.com/swaggo/swag v1.8.5
|
github.com/swaggo/swag v1.8.5
|
||||||
go.uber.org/zap v1.23.0
|
go.uber.org/zap v1.23.0
|
||||||
@ -30,7 +33,6 @@ require (
|
|||||||
github.com/go-openapi/jsonreference v0.19.6 // indirect
|
github.com/go-openapi/jsonreference v0.19.6 // indirect
|
||||||
github.com/go-openapi/spec v0.20.4 // indirect
|
github.com/go-openapi/spec v0.20.4 // indirect
|
||||||
github.com/go-openapi/swag v0.19.15 // indirect
|
github.com/go-openapi/swag v0.19.15 // indirect
|
||||||
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
|
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
github.com/josharian/intern v1.0.0 // indirect
|
github.com/josharian/intern v1.0.0 // indirect
|
||||||
|
4
go.sum
4
go.sum
@ -372,6 +372,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=
|
||||||
@ -494,6 +496,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1
|
|||||||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||||
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
|
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
|
||||||
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
|
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
|
||||||
|
github.com/sony/sonyflake v1.1.0 h1:wnrEcL3aOkWmPlhScLEGAXKkLAIslnBteNUq4Bw6MM4=
|
||||||
|
github.com/sony/sonyflake v1.1.0/go.mod h1:LORtCywH/cq10ZbyfhKrHYgAUGH7mOBa76enV9txy/Y=
|
||||||
github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
|
github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
|
||||||
github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
|
github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
|
||||||
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
|
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
|
||||||
|
@ -3,8 +3,8 @@ package auth
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"gofiber.study.skcks.cn/common/errorx"
|
"gofiber.study.skcks.cn/common/errorx"
|
||||||
"gofiber.study.skcks.cn/dto"
|
|
||||||
"gofiber.study.skcks.cn/global"
|
"gofiber.study.skcks.cn/global"
|
||||||
|
"gofiber.study.skcks.cn/model/dto"
|
||||||
"gofiber.study.skcks.cn/model/generic/models"
|
"gofiber.study.skcks.cn/model/generic/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user