✨ Drop direct connection and uses consul
This commit is contained in:
@ -4,10 +4,16 @@ import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/consul/api"
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
_ "github.com/mbobakov/grpc-consul-resolver"
|
||||
)
|
||||
|
||||
var C *api.Client
|
||||
|
||||
func Register() error {
|
||||
cfg := api.DefaultConfig()
|
||||
cfg.Address = viper.GetString("consul.addr")
|
||||
@ -35,5 +41,18 @@ func Register() error {
|
||||
DeregisterCriticalServiceAfter: "3m",
|
||||
}
|
||||
|
||||
return client.Agent().ServiceRegister(registration)
|
||||
if err := client.Agent().ServiceRegister(registration); err != nil {
|
||||
return err
|
||||
} else {
|
||||
C = client
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func DiscoverPaperclip() (*grpc.ClientConn, error) {
|
||||
return grpc.NewClient(
|
||||
"consul://127.0.0.1:8500/Hydrogen.Paperclip",
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
|
||||
)
|
||||
}
|
||||
|
@ -1,21 +0,0 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
pcpb "git.solsynth.dev/hydrogen/paperclip/pkg/grpc/proto"
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
var Attachments pcpb.AttachmentsClient
|
||||
|
||||
func ConnectPaperclip() error {
|
||||
addr := viper.GetString("paperclip.grpc_endpoint")
|
||||
if conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
|
||||
return err
|
||||
} else {
|
||||
Attachments = pcpb.NewAttachmentsClient(conn)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -34,10 +34,10 @@ func NewGRPC() {
|
||||
}
|
||||
|
||||
func ListenGRPC() error {
|
||||
listen, err := net.Listen("tcp", viper.GetString("grpc_bind"))
|
||||
listener, err := net.Listen("tcp", viper.GetString("grpc_bind"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return S.Serve(listen)
|
||||
return S.Serve(listener)
|
||||
}
|
||||
|
@ -3,10 +3,9 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
pcpb "git.solsynth.dev/hydrogen/paperclip/pkg/grpc/proto"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/grpc"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/utils"
|
||||
@ -25,7 +24,11 @@ func setAvatar(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := grpc.Attachments.CheckAttachmentExists(context.Background(), &pcpb.AttachmentLookupRequest{
|
||||
pc, err := gap.DiscoverPaperclip()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "attachments services was not available")
|
||||
}
|
||||
if _, err := pcpb.NewAttachmentsClient(pc).CheckAttachmentExists(context.Background(), &pcpb.AttachmentLookupRequest{
|
||||
Id: lo.ToPtr(uint64(data.AttachmentID)),
|
||||
Usage: lo.ToPtr("p.avatar"),
|
||||
}); err != nil {
|
||||
@ -54,7 +57,11 @@ func setBanner(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := grpc.Attachments.CheckAttachmentExists(context.Background(), &pcpb.AttachmentLookupRequest{
|
||||
pc, err := gap.DiscoverPaperclip()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "attachments services was not available")
|
||||
}
|
||||
if _, err := pcpb.NewAttachmentsClient(pc).CheckAttachmentExists(context.Background(), &pcpb.AttachmentLookupRequest{
|
||||
Id: lo.ToPtr(uint64(data.AttachmentID)),
|
||||
Usage: lo.ToPtr("p.banner"),
|
||||
}); err != nil {
|
||||
|
@ -56,9 +56,6 @@ func main() {
|
||||
if err := services.SetupAPNS(); err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when connecting APNs...")
|
||||
}
|
||||
if err := grpc.ConnectPaperclip(); err != nil {
|
||||
log.Fatal().Err(err).Msg("An error occurred when connecting to Paperclip...")
|
||||
}
|
||||
|
||||
// Server
|
||||
server.NewServer()
|
||||
|
Reference in New Issue
Block a user