🎨 Update project structure
This commit is contained in:
70
pkg/internal/grpc/attachments.go
Normal file
70
pkg/internal/grpc/attachments.go
Normal file
@ -0,0 +1,70 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/proto"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/models"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func (v *Server) GetAttachment(ctx context.Context, request *proto.AttachmentLookupRequest) (*proto.Attachment, error) {
|
||||
var attachment models.Attachment
|
||||
|
||||
tx := database.C.Model(&models.Attachment{})
|
||||
if request.Id != nil {
|
||||
tx = tx.Where("id = ?", request.GetId())
|
||||
}
|
||||
if request.Uuid != nil {
|
||||
tx = tx.Where("uuid = ?", request.GetUuid())
|
||||
}
|
||||
if request.Usage != nil {
|
||||
tx = tx.Where("usage = ?", request.GetUsage())
|
||||
}
|
||||
|
||||
if err := tx.First(&attachment).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rawMetadata, _ := jsoniter.Marshal(attachment.Metadata)
|
||||
|
||||
return &proto.Attachment{
|
||||
Id: uint64(attachment.ID),
|
||||
Uuid: attachment.Uuid,
|
||||
Size: attachment.Size,
|
||||
Name: attachment.Name,
|
||||
Alt: attachment.Alternative,
|
||||
Usage: attachment.Usage,
|
||||
Mimetype: attachment.MimeType,
|
||||
Hash: attachment.HashCode,
|
||||
Destination: attachment.Destination,
|
||||
Metadata: rawMetadata,
|
||||
IsMature: attachment.IsMature,
|
||||
AccountId: uint64(attachment.AccountID),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *Server) CheckAttachmentExists(ctx context.Context, request *proto.AttachmentLookupRequest) (*emptypb.Empty, error) {
|
||||
tx := database.C.Model(&models.Attachment{})
|
||||
if request.Id != nil {
|
||||
tx = tx.Where("id = ?", request.GetId())
|
||||
}
|
||||
if request.Uuid != nil {
|
||||
tx = tx.Where("uuid = ?", request.GetUuid())
|
||||
}
|
||||
if request.Usage != nil {
|
||||
tx = tx.Where("usage = ?", request.GetUsage())
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := tx.Model(&models.Attachment{}).Count(&count).Error; err != nil {
|
||||
return nil, err
|
||||
} else if count == 0 {
|
||||
return nil, fmt.Errorf("record not found")
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
22
pkg/internal/grpc/client.go
Normal file
22
pkg/internal/grpc/client.go
Normal file
@ -0,0 +1,22 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
idpb "git.solsynth.dev/hydrogen/passport/pkg/grpc/proto"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var Auth idpb.AuthClient
|
||||
|
||||
func ConnectPassport() error {
|
||||
addr := viper.GetString("passport.grpc_endpoint")
|
||||
if conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
|
||||
return err
|
||||
} else {
|
||||
Auth = idpb.NewAuthClient(conn)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
29
pkg/internal/grpc/server.go
Normal file
29
pkg/internal/grpc/server.go
Normal file
@ -0,0 +1,29 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/proto"
|
||||
"net"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
proto.UnimplementedAttachmentsServer
|
||||
}
|
||||
|
||||
func StartGrpc() error {
|
||||
listen, err := net.Listen("tcp", viper.GetString("grpc_bind"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
server := grpc.NewServer()
|
||||
|
||||
proto.RegisterAttachmentsServer(server, &Server{})
|
||||
|
||||
reflection.Register(server)
|
||||
|
||||
return server.Serve(listen)
|
||||
}
|
Reference in New Issue
Block a user