From ed7cfb418c90c2821dda7878a076661df685bc2d Mon Sep 17 00:00:00 2001 From: viletyy Date: Sun, 13 Jun 2021 22:03:43 +0800 Subject: [PATCH] add: app info --- config.yaml.example | 3 ++- config/app.go | 5 +++-- internal/middleware/app_info.go | 20 ++++++++++++++++++++ internal/routers/router.go | 3 ++- pkg/app/jwt.go | 4 ++-- 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 internal/middleware/app_info.go diff --git a/config.yaml.example b/config.yaml.example index c001bbe..f4574a6 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -1,6 +1,7 @@ app: + name: 'potato' + version: '1.0.0' jwt_secret: 'viletyy' - jwt_issuer: 'potato' jwt_expire: '7200' run_mode: 'debug' page_size: 10 diff --git a/config/app.go b/config/app.go index 69d14a4..b1734d5 100644 --- a/config/app.go +++ b/config/app.go @@ -1,15 +1,16 @@ /* * @Date: 2021-03-22 09:46:19 * @LastEditors: viletyy - * @LastEditTime: 2021-06-11 16:40:34 + * @LastEditTime: 2021-06-13 22:00:01 * @FilePath: /potato/config/app.go */ package config type App struct { + Name string `mapstructure:"name" json:"name" yaml:"name"` + Version string `mapstructure:"version" json:"version" yaml:"version"` PageSize int64 `mapstructure:"page_size" json:"page_size" yaml:"page_size"` JwtSecret string `mapstructure:"jwt_secret" json:"jwt_secret" yaml:"jwt_secret"` - JwtIssuser string `mapstructure:"jwt_issuser" json:"jwt_issuser" yaml:"jwt_issuser"` JwtExpire int64 `mapstructure:"jwt_expire" json:"jwt_expire" yaml:"jwt_expire"` RunMode string `mapstructure:"run_mode" json:"run_mode" yaml:"run_mode"` UploadSavePath string `mapstructure:"upload_save_path" json:"upload_save_path" yaml:"upload_save_path"` diff --git a/internal/middleware/app_info.go b/internal/middleware/app_info.go new file mode 100644 index 0000000..dccd049 --- /dev/null +++ b/internal/middleware/app_info.go @@ -0,0 +1,20 @@ +/* + * @Date: 2021-06-13 22:01:30 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-13 22:02:41 + * @FilePath: /potato/internal/middleware/app_info.go + */ +package middleware + +import ( + "github.com/gin-gonic/gin" + "github.com/viletyy/potato/global" +) + +func AppInfo() gin.HandlerFunc { + return func(c *gin.Context) { + c.Set("app_name", global.GO_CONFIG.App.Name) + c.Set("app_version", global.GO_CONFIG.App.Version) + c.Next() + } +} diff --git a/internal/routers/router.go b/internal/routers/router.go index c008499..438a732 100644 --- a/internal/routers/router.go +++ b/internal/routers/router.go @@ -1,7 +1,7 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-06-12 22:24:53 + * @LastEditTime: 2021-06-13 22:03:27 * @FilePath: /potato/internal/routers/router.go */ package routers @@ -35,6 +35,7 @@ func InitRouter() *gin.Engine { Engine.Use(middleware.CORS()) Engine.Use(middleware.AccessLog()) + Engine.Use(middleware.AppInfo()) Engine.StaticFS("/static", http.Dir(global.GO_CONFIG.App.UploadSavePath)) Engine.POST("/api/v1/auth", v1.GetAuth) diff --git a/pkg/app/jwt.go b/pkg/app/jwt.go index af0338f..2031b5a 100644 --- a/pkg/app/jwt.go +++ b/pkg/app/jwt.go @@ -1,7 +1,7 @@ /* * @Date: 2021-06-10 18:25:19 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 22:23:51 + * @LastEditTime: 2021-06-13 22:00:15 * @FilePath: /potato/pkg/app/jwt.go */ package app @@ -33,7 +33,7 @@ func GenerateToken(appKey, appSecret string) (string, error) { AppSecret: crypt.Md5Encode(appSecret), StandardClaims: jwt.StandardClaims{ ExpiresAt: expireTime.Unix(), - Issuer: global.GO_CONFIG.App.JwtIssuser, + Issuer: global.GO_CONFIG.App.Name, }, }