refactor: reorganize modules and add Linux build tooling
This commit is contained in:
55
module/base/initial/internal/logic/check/config.go
Normal file
55
module/base/initial/internal/logic/check/config.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/initial/internal/models"
|
||||
pb "bsm/full/module/base/initial/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 获取应用的相关配置信息
|
||||
func Config(ctx context.Context, in *pb.ConfigRequest) (reply *pb.ConfigReply, err error) {
|
||||
// 输入验证
|
||||
if in == nil || in.App == "" || in.Os == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
var (
|
||||
configs []*models.InitialConfig
|
||||
data []*pb.ConfigItem
|
||||
)
|
||||
|
||||
// 查询配置,优先返回特定OS的配置,然后是通用配置
|
||||
configs, err = GetConfigByCache(in.App, in.Os)
|
||||
|
||||
if err != nil {
|
||||
printer.Error("GetConfigByCache", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 去重处理:同一个key只保留优先级最高的配置
|
||||
configMap := make(map[string]*models.InitialConfig)
|
||||
for _, item := range configs {
|
||||
if existing, exists := configMap[item.Key]; !exists || item.Os != "ALL" {
|
||||
configMap[item.Key] = item
|
||||
} else if existing.Os == "ALL" && item.Os != "ALL" {
|
||||
configMap[item.Key] = item
|
||||
}
|
||||
}
|
||||
|
||||
// 转换为响应格式
|
||||
for _, item := range configMap {
|
||||
data = append(data, &pb.ConfigItem{
|
||||
Identity: item.Identity,
|
||||
Key: item.Key,
|
||||
Value: item.Value,
|
||||
Version: item.Version,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.ConfigReply{
|
||||
Data: data,
|
||||
}, nil
|
||||
}
|
||||
29
module/base/initial/internal/logic/check/config_cache.go
Normal file
29
module/base/initial/internal/logic/check/config_cache.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"bsm/full/module/base/initial/internal/impl"
|
||||
"bsm/full/module/base/initial/internal/models"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// GetConfigCache 获取配置缓存
|
||||
func GetConfigByCache(app, os string) ([]*models.InitialConfig, error) {
|
||||
var configs []*models.InitialConfig
|
||||
|
||||
key := impl.RedisService.BuildKey("config", app, os)
|
||||
|
||||
err := impl.RedisService.Get(key, &configs)
|
||||
if err == nil {
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
err = impl.DBService.Where("app = ? AND (os = ? OR os = ?)", app, os, "ALL").
|
||||
Order("CASE WHEN os = '" + os + "' THEN 0 ELSE 1 END, id DESC").
|
||||
Find(&configs).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = impl.RedisService.Set(key, configs, vars.DefaultTTL)
|
||||
return configs, err
|
||||
}
|
||||
32
module/base/initial/internal/logic/check/hello.go
Normal file
32
module/base/initial/internal/logic/check/hello.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
pb "bsm/full/module/base/initial/pb"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// HELLO - 健康检查接口
|
||||
func Hello(ctx context.Context, in *pb.Crc) (reply *pb.StatusReply, err error) {
|
||||
// 输入验证
|
||||
if in.Code != "" {
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: vars.OK,
|
||||
Details: "Hello " + in.Code,
|
||||
Timeseq: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 可以在这里添加更多的健康检查逻辑
|
||||
// 例如检查数据库连接、Redis连接等
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: vars.OK,
|
||||
Details: "",
|
||||
Timeseq: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
55
module/base/initial/internal/logic/check/updates.go
Normal file
55
module/base/initial/internal/logic/check/updates.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/initial/internal/impl"
|
||||
"bsm/full/module/base/initial/internal/models"
|
||||
pb "bsm/full/module/base/initial/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 检查更新
|
||||
func Updates(ctx context.Context, in *pb.CheckForUpdatesRequest) (reply *pb.CheckForUpdatesReply, err error) {
|
||||
// 输入验证
|
||||
if in == nil || in.App == "" || in.Os == "" || in.Arch == "" || in.Version == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
var data models.InitialApps
|
||||
|
||||
// 查询最新版本
|
||||
err = impl.DBService.Where("app = ? AND os = ? AND arch = ?", in.App, in.Os, in.Arch).
|
||||
Order("pubdate DESC, id DESC").
|
||||
First(&data).Error
|
||||
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
// 没有找到对应的应用版本
|
||||
return &pb.CheckForUpdatesReply{
|
||||
Identity: "0",
|
||||
Version: "0",
|
||||
}, nil
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 比较版本号
|
||||
if in.Version != data.Version {
|
||||
// 有新版本可用
|
||||
return &pb.CheckForUpdatesReply{
|
||||
Identity: data.Identity,
|
||||
Version: data.Version,
|
||||
Summary: data.Summary,
|
||||
Files: data.Files,
|
||||
Pubdate: data.Pubdate.Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 当前版本是最新的
|
||||
return &pb.CheckForUpdatesReply{
|
||||
Identity: data.Identity,
|
||||
Version: data.Version,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user