diff --git a/authentication/Dockerfile b/authentication/Dockerfile new file mode 100644 index 0000000..bc18e32 --- /dev/null +++ b/authentication/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.20-alpine AS build +WORKDIR /app +COPY go.mod go.sum ./ +RUN go mod download +COPY . . +RUN CGO_ENABLED=0 GOOS=linux go build -o main + +FROM alpine:latest +WORKDIR /app +COPY --from=build /app/main . +ENV AUTH_SERVICE_PORT=${AUTH_SERVICE_PORT} +ENV POSTGRES_URI=${POSTGRES_URI} +CMD [ "./main"] \ No newline at end of file diff --git a/authentication/db/connect.go b/authentication/db/connect.go new file mode 100644 index 0000000..c4ab3bc --- /dev/null +++ b/authentication/db/connect.go @@ -0,0 +1,36 @@ +package db + +import ( + "landate/authentication/models" + "landate/config" + "log" + + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +var DB *gorm.DB + +func GetDBInstance() *gorm.DB { + return DB +} + +func PGConnect() { + postgresURI := config.GetEnvConfig("POSTGRES_URI") + db, err := gorm.Open(postgres.Open(postgresURI), &gorm.Config{ + Logger: logger.Default.LogMode(logger.Info), + }) + if err != nil { + log.Fatal("Failed to connect to Database.", err) + } + log.Println("Connected ✅") + db.Logger = logger.Default.LogMode(logger.Info) + + // Migrate the schema to Database + log.Println("Running migrations...") + db.AutoMigrate(&models.User{}) + + DB = db + +} diff --git a/authentication/handlers/handlers.go b/authentication/handlers/handlers.go new file mode 100644 index 0000000..5b0b80b --- /dev/null +++ b/authentication/handlers/handlers.go @@ -0,0 +1,33 @@ +package handlers + +import "github.com/gofiber/fiber/v2" + +// @Desp: Create User +func CreateUser(c *fiber.Ctx) error { + return nil +} + +// @Desp: Get User +func RetrieveUser(c *fiber.Ctx) error { + return nil +} + +func GetAllUsers(c *fiber.Ctx) error { + return nil +} + +func RemoveUser(c *fiber.Ctx) error { + return nil +} + +func UpdateUser(c *fiber.Ctx) error { + return nil +} + +func GetUserByWalledId(c *fiber.Ctx) error { + return nil +} + +func GetUserById(c *fiber.Ctx) error { + return nil +} diff --git a/authentication/models/models.go b/authentication/models/models.go index 0cd94f8..f00a991 100644 --- a/authentication/models/models.go +++ b/authentication/models/models.go @@ -2,6 +2,8 @@ package models type User struct { Id int `json:"id"` + FirstName string `json:"firstName"` + LastName string `json:"lastName"` Name string `json:"name"` WalletAddress string `json:"walletAddress"` Email string `json:"email"` diff --git a/authentication/routes/routes.go b/authentication/routes/routes.go new file mode 100644 index 0000000..0db51ae --- /dev/null +++ b/authentication/routes/routes.go @@ -0,0 +1 @@ +package routes diff --git a/broker/consumer/consumer.go b/broker/consumer/consumer.go new file mode 100644 index 0000000..8c0d9c4 --- /dev/null +++ b/broker/consumer/consumer.go @@ -0,0 +1,5 @@ +package consumer + +func MessageConsumer() { + +} diff --git a/broker/msg_broker.go b/broker/msg_broker.go deleted file mode 100644 index ab0bbdb..0000000 --- a/broker/msg_broker.go +++ /dev/null @@ -1,48 +0,0 @@ -package broker - -import ( - "fmt" - - "github.com/rabbitmq/amqp091-go" -) - -type MsgBroker struct { - mbChannel *amqp091.Channel -} - -func (mb *MsgBroker) RabbitMQClient() *MsgBroker { - - conn, err := amqp091.Dial("amqp://guest:guest@localhost:5672/") - if err != nil { - fmt.Println("Failed Initializing Broker connection.") - } - - channel, err := conn.Channel() - if err != nil { - fmt.Println("Error:", err) - } - - defer channel.Close() - // mb.mbChannel = channel - return &MsgBroker{ - mbChannel: channel, - } -} - -func (mb *MsgBroker) RabbitProducer() { - - queue, err := mb.mbChannel.QueueDeclare( - "TestMsgQueue", - false, - false, - false, - false, - nil, // arguments - ) - if err != nil { - fmt.Println(err) - } - - fmt.Println(queue) - -} diff --git a/broker/producer/producer.go b/broker/producer/producer.go new file mode 100644 index 0000000..f651537 --- /dev/null +++ b/broker/producer/producer.go @@ -0,0 +1,47 @@ +package producer + +import ( + "context" + broker "landate/broker" + "log" + "time" + + amqp "github.com/rabbitmq/amqp091-go" +) + +func MessageProducer() { + + ch, bctx := broker.RabbitMQClient() + + err := ch.ExchangeDeclare( + "payload", // name + "fanout", // type + true, // durable + false, // auto-deleted + false, // internal + false, // no-wait + nil, // arguments + ) + failOnError(err, "Failed to declare an exchange") + + ctx, cancel := context.WithTimeout(bctx, 5*time.Second) + defer cancel() + + err = ch.PublishWithContext(ctx, + "logs", // exchange + "", // routing key + false, // mandatory + false, // immediate + amqp.Publishing{ + ContentType: "text/plain", + Body: []byte(""), + }) + + failOnError(err, "Failed to publish a message") +} + +func failOnError(err error, msg string) { + if err != nil { + log.Panicf("%s: %s", msg, err) + } +} diff --git a/broker/rabbitmq.go b/broker/rabbitmq.go new file mode 100644 index 0000000..62a6b33 --- /dev/null +++ b/broker/rabbitmq.go @@ -0,0 +1,33 @@ +package broker + +import ( + "context" + "log" + + "github.com/rabbitmq/amqp091-go" +) + +type MsgBroker struct { + mbChannel *amqp091.Channel + ctx context.Context +} + +func RabbitMQClient() (*amqp091.Channel, context.Context) { + + conn, err := amqp091.Dial("amqp://guest:guest@localhost:5672/") + failOnError(err, "Failed to connect to RabbitMQ") + defer conn.Close() + + channel, err := conn.Channel() + failOnError(err, "Failed to open a channel") + defer channel.Close() + + ctx := context.Background() + return channel, ctx +} + +func failOnError(err error, msg string) { + if err != nil { + log.Panicf("%s: %s", msg, err) + } +}