diff --git a/app/app.go b/app/app.go index d1b5ec6..6873230 100644 --- a/app/app.go +++ b/app/app.go @@ -26,6 +26,8 @@ func Run() { reloadRedis(global.Config) reloadDataSources(global.Config) reloadJwt(global.Config) + reloadSonyFlake(global.Config) + reloadNanoId(global.Config) global.App = reloadApp(global.Config) diff --git a/app/nanoid.go b/app/nanoid.go new file mode 100644 index 0000000..5bbb04d --- /dev/null +++ b/app/nanoid.go @@ -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] 配置完成") + }) +} diff --git a/app/sonyflake.go b/app/sonyflake.go new file mode 100644 index 0000000..41dced3 --- /dev/null +++ b/app/sonyflake.go @@ -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 生成器 初始化 完成") + }) +} diff --git a/common/config/config.go b/common/config/config.go index 502eaf6..8fa4d12 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -5,6 +5,12 @@ type BasicConfig struct { Mysql MysqlConfig `yaml:"mysql"` Redis RedisConfig `yaml:"redis"` Jwt JwtConfig `yaml:"jwt"` + NanoId NanoIdConfig `yaml:"nanoId"` +} + +type NanoIdConfig struct { + Sequence string `yaml:"sequence"` + Length int `yaml:"length"` } type JwtConfig struct { diff --git a/config.yaml b/config.yaml index 9f5818f..cbb431e 100644 --- a/config.yaml +++ b/config.yaml @@ -29,3 +29,7 @@ redis: addr: "10.10.10.100:16379" pass: "12341234" db: 0 + +nanoId: + sequence: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ + length: 36 \ No newline at end of file diff --git a/controller/auth/auth.go b/controller/auth/auth.go index 8c5fd56..e3d4e4b 100644 --- a/controller/auth/auth.go +++ b/controller/auth/auth.go @@ -5,7 +5,7 @@ import ( "gofiber.study.skcks.cn/common/errorx" "gofiber.study.skcks.cn/common/response" "gofiber.study.skcks.cn/controller/types" - "gofiber.study.skcks.cn/dto" + "gofiber.study.skcks.cn/model/dto" "gofiber.study.skcks.cn/services/auth" ) diff --git a/controller/test/test.go b/controller/test/test.go index b990d8d..fe87568 100644 --- a/controller/test/test.go +++ b/controller/test/test.go @@ -75,3 +75,47 @@ func (c *Controller) JwtDecode() { 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)) + }) +} diff --git a/docs/docs.go b/docs/docs.go index f755b7d..a44ded0 100644 --- a/docs/docs.go +++ b/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": { "get": { "security": [ diff --git a/docs/swagger.json b/docs/swagger.json index f0b2bc6..e200d70 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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": { "get": { "security": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 369ec7a..f5d40b3 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -378,6 +378,54 @@ paths: summary: jwt token 生成测试 tags: - 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: get: consumes: diff --git a/global/nanoid.go b/global/nanoid.go new file mode 100644 index 0000000..67d4408 --- /dev/null +++ b/global/nanoid.go @@ -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) +} diff --git a/global/sonyflake.go b/global/sonyflake.go new file mode 100644 index 0000000..93962e9 --- /dev/null +++ b/global/sonyflake.go @@ -0,0 +1,5 @@ +package global + +import "github.com/sony/sonyflake" + +var SonyFlake *sonyflake.Sonyflake diff --git a/go.mod b/go.mod index 31a09e5..663ab80 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,9 @@ require ( github.com/goccy/go-json v0.8.1 github.com/gofiber/fiber/v2 v2.38.1 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/swaggo/swag v1.8.5 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/spec v0.20.4 // 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/hashicorp/hcl v1.0.0 // indirect github.com/josharian/intern v1.0.0 // indirect diff --git a/go.sum b/go.sum index 5ead515..a424cab 100644 --- a/go.sum +++ b/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.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= 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.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= 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/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/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/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo= github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= diff --git a/dto/auth.go b/model/dto/auth.go similarity index 100% rename from dto/auth.go rename to model/dto/auth.go diff --git a/services/auth/auth.go b/services/auth/auth.go index 6cbe70c..bdd7e22 100644 --- a/services/auth/auth.go +++ b/services/auth/auth.go @@ -3,8 +3,8 @@ package auth import ( "errors" "gofiber.study.skcks.cn/common/errorx" - "gofiber.study.skcks.cn/dto" "gofiber.study.skcks.cn/global" + "gofiber.study.skcks.cn/model/dto" "gofiber.study.skcks.cn/model/generic/models" )