-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
158 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "git.vtb.link/eoefans/internal/app/api/help" | ||
| "git.vtb.link/eoefans/internal/app/api/service" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func GetVersion(s *service.Tool) func(ctx *gin.Context) { | ||
| return func(ctx *gin.Context) { | ||
| if resp, err := s.GetVersion(ctx); err != nil { | ||
| _ = ctx.Error(err) | ||
| return | ||
| } else { | ||
| ctx.JSON(http.StatusOK, help.SuccessJson(resp)) | ||
| } | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package idl | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| ) | ||
|
|
||
| // SystemConfig 系统配置 | ||
| type SystemConfig struct { | ||
| ID int64 `json:"id"` | ||
| Key string `json:"key"` | ||
| Value json.RawMessage `json:"value"` | ||
| Comment string `json:"comment"` | ||
| } | ||
|
|
||
| func (s SystemConfig) UnmarshalValue(v interface{}) error { | ||
| return json.Unmarshal(s.Value, v) | ||
| } | ||
|
|
||
| func (s *SystemConfig) MarshalValue(v interface{}) error { | ||
| if b, err := json.Marshal(v); err != nil { | ||
| return err | ||
| } else { | ||
| s.Value = b | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type SystemConfigRepository interface { | ||
| // GetSystemConfig 获取系统配置 | ||
| GetSystemConfig(key string) (*SystemConfig, error) | ||
| } | ||
|
|
||
| const ( | ||
| SysKeyVersion = "version" | ||
| ) | ||
|
|
||
| type SysVersionConfig struct { | ||
| Version string `json:"version"` | ||
| VersionMessage string `json:"version_message"` | ||
| DownloadURL map[string]string `json:"download_url"` | ||
| UpdatedAt string `json:"updated_at"` | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "git.vtb.link/eoefans/internal/app/api/apperrors" | ||
| "git.vtb.link/eoefans/internal/app/api/idl" | ||
| "git.vtb.link/eoefans/internal/pkg/cache" | ||
| "git.vtb.link/eoefans/internal/repository" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| type Tool struct { | ||
| db *gorm.DB | ||
| iCache cache.ICache | ||
| } | ||
|
|
||
| func NewTool(db *gorm.DB, iCache cache.ICache) *Tool { | ||
| return &Tool{ | ||
| db: db, | ||
| iCache: iCache, | ||
| } | ||
| } | ||
|
|
||
| func (s *Tool) GetVersion(ctx context.Context) (*idl.SysVersionConfig, error) { | ||
| if res, ok := s.iCache.Get(idl.SysKeyVersion); ok { | ||
| return res.(*idl.SysVersionConfig), nil | ||
| } | ||
|
|
||
| cfg, err := repository.NewSystemConfigRepository(s.db).GetSystemConfig(idl.SysKeyVersion) | ||
| if err != nil { | ||
| return nil, err | ||
| } else if cfg == nil { | ||
| return nil, apperrors.NewServiceError(4004, "version config not found") | ||
| } | ||
|
|
||
| var version *idl.SysVersionConfig | ||
| if err := cfg.UnmarshalValue(&version); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if err = s.iCache.Set(idl.SysKeyVersion, version, 5*time.Minute); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return version, nil | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package repository | ||
|
|
||
| import ( | ||
| "git.vtb.link/eoefans/internal/app/api/idl" | ||
| "github.com/pkg/errors" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| const ( | ||
| systemConfigTableName = "system_config" | ||
| ) | ||
|
|
||
| func NewSystemConfigRepository(tx *gorm.DB) idl.SystemConfigRepository { | ||
| return &SystemConfigMysqlImpl{tx: tx} | ||
| } | ||
|
|
||
| type SystemConfigMysqlImpl struct { | ||
| tx *gorm.DB | ||
| } | ||
|
|
||
| func (m *SystemConfigMysqlImpl) GetSystemConfig(key string) (*idl.SystemConfig, error) { | ||
| var config *idl.SystemConfig | ||
| result := m.tx.Table(systemConfigTableName).Where("`key` = ?", key).Take(&config) | ||
| if result.Error != nil { | ||
| if result.Error == gorm.ErrRecordNotFound { | ||
| return nil, nil | ||
| } | ||
| return nil, errors.Wrap(result.Error, "get system config error") | ||
| } | ||
|
|
||
| return config, nil | ||
| } |