init: init refactor

This commit is contained in:
Blizzard
2026-04-27 00:02:18 +08:00
commit e515f6a287
360 changed files with 30713 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
Name: file.rpc
ListenOn: 0.0.0.0:9102
Etcd:
Hosts:
- 192.168.100.127:2379
Key: file.rpc
# MySQL
DB:
DataSource: root:root@tcp(192.168.100.127:3307)/sundynix_micro_go?charset=utf8mb4&parseTime=True&loc=Local
# MinIO
Minio:
Endpoint: 129.28.103.17:3407
AccessKeyId: qP5QXP3g6Axw1hkwX21Y
AccessKeySecret: sddT6J3S6yDn9m1wfth0pzelPg9KWmbHjMAUF5S9
BucketName: sundynix-plant
BucketUrl: https://res.sundynix.cn/sundynix-plant
UseSsl: false
+39
View File
@@ -0,0 +1,39 @@
package main
import (
"flag"
"fmt"
"sundynix-micro-go/app/file/rpc/file"
"sundynix-micro-go/app/file/rpc/internal/config"
"sundynix-micro-go/app/file/rpc/internal/server"
"sundynix-micro-go/app/file/rpc/internal/svc"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
var configFile = flag.String("f", "etc/file.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
file.RegisterFileServiceServer(grpcServer, server.NewFileServiceServer(ctx))
if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer)
}
})
defer s.Stop()
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
s.Start()
}
+439
View File
@@ -0,0 +1,439 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.11
// protoc v7.34.1
// source: pb/file.proto
package file
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
unsafe "unsafe"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type CommonResp struct {
state protoimpl.MessageState `protogen:"open.v1"`
Code int64 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
Msg string `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *CommonResp) Reset() {
*x = CommonResp{}
mi := &file_pb_file_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *CommonResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CommonResp) ProtoMessage() {}
func (x *CommonResp) ProtoReflect() protoreflect.Message {
mi := &file_pb_file_proto_msgTypes[0]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use CommonResp.ProtoReflect.Descriptor instead.
func (*CommonResp) Descriptor() ([]byte, []int) {
return file_pb_file_proto_rawDescGZIP(), []int{0}
}
func (x *CommonResp) GetCode() int64 {
if x != nil {
return x.Code
}
return 0
}
func (x *CommonResp) GetMsg() string {
if x != nil {
return x.Msg
}
return ""
}
type FileInfo struct {
state protoimpl.MessageState `protogen:"open.v1"`
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Url string `protobuf:"bytes,3,opt,name=url,proto3" json:"url,omitempty"`
Tag string `protobuf:"bytes,4,opt,name=tag,proto3" json:"tag,omitempty"`
Key string `protobuf:"bytes,5,opt,name=key,proto3" json:"key,omitempty"`
Suffix string `protobuf:"bytes,6,opt,name=suffix,proto3" json:"suffix,omitempty"`
Md5 string `protobuf:"bytes,7,opt,name=md5,proto3" json:"md5,omitempty"`
CreatedAt int64 `protobuf:"varint,8,opt,name=createdAt,proto3" json:"createdAt,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *FileInfo) Reset() {
*x = FileInfo{}
mi := &file_pb_file_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *FileInfo) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*FileInfo) ProtoMessage() {}
func (x *FileInfo) ProtoReflect() protoreflect.Message {
mi := &file_pb_file_proto_msgTypes[1]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use FileInfo.ProtoReflect.Descriptor instead.
func (*FileInfo) Descriptor() ([]byte, []int) {
return file_pb_file_proto_rawDescGZIP(), []int{1}
}
func (x *FileInfo) GetId() string {
if x != nil {
return x.Id
}
return ""
}
func (x *FileInfo) GetName() string {
if x != nil {
return x.Name
}
return ""
}
func (x *FileInfo) GetUrl() string {
if x != nil {
return x.Url
}
return ""
}
func (x *FileInfo) GetTag() string {
if x != nil {
return x.Tag
}
return ""
}
func (x *FileInfo) GetKey() string {
if x != nil {
return x.Key
}
return ""
}
func (x *FileInfo) GetSuffix() string {
if x != nil {
return x.Suffix
}
return ""
}
func (x *FileInfo) GetMd5() string {
if x != nil {
return x.Md5
}
return ""
}
func (x *FileInfo) GetCreatedAt() int64 {
if x != nil {
return x.CreatedAt
}
return 0
}
type GetFileByIdReq struct {
state protoimpl.MessageState `protogen:"open.v1"`
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *GetFileByIdReq) Reset() {
*x = GetFileByIdReq{}
mi := &file_pb_file_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *GetFileByIdReq) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetFileByIdReq) ProtoMessage() {}
func (x *GetFileByIdReq) ProtoReflect() protoreflect.Message {
mi := &file_pb_file_proto_msgTypes[2]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetFileByIdReq.ProtoReflect.Descriptor instead.
func (*GetFileByIdReq) Descriptor() ([]byte, []int) {
return file_pb_file_proto_rawDescGZIP(), []int{2}
}
func (x *GetFileByIdReq) GetId() string {
if x != nil {
return x.Id
}
return ""
}
type GetFileByIdResp struct {
state protoimpl.MessageState `protogen:"open.v1"`
File *FileInfo `protobuf:"bytes,1,opt,name=file,proto3" json:"file,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *GetFileByIdResp) Reset() {
*x = GetFileByIdResp{}
mi := &file_pb_file_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *GetFileByIdResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetFileByIdResp) ProtoMessage() {}
func (x *GetFileByIdResp) ProtoReflect() protoreflect.Message {
mi := &file_pb_file_proto_msgTypes[3]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetFileByIdResp.ProtoReflect.Descriptor instead.
func (*GetFileByIdResp) Descriptor() ([]byte, []int) {
return file_pb_file_proto_rawDescGZIP(), []int{3}
}
func (x *GetFileByIdResp) GetFile() *FileInfo {
if x != nil {
return x.File
}
return nil
}
type GetFilesByIdsReq struct {
state protoimpl.MessageState `protogen:"open.v1"`
Ids []string `protobuf:"bytes,1,rep,name=ids,proto3" json:"ids,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *GetFilesByIdsReq) Reset() {
*x = GetFilesByIdsReq{}
mi := &file_pb_file_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *GetFilesByIdsReq) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetFilesByIdsReq) ProtoMessage() {}
func (x *GetFilesByIdsReq) ProtoReflect() protoreflect.Message {
mi := &file_pb_file_proto_msgTypes[4]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetFilesByIdsReq.ProtoReflect.Descriptor instead.
func (*GetFilesByIdsReq) Descriptor() ([]byte, []int) {
return file_pb_file_proto_rawDescGZIP(), []int{4}
}
func (x *GetFilesByIdsReq) GetIds() []string {
if x != nil {
return x.Ids
}
return nil
}
type GetFilesByIdsResp struct {
state protoimpl.MessageState `protogen:"open.v1"`
Files []*FileInfo `protobuf:"bytes,1,rep,name=files,proto3" json:"files,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *GetFilesByIdsResp) Reset() {
*x = GetFilesByIdsResp{}
mi := &file_pb_file_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *GetFilesByIdsResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetFilesByIdsResp) ProtoMessage() {}
func (x *GetFilesByIdsResp) ProtoReflect() protoreflect.Message {
mi := &file_pb_file_proto_msgTypes[5]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetFilesByIdsResp.ProtoReflect.Descriptor instead.
func (*GetFilesByIdsResp) Descriptor() ([]byte, []int) {
return file_pb_file_proto_rawDescGZIP(), []int{5}
}
func (x *GetFilesByIdsResp) GetFiles() []*FileInfo {
if x != nil {
return x.Files
}
return nil
}
var File_pb_file_proto protoreflect.FileDescriptor
const file_pb_file_proto_rawDesc = "" +
"\n" +
"\rpb/file.proto\x12\x04file\"2\n" +
"\n" +
"CommonResp\x12\x12\n" +
"\x04code\x18\x01 \x01(\x03R\x04code\x12\x10\n" +
"\x03msg\x18\x02 \x01(\tR\x03msg\"\xac\x01\n" +
"\bFileInfo\x12\x0e\n" +
"\x02id\x18\x01 \x01(\tR\x02id\x12\x12\n" +
"\x04name\x18\x02 \x01(\tR\x04name\x12\x10\n" +
"\x03url\x18\x03 \x01(\tR\x03url\x12\x10\n" +
"\x03tag\x18\x04 \x01(\tR\x03tag\x12\x10\n" +
"\x03key\x18\x05 \x01(\tR\x03key\x12\x16\n" +
"\x06suffix\x18\x06 \x01(\tR\x06suffix\x12\x10\n" +
"\x03md5\x18\a \x01(\tR\x03md5\x12\x1c\n" +
"\tcreatedAt\x18\b \x01(\x03R\tcreatedAt\" \n" +
"\x0eGetFileByIdReq\x12\x0e\n" +
"\x02id\x18\x01 \x01(\tR\x02id\"5\n" +
"\x0fGetFileByIdResp\x12\"\n" +
"\x04file\x18\x01 \x01(\v2\x0e.file.FileInfoR\x04file\"$\n" +
"\x10GetFilesByIdsReq\x12\x10\n" +
"\x03ids\x18\x01 \x03(\tR\x03ids\"9\n" +
"\x11GetFilesByIdsResp\x12$\n" +
"\x05files\x18\x01 \x03(\v2\x0e.file.FileInfoR\x05files2\x8b\x01\n" +
"\vFileService\x12:\n" +
"\vGetFileById\x12\x14.file.GetFileByIdReq\x1a\x15.file.GetFileByIdResp\x12@\n" +
"\rGetFilesByIds\x12\x16.file.GetFilesByIdsReq\x1a\x17.file.GetFilesByIdsRespB\bZ\x06./fileb\x06proto3"
var (
file_pb_file_proto_rawDescOnce sync.Once
file_pb_file_proto_rawDescData []byte
)
func file_pb_file_proto_rawDescGZIP() []byte {
file_pb_file_proto_rawDescOnce.Do(func() {
file_pb_file_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_pb_file_proto_rawDesc), len(file_pb_file_proto_rawDesc)))
})
return file_pb_file_proto_rawDescData
}
var file_pb_file_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
var file_pb_file_proto_goTypes = []any{
(*CommonResp)(nil), // 0: file.CommonResp
(*FileInfo)(nil), // 1: file.FileInfo
(*GetFileByIdReq)(nil), // 2: file.GetFileByIdReq
(*GetFileByIdResp)(nil), // 3: file.GetFileByIdResp
(*GetFilesByIdsReq)(nil), // 4: file.GetFilesByIdsReq
(*GetFilesByIdsResp)(nil), // 5: file.GetFilesByIdsResp
}
var file_pb_file_proto_depIdxs = []int32{
1, // 0: file.GetFileByIdResp.file:type_name -> file.FileInfo
1, // 1: file.GetFilesByIdsResp.files:type_name -> file.FileInfo
2, // 2: file.FileService.GetFileById:input_type -> file.GetFileByIdReq
4, // 3: file.FileService.GetFilesByIds:input_type -> file.GetFilesByIdsReq
3, // 4: file.FileService.GetFileById:output_type -> file.GetFileByIdResp
5, // 5: file.FileService.GetFilesByIds:output_type -> file.GetFilesByIdsResp
4, // [4:6] is the sub-list for method output_type
2, // [2:4] is the sub-list for method input_type
2, // [2:2] is the sub-list for extension type_name
2, // [2:2] is the sub-list for extension extendee
0, // [0:2] is the sub-list for field type_name
}
func init() { file_pb_file_proto_init() }
func file_pb_file_proto_init() {
if File_pb_file_proto != nil {
return
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_pb_file_proto_rawDesc), len(file_pb_file_proto_rawDesc)),
NumEnums: 0,
NumMessages: 6,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_pb_file_proto_goTypes,
DependencyIndexes: file_pb_file_proto_depIdxs,
MessageInfos: file_pb_file_proto_msgTypes,
}.Build()
File_pb_file_proto = out.File
file_pb_file_proto_goTypes = nil
file_pb_file_proto_depIdxs = nil
}
+163
View File
@@ -0,0 +1,163 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.6.1
// - protoc v7.34.1
// source: pb/file.proto
package file
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.64.0 or later.
const _ = grpc.SupportPackageIsVersion9
const (
FileService_GetFileById_FullMethodName = "/file.FileService/GetFileById"
FileService_GetFilesByIds_FullMethodName = "/file.FileService/GetFilesByIds"
)
// FileServiceClient is the client API for FileService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type FileServiceClient interface {
// 根据ID获取文件信息
GetFileById(ctx context.Context, in *GetFileByIdReq, opts ...grpc.CallOption) (*GetFileByIdResp, error)
// 根据ID列表批量获取文件信息
GetFilesByIds(ctx context.Context, in *GetFilesByIdsReq, opts ...grpc.CallOption) (*GetFilesByIdsResp, error)
}
type fileServiceClient struct {
cc grpc.ClientConnInterface
}
func NewFileServiceClient(cc grpc.ClientConnInterface) FileServiceClient {
return &fileServiceClient{cc}
}
func (c *fileServiceClient) GetFileById(ctx context.Context, in *GetFileByIdReq, opts ...grpc.CallOption) (*GetFileByIdResp, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetFileByIdResp)
err := c.cc.Invoke(ctx, FileService_GetFileById_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *fileServiceClient) GetFilesByIds(ctx context.Context, in *GetFilesByIdsReq, opts ...grpc.CallOption) (*GetFilesByIdsResp, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetFilesByIdsResp)
err := c.cc.Invoke(ctx, FileService_GetFilesByIds_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// FileServiceServer is the server API for FileService service.
// All implementations must embed UnimplementedFileServiceServer
// for forward compatibility.
type FileServiceServer interface {
// 根据ID获取文件信息
GetFileById(context.Context, *GetFileByIdReq) (*GetFileByIdResp, error)
// 根据ID列表批量获取文件信息
GetFilesByIds(context.Context, *GetFilesByIdsReq) (*GetFilesByIdsResp, error)
mustEmbedUnimplementedFileServiceServer()
}
// UnimplementedFileServiceServer must be embedded to have
// forward compatible implementations.
//
// NOTE: this should be embedded by value instead of pointer to avoid a nil
// pointer dereference when methods are called.
type UnimplementedFileServiceServer struct{}
func (UnimplementedFileServiceServer) GetFileById(context.Context, *GetFileByIdReq) (*GetFileByIdResp, error) {
return nil, status.Error(codes.Unimplemented, "method GetFileById not implemented")
}
func (UnimplementedFileServiceServer) GetFilesByIds(context.Context, *GetFilesByIdsReq) (*GetFilesByIdsResp, error) {
return nil, status.Error(codes.Unimplemented, "method GetFilesByIds not implemented")
}
func (UnimplementedFileServiceServer) mustEmbedUnimplementedFileServiceServer() {}
func (UnimplementedFileServiceServer) testEmbeddedByValue() {}
// UnsafeFileServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to FileServiceServer will
// result in compilation errors.
type UnsafeFileServiceServer interface {
mustEmbedUnimplementedFileServiceServer()
}
func RegisterFileServiceServer(s grpc.ServiceRegistrar, srv FileServiceServer) {
// If the following call panics, it indicates UnimplementedFileServiceServer was
// embedded by pointer and is nil. This will cause panics if an
// unimplemented method is ever invoked, so we test this at initialization
// time to prevent it from happening at runtime later due to I/O.
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
t.testEmbeddedByValue()
}
s.RegisterService(&FileService_ServiceDesc, srv)
}
func _FileService_GetFileById_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetFileByIdReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(FileServiceServer).GetFileById(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: FileService_GetFileById_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(FileServiceServer).GetFileById(ctx, req.(*GetFileByIdReq))
}
return interceptor(ctx, in, info, handler)
}
func _FileService_GetFilesByIds_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetFilesByIdsReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(FileServiceServer).GetFilesByIds(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: FileService_GetFilesByIds_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(FileServiceServer).GetFilesByIds(ctx, req.(*GetFilesByIdsReq))
}
return interceptor(ctx, in, info, handler)
}
// FileService_ServiceDesc is the grpc.ServiceDesc for FileService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var FileService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "file.FileService",
HandlerType: (*FileServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetFileById",
Handler: _FileService_GetFileById_Handler,
},
{
MethodName: "GetFilesByIds",
Handler: _FileService_GetFilesByIds_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "pb/file.proto",
}
+52
View File
@@ -0,0 +1,52 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.10.1
// Source: file.proto
package fileservice
import (
"context"
"sundynix-micro-go/app/file/rpc/file"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
)
type (
CommonResp = file.CommonResp
FileInfo = file.FileInfo
GetFileByIdReq = file.GetFileByIdReq
GetFileByIdResp = file.GetFileByIdResp
GetFilesByIdsReq = file.GetFilesByIdsReq
GetFilesByIdsResp = file.GetFilesByIdsResp
FileService interface {
// 根据ID获取文件信息
GetFileById(ctx context.Context, in *GetFileByIdReq, opts ...grpc.CallOption) (*GetFileByIdResp, error)
// 根据ID列表批量获取文件信息
GetFilesByIds(ctx context.Context, in *GetFilesByIdsReq, opts ...grpc.CallOption) (*GetFilesByIdsResp, error)
}
defaultFileService struct {
cli zrpc.Client
}
)
func NewFileService(cli zrpc.Client) FileService {
return &defaultFileService{
cli: cli,
}
}
// 根据ID获取文件信息
func (m *defaultFileService) GetFileById(ctx context.Context, in *GetFileByIdReq, opts ...grpc.CallOption) (*GetFileByIdResp, error) {
client := file.NewFileServiceClient(m.cli.Conn())
return client.GetFileById(ctx, in, opts...)
}
// 根据ID列表批量获取文件信息
func (m *defaultFileService) GetFilesByIds(ctx context.Context, in *GetFilesByIdsReq, opts ...grpc.CallOption) (*GetFilesByIdsResp, error) {
client := file.NewFileServiceClient(m.cli.Conn())
return client.GetFilesByIds(ctx, in, opts...)
}
+18
View File
@@ -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,
}
}
+52
View File
@@ -0,0 +1,52 @@
syntax = "proto3";
package file;
option go_package = "./file";
// ---------- 通用消息 ----------
message CommonResp {
int64 code = 1;
string msg = 2;
}
// ---------- 文件信息 ----------
message FileInfo {
string id = 1;
string name = 2;
string url = 3;
string tag = 4;
string key = 5;
string suffix = 6;
string md5 = 7;
int64 createdAt = 8;
}
// ---------- 请求/响应 ----------
message GetFileByIdReq {
string id = 1;
}
message GetFileByIdResp {
FileInfo file = 1;
}
message GetFilesByIdsReq {
repeated string ids = 1;
}
message GetFilesByIdsResp {
repeated FileInfo files = 1;
}
// ---------- 服务定义 ----------
service FileService {
// 根据ID获取文件信息
rpc GetFileById(GetFileByIdReq) returns (GetFileByIdResp);
// 根据ID列表批量获取文件信息
rpc GetFilesByIds(GetFilesByIdsReq) returns (GetFilesByIdsResp);
}