feat: import services and standardize Go 1.26.5
This commit is contained in:
43
apps/base/ads/internal/config/config.go
Normal file
43
apps/base/ads/internal/config/config.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
"git.apinb.com/bsm-sdk/core/crypto/encipher"
|
||||
"git.apinb.com/bsm-sdk/core/env"
|
||||
)
|
||||
|
||||
var (
|
||||
Spec SrvConfig // 全局服务配置实例
|
||||
)
|
||||
|
||||
// SrvConfig 服务配置结构体
|
||||
type SrvConfig struct {
|
||||
conf.Base `yaml:",inline"` // 基础配置(服务名、端口、缓存等)
|
||||
Databases *conf.DBConf `yaml:"Databases"` // 数据库配置
|
||||
MicroService *conf.MicroServiceConf `yaml:"MicroService"` // 微服务配置
|
||||
Rpc map[string]conf.RpcConf `yaml:"Rpc"` // RPC服务配置
|
||||
Gateway *conf.GatewayConf `yaml:"Gateway"` // 网关配置
|
||||
Apm *conf.ApmConf `yaml:"APM"` // APM监控配置
|
||||
Etcd *conf.EtcdConf `yaml:"Etcd"` // Etcd配置
|
||||
}
|
||||
|
||||
// New 初始化服务配置
|
||||
func New(srvKey string) {
|
||||
// 初始化配置 创建一个新的配置实例,用于服务配置
|
||||
conf.New(srvKey, &Spec)
|
||||
|
||||
// 配置校验 服务IP,端口; 端口如果不合规,则随机分配端口
|
||||
Spec.Port = conf.CheckPort(Spec.Port)
|
||||
Spec.BindIP = conf.CheckIP(Spec.BindIP)
|
||||
Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port)
|
||||
|
||||
// 配置校验 服务名称地址及监听地址不能为空
|
||||
conf.NotNil(Spec.Service, Spec.Cache)
|
||||
|
||||
// 初始化加密SecretKey
|
||||
encipher.New(env.Runtime.JwtSecretKey)
|
||||
|
||||
conf.PrintInfo(Spec.Addr)
|
||||
}
|
||||
29
apps/base/ads/internal/impl/impl.go
Normal file
29
apps/base/ads/internal/impl/impl.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-apps/ads/internal/config"
|
||||
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||
"git.apinb.com/bsm-sdk/core/with"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisService *redis.RedisClient // Redis缓存服务客户端
|
||||
EtcdService *clientv3.Client // Etcd服务发现客户端
|
||||
DBService *gorm.DB // 数据库服务客户端
|
||||
MemorySerice *cache.Cache // 内存缓存服务
|
||||
)
|
||||
|
||||
// NewImpl 初始化各类服务实例
|
||||
func NewImpl() {
|
||||
// 初始化内存缓存服务
|
||||
MemorySerice = with.Memory(nil)
|
||||
// 初始化Redis缓存服务
|
||||
RedisService = with.RedisCache(config.Spec.Cache)
|
||||
// 初始化数据库服务
|
||||
DBService = with.Databases(config.Spec.Databases, nil)
|
||||
// 初始化Etcd服务发现
|
||||
EtcdService = with.Etcd(config.Spec.Etcd)
|
||||
}
|
||||
40
apps/base/ads/internal/logic/fetch/by_pos.go
Normal file
40
apps/base/ads/internal/logic/fetch/by_pos.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package fetch
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-apps/ads/internal/impl"
|
||||
"git.apinb.com/bsm-apps/ads/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/ads/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
)
|
||||
|
||||
// 通过广告位获取广告信息
|
||||
func ByPos(ctx context.Context, in *pb.ByPosRequest) (reply *pb.ByPosReply, err error) {
|
||||
// 参数验证
|
||||
if in.Key == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 从数据库查询
|
||||
var adsItems []models.AdsItem
|
||||
err = impl.DBService.Where("pos_key = ? AND status = ?", in.Key, 1).Find(&adsItems).Error
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 转换为protobuf格式
|
||||
result := make([]*pb.AdsItem, 0, len(adsItems))
|
||||
for _, item := range adsItems {
|
||||
result = append(result, &pb.AdsItem{
|
||||
Id: int64(item.ID),
|
||||
Title: item.Title,
|
||||
Content: item.Content,
|
||||
Type: int32(item.Type),
|
||||
ToUrl: item.ToUrl,
|
||||
Created: item.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.ByPosReply{Data: result}, nil
|
||||
}
|
||||
44
apps/base/ads/internal/models/ads_item.go
Normal file
44
apps/base/ads/internal/models/ads_item.go
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @Author: ZhaoYadong
|
||||
* @Date: 2021-12-13 14:28:49
|
||||
* @LastEditors: ZhaoYadong
|
||||
* @LastEditTime: 2022-01-18 17:57:50
|
||||
* @FilePath: /src/git.buka.tv/cloud-disk/internal/models/file.go
|
||||
*/
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/engine/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ContentType int32
|
||||
|
||||
const (
|
||||
CONTENT_TYPE_TEXT ContentType = iota + 1 // 1.文本
|
||||
CONTENT_TYPE_IMAGE // 2.图片
|
||||
CONTENT_TYPE_VIDEO // 3.视频
|
||||
CONTENT_TYPE_AUDIO // 4.音频
|
||||
CONTENT_TYPE_LINK // 5.连接
|
||||
CONTENT_TYPE_ATTACHMENT // 6.附件
|
||||
)
|
||||
|
||||
type AdsItem struct {
|
||||
gorm.Model
|
||||
Title string `gorm:"column:title;type:varchar(255);not null;" json:"title"` // 广告名称
|
||||
PosKey string `gorm:"column:pos_key;type:varchar(255);not null;" json:"pos_key"` // 广告位key
|
||||
Content string `gorm:"column:content;type:varchar(255);default:'';" json:"content"` // 广告内容
|
||||
Type ContentType `gorm:"column:type;default:0;" json:"type"` // 广告类型
|
||||
ToUrl string `gorm:"column:to_url;type:varchar(255);default:'';" json:"to_url"`
|
||||
types.Std_Status
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&AdsItem{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *AdsItem) TableName() string {
|
||||
return "ads_item" //对应数据库表名
|
||||
}
|
||||
28
apps/base/ads/internal/models/ads_pos.go
Normal file
28
apps/base/ads/internal/models/ads_pos.go
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @Author: ZhaoYadong
|
||||
* @Date: 2021-12-13 14:28:49
|
||||
* @LastEditors: ZhaoYadong
|
||||
* @LastEditTime: 2022-01-18 16:55:00
|
||||
* @FilePath: /src/git.buka.tv/cloud-disk/internal/models/share.go
|
||||
*/
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/engine/types"
|
||||
)
|
||||
|
||||
type AdsPos struct {
|
||||
types.Std_IdCreated
|
||||
Key string `gorm:"column:key;type:varchar(255);not null;" json:"key"` // 广告位的标记
|
||||
Name string `gorm:"column:name;type:varchar(255);not null;" json:"name"` // 广告位的名称
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&AdsPos{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *AdsPos) TableName() string {
|
||||
return "ads_pos" //对应数据库表名
|
||||
}
|
||||
21
apps/base/ads/internal/server/fetch_server.go
Normal file
21
apps/base/ads/internal/server/fetch_server.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.apinb.com/bsm-apps/ads/internal/logic/fetch"
|
||||
pb "git.apinb.com/bsm-apps/ads/pb"
|
||||
)
|
||||
|
||||
type FetchServer struct {
|
||||
pb.UnimplementedFetchServer
|
||||
}
|
||||
|
||||
func NewFetchServer() *FetchServer {
|
||||
return &FetchServer{}
|
||||
}
|
||||
|
||||
// 通过广告位获取广告信息
|
||||
func (s *FetchServer) ByPos(ctx context.Context, in *pb.ByPosRequest) (*pb.ByPosReply, error) {
|
||||
return fetch.ByPos(ctx, in)
|
||||
}
|
||||
91
apps/base/ads/internal/server/new.go
Normal file
91
apps/base/ads/internal/server/new.go
Normal file
@@ -0,0 +1,91 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
pb "git.apinb.com/bsm-apps/ads/pb"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Grpc *grpc.Server
|
||||
Ctx context.Context
|
||||
Mux *gwRuntime.ServeMux
|
||||
grpcConns map[string]*grpc.ClientConn // 连接池
|
||||
}
|
||||
|
||||
func New(addr string) *Server {
|
||||
srv := &Server{
|
||||
Ctx: context.Background(),
|
||||
Grpc: grpc.NewServer(),
|
||||
Mux: gwRuntime.NewServeMux(gwRuntime.WithForwardResponseRewriter(responseEnvelope)),
|
||||
grpcConns: make(map[string]*grpc.ClientConn),
|
||||
}
|
||||
|
||||
// register service to grpc.Server
|
||||
pb.RegisterFetchServer(srv.Grpc, NewFetchServer())
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 连接池: 只创建一次连接并复用
|
||||
conn, ok := srv.grpcConns[addr]
|
||||
if !ok {
|
||||
var err error
|
||||
conn, err = grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
panic("failed to dial grpc server: " + err.Error())
|
||||
}
|
||||
srv.grpcConns[addr] = conn
|
||||
}
|
||||
|
||||
// 将服务注册到Gateway
|
||||
|
||||
if err := pb.RegisterFetchHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Fetch handler: " + err.Error())
|
||||
}
|
||||
|
||||
// Register services swagger
|
||||
srv.RegisterSwagger()
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
// RegisterSwagger 注册swagger
|
||||
func (s *Server) RegisterSwagger() {
|
||||
srvKey := strings.ToLower(vars.ServiceKey)
|
||||
s.Mux.HandlePath("GET", "/"+srvKey+".swagger.json", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
bytes, err := os.ReadFile("./swagger/" + srvKey + ".swagger.json")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
w.Write(bytes)
|
||||
return
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// response envelope
|
||||
func responseEnvelope(_ context.Context, response proto.Message) (interface{}, error) {
|
||||
name := string(response.ProtoReflect().Descriptor().Name())
|
||||
if name == "Status" || name == "Error" || name == "StatusReply" {
|
||||
return response, nil
|
||||
}
|
||||
return map[string]any{
|
||||
"code": 0,
|
||||
"message": vars.OK,
|
||||
"details": response,
|
||||
"timeseq": time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user