Skip to content

Commit

Permalink
[feature] 新增版本检查api
Browse files Browse the repository at this point in the history
  • Loading branch information
runs committed Jan 22, 2023
1 parent 9857e61 commit 2f00503
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 3 deletions.
20 changes: 20 additions & 0 deletions internal/app/api/handler/tools.go
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))
}
}
}
42 changes: 42 additions & 0 deletions internal/app/api/idl/system_config.go
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"`
}
7 changes: 7 additions & 0 deletions internal/app/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func InitRouters(
bvService *service.BilbilVideo,
authService *service.Auth,
userService *service.User,
toolService *service.Tool,
errMiddlewares *middlewares.ErrorInterceptor,
sessionMiddlewares *middlewares.Session,
) httpserver.InitRouters {
Expand All @@ -42,5 +43,11 @@ func InitRouters(
usersApi.GET("/info", handler.UserInfo(userService))
usersApi.POST("/update", handler.UserUpdate(userService))
}

toolsApi := r.Group("/v1/tools")
{
// 获取版本号
toolsApi.GET("/version", handler.GetVersion(toolService))
}
}
}
48 changes: 48 additions & 0 deletions internal/app/api/service/tools.go
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
}
1 change: 1 addition & 0 deletions internal/app/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ func ServiceProvider() fx.Option {
service.NewBilbilVideo,
service.NewAuth,
service.NewUser,
service.NewTool,
)
}
3 changes: 2 additions & 1 deletion internal/app/spider/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ func (u *Update) spider() error {
}

for _, video := range list {
time.Sleep(400 * time.Millisecond)
time.Sleep(300 * time.Millisecond)
// 获取视频信息
vInfo, err := u.sdk.VideoWebInfo(video.Bvid)
if err != nil {
if bErr, ok := err.(*bilibili.Error); ok {
//bErr.Code == 62022 // 视频被删除
u.logger.Warn("VideoWebInfo error", zap.String("bvid", video.Bvid), zap.Int("code", bErr.Code), zap.String("message", bErr.Message))
} else {
u.logger.Error("VideoWebInfo error", zap.String("bvid", video.Bvid), zap.Error(err))
Expand Down
2 changes: 1 addition & 1 deletion internal/app/spider/video_analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewAnalysis(logger *zap.Logger, scores ...Score) *Analysis {
analysis.scoreMap[score.GetKeyType()] = score
}

go analysis.tick(time.NewTicker(10 * time.Minute))
go analysis.tick(time.NewTicker(1 * time.Minute))
return analysis
}

Expand Down
4 changes: 4 additions & 0 deletions internal/app/spider/video_analysis/blacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func NewBlacklist(db *gorm.DB) (*Blacklist, error) {
blacklistMap: map[string]int{},
}

if err := blacklist.init(); err != nil {
return nil, err
}

return blacklist, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/repository/bilbil_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
bilibiliVideoTableName = "bilbil_asoul_video"
bilibiliVideoTableName = "bilbil_video"
bilibiliVideoTagTableName = "bilbil_video_tag"

preAliasString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand Down
32 changes: 32 additions & 0 deletions internal/repository/system_config.go
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
}

0 comments on commit 2f00503

Please sign in to comment.