init: init refactor
This commit is contained in:
Executable
+18
@@ -0,0 +1,18 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
DB struct {
|
||||
DataSource string
|
||||
}
|
||||
Minio struct {
|
||||
Endpoint string
|
||||
AccessKeyId string
|
||||
AccessKeySecret string
|
||||
BucketName string
|
||||
BucketUrl string
|
||||
UseSsl bool
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
fileModel "sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GetFileByIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetFileByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileByIdLogic {
|
||||
return &GetFileByIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID获取文件信息
|
||||
func (l *GetFileByIdLogic) GetFileById(in *file.GetFileByIdReq) (*file.GetFileByIdResp, error) {
|
||||
var oss fileModel.SundynixOss
|
||||
err := l.svcCtx.DB.Where("id = ?", in.Id).First(&oss).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, status.Error(codes.NotFound, "文件不存在")
|
||||
}
|
||||
l.Errorf("查询文件失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询文件失败")
|
||||
}
|
||||
|
||||
return &file.GetFileByIdResp{
|
||||
File: convertOssToProto(&oss),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func convertOssToProto(oss *fileModel.SundynixOss) *file.FileInfo {
|
||||
return &file.FileInfo{
|
||||
Id: oss.ID,
|
||||
Name: oss.Name,
|
||||
Url: oss.Url,
|
||||
Tag: oss.Tag,
|
||||
Key: oss.Key,
|
||||
Suffix: oss.Suffix,
|
||||
Md5: oss.MD5,
|
||||
CreatedAt: oss.CreatedAt.Unix(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
fileModel "sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type GetFilesByIdsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetFilesByIdsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFilesByIdsLogic {
|
||||
return &GetFilesByIdsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID列表批量获取文件信息
|
||||
func (l *GetFilesByIdsLogic) GetFilesByIds(in *file.GetFilesByIdsReq) (*file.GetFilesByIdsResp, error) {
|
||||
if len(in.Ids) == 0 {
|
||||
return &file.GetFilesByIdsResp{Files: []*file.FileInfo{}}, nil
|
||||
}
|
||||
|
||||
var ossList []fileModel.SundynixOss
|
||||
err := l.svcCtx.DB.Where("id IN ?", in.Ids).Find(&ossList).Error
|
||||
if err != nil {
|
||||
l.Errorf("批量查询文件失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "批量查询文件失败")
|
||||
}
|
||||
|
||||
files := make([]*file.FileInfo, 0, len(ossList))
|
||||
for _, oss := range ossList {
|
||||
files = append(files, convertOssToProto(&oss))
|
||||
}
|
||||
|
||||
return &file.GetFilesByIdsResp{Files: files}, nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
// Source: file.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/logic"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type FileServiceServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
file.UnimplementedFileServiceServer
|
||||
}
|
||||
|
||||
func NewFileServiceServer(svcCtx *svc.ServiceContext) *FileServiceServer {
|
||||
return &FileServiceServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID获取文件信息
|
||||
func (s *FileServiceServer) GetFileById(ctx context.Context, in *file.GetFileByIdReq) (*file.GetFileByIdResp, error) {
|
||||
l := logic.NewGetFileByIdLogic(ctx, s.svcCtx)
|
||||
return l.GetFileById(in)
|
||||
}
|
||||
|
||||
// 根据ID列表批量获取文件信息
|
||||
func (s *FileServiceServer) GetFilesByIds(ctx context.Context, in *file.GetFilesByIdsReq) (*file.GetFilesByIdsResp, error) {
|
||||
l := logic.NewGetFilesByIdsLogic(ctx, s.svcCtx)
|
||||
return l.GetFilesByIds(in)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
fileModel "sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/internal/config"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
MinioClient *minio.Client
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
// 初始化GORM
|
||||
db, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{})
|
||||
if err != nil {
|
||||
logx.Errorf("连接数据库失败: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
// 自动迁移
|
||||
if err := db.AutoMigrate(&fileModel.SundynixOss{}); err != nil {
|
||||
logx.Errorf("数据库迁移失败: %v", err)
|
||||
}
|
||||
|
||||
// 初始化MinIO客户端
|
||||
minioClient, err := minio.New(c.Minio.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(c.Minio.AccessKeyId, c.Minio.AccessKeySecret, ""),
|
||||
Secure: c.Minio.UseSsl,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("初始化MinIO客户端失败: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
MinioClient: minioClient,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user