diff --git a/API_GatewayWeb/DataStructures/main.go b/API_GatewayWeb/DataStructures/main.go new file mode 100644 index 0000000..35f9d02 --- /dev/null +++ b/API_GatewayWeb/DataStructures/main.go @@ -0,0 +1,50 @@ +package DataStructures + +import ( + "go.mongodb.org/mongo-driver/bson/primitive" + "time" +) + +type User struct { + Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + Name string `json:"name" bson:"name"` + Lastname string `json:"lastname" bson:"lastname"` + Email string `json:"email" bson:"email"` + Password string `json:"password" bson:"password"` + Phone string `json:"phone" bson:"phone"` + Role string `json:"role" bson:"role"` +} + +type Restaurant struct { + Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + Name string `json:"name" bson:"name"` + Address string `json:"address" bson:"address"` + Contact string `json:"contact" bson:"contact"` +} + +type Product struct { + Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + RestaurantId primitive.ObjectID `json:"restaurantId" bson:"restaurantId"` + Name string `json:"name" bson:"name"` + Price int32 `json:"price" bson:"price"` // in cents +} + +type Order struct { + Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + SellerId primitive.ObjectID `json:"sellerId" bson:"sellerId"` + DeliveryPersonId primitive.ObjectID `json:"deliveryPersonId" bson:"deliveryPersonId"` + Address string `json:"address" bson:"address"` + CustomerName string `json:"customerName" bson:"customerName"` + OrderItems []OrderItem `json:"items" bson:"items"` + Status int8 `json:"status" bson:"status"` + OrderDate time.Time `json:"orderDate" bson:"orderDate"` + PaymentType int8 `json:"paymentType" bson:"paymentType"` + TotalPrice int32 `json:"totalPrice" bson:"totalPrice"` // in cents +} + +type OrderItem struct { + Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + Product Product `json:"product" bson:"product"` + Quantity int32 `json:"quantity" bson:"quantity"` + Price int32 `json:"price" bson:"price"` // in cents +} diff --git a/API_GatewayWeb/DataStructures/order_service.proto b/API_GatewayWeb/DataStructures/order_service.proto new file mode 100644 index 0000000..1bbf727 --- /dev/null +++ b/API_GatewayWeb/DataStructures/order_service.proto @@ -0,0 +1,74 @@ +syntax = "proto3"; + +package com.blazc; + +option go_package = "com.blazc"; + +service OrderService { + rpc CreateOrder (Order) returns (Confirmation) {} + rpc GetOrder (GetOrderRequest) returns (Order) {} + rpc UpdateOrder (Order) returns (Confirmation) {} + rpc GetOrdersBySeller (GetOrdersRequest) returns (stream Order) {} + rpc GetOrdersByDeliveryPerson (GetOrdersRequest) returns (stream Order) {} + rpc DeleteOrder (DeleteOrderRequest) returns (Confirmation) {} + rpc Health (Empty) returns (Confirmation) {} +} + +message Empty {} + +message Confirmation { + string message = 2; + string error = 3; +} + +message GetOrdersRequest { + string id = 1; +} + +message GetOrderRequest { + string id = 1; +} + +message DeleteOrderRequest { + string id = 1; +} + +message Order { + string id = 1; + string sellerId = 2; + string deliveryPersonId = 3; + string address = 4; + string customerName = 5; + repeated OrderItem items = 6; + OrderStatus status = 7; + string orderDate = 8; + PaymentType paymentType = 9; + int32 totalPrice = 10; +} + +message OrderItem { + string id = 1; + Product product = 2; + int32 quantity = 3; + int32 price = 4; +} + +message Product { + string id = 1; + string name = 2; + int32 price = 3; +} + +enum PaymentType { + CREDIT_CARD = 0; + CASH = 2; +} + +enum OrderStatus { + PENDING = 0; + PREPARING = 1; + READY_FOR_PICKUP = 2; + DELIVERING = 3; + DELIVERED = 4; + CANCELLED = 5; +} \ No newline at end of file diff --git a/API_GatewayWeb/HTTP_API/Product.go b/API_GatewayWeb/HTTP_API/Product.go new file mode 100644 index 0000000..0abdbb2 --- /dev/null +++ b/API_GatewayWeb/HTTP_API/Product.go @@ -0,0 +1,89 @@ +package HTTP_API + +import ( + "API_GatewayWeb/DataStructures" + "github.com/gin-gonic/gin" + "go.mongodb.org/mongo-driver/bson/primitive" +) + +func (a *Controller) getAllProductsByRestaurantId(ctx *gin.Context) { + id, err := primitive.ObjectIDFromHex(ctx.Param("id")) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + restaurant, err := a.logic.GetAllProductsByRestaurantId(ctx.Request.Context(), id) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, restaurant) +} + +func (a *Controller) getProductById(ctx *gin.Context) { + id, err := primitive.ObjectIDFromHex(ctx.Param("id")) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + product, err := a.logic.GetProductById(ctx.Request.Context(), id) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, product) +} + +func (a *Controller) createProduct(ctx *gin.Context) { + var product DataStructures.Product + err := ctx.BindJSON(&product) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + createdProduct, err := a.logic.CreateProduct(ctx.Request.Context(), product) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, createdProduct) +} + +func (a *Controller) updateProduct(ctx *gin.Context) { + var product DataStructures.Product + err := ctx.BindJSON(&product) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + updated, err := a.logic.UpdateProduct(ctx.Request.Context(), product) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, updated) +} + +func (a *Controller) deleteProduct(ctx *gin.Context) { + id, err := primitive.ObjectIDFromHex(ctx.Param("id")) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + err = a.logic.DeleteProduct(ctx.Request.Context(), id) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, gin.H{"message": "Product deleted"}) +} diff --git a/API_GatewayWeb/HTTP_API/Restaurant.go b/API_GatewayWeb/HTTP_API/Restaurant.go new file mode 100644 index 0000000..3c3ff0b --- /dev/null +++ b/API_GatewayWeb/HTTP_API/Restaurant.go @@ -0,0 +1,83 @@ +package HTTP_API + +import ( + "API_GatewayWeb/DataStructures" + "github.com/gin-gonic/gin" + "go.mongodb.org/mongo-driver/bson/primitive" +) + +func (a *Controller) getAllRestaurants(ctx *gin.Context) { + restaurants, err := a.logic.GetAllRestaurants(ctx.Request.Context()) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, restaurants) +} + +func (a *Controller) getRestaurantById(ctx *gin.Context) { + id, err := primitive.ObjectIDFromHex(ctx.Param("id")) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + restaurant, err := a.logic.GetRestaurantById(ctx.Request.Context(), id) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, restaurant) +} + +func (a *Controller) createRestaurant(ctx *gin.Context) { + var restaurant DataStructures.Restaurant + err := ctx.BindJSON(&restaurant) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + createdRestaurant, err := a.logic.CreateRestaurant(ctx.Request.Context(), restaurant) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, createdRestaurant) +} + +func (a *Controller) updateRestaurant(ctx *gin.Context) { + var restaurant DataStructures.Restaurant + err := ctx.BindJSON(&restaurant) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + updated, err := a.logic.UpdateRestaurant(ctx.Request.Context(), restaurant) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, updated) +} + +func (a *Controller) deleteRestaurant(ctx *gin.Context) { + id, err := primitive.ObjectIDFromHex(ctx.Param("id")) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + err = a.logic.DeleteRestaurant(ctx.Request.Context(), id) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, gin.H{}) +} diff --git a/API_GatewayWeb/HTTP_API/Routes.go b/API_GatewayWeb/HTTP_API/Routes.go new file mode 100644 index 0000000..c76dcba --- /dev/null +++ b/API_GatewayWeb/HTTP_API/Routes.go @@ -0,0 +1,37 @@ +package HTTP_API + +import "github.com/gin-gonic/gin" + +func (a *Controller) registerRoutes(engine *gin.Engine) { + + api := engine.Group("/api/v1") + api.GET("/", a.Ping) + + a.registerUserRoutes(api.Group("/users")) + a.registerRestaurantRoutes(api.Group("/restaurants")) + a.registerProductRoutes(api.Group("/products")) +} + +func (a *Controller) registerUserRoutes(api *gin.RouterGroup) { + api.GET("/", a.getAllUsers) + api.POST("/", a.createUser) + api.GET("/:id", a.getUserById) + api.PUT("/:id", a.updateUser) + api.DELETE("/:id", a.deleteUser) +} + +func (a *Controller) registerRestaurantRoutes(api *gin.RouterGroup) { + api.GET("/", a.getAllRestaurants) + api.POST("/", a.createRestaurant) + api.GET("/:id", a.getRestaurantById) + api.PUT("/:id", a.updateRestaurant) + api.DELETE("/:id", a.deleteRestaurant) +} + +func (a *Controller) registerProductRoutes(api *gin.RouterGroup) { + api.GET("/restaurant/:id", a.getAllProductsByRestaurantId) + api.POST("/", a.createProduct) + api.GET("/:id", a.getProductById) + api.PUT("/:id", a.updateProduct) + api.DELETE("/:id", a.deleteProduct) +} diff --git a/API_GatewayWeb/HTTP_API/User.go b/API_GatewayWeb/HTTP_API/User.go new file mode 100644 index 0000000..6b6b2e8 --- /dev/null +++ b/API_GatewayWeb/HTTP_API/User.go @@ -0,0 +1,86 @@ +package HTTP_API + +import ( + "API_GatewayWeb/DataStructures" + "fmt" + "github.com/gin-gonic/gin" + "go.mongodb.org/mongo-driver/bson/primitive" +) + +func (a *Controller) getAllUsers(ctx *gin.Context) { + users, err := a.logic.GetAllUsers(ctx.Request.Context()) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, users) +} + +func (a *Controller) getUserById(ctx *gin.Context) { + id, err := primitive.ObjectIDFromHex(ctx.Param("id")) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + user, err := a.logic.GetUserById(ctx.Request.Context(), id) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, user) +} + +func (a *Controller) createUser(ctx *gin.Context) { + var user DataStructures.User + err := ctx.BindJSON(&user) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + fmt.Println(user) + + createdUser, err := a.logic.CreateUser(ctx.Request.Context(), user) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(201, createdUser) +} + +func (a *Controller) updateUser(ctx *gin.Context) { + var user DataStructures.User + err := ctx.BindJSON(&user) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + updatedUser, err := a.logic.UpdateUser(ctx.Request.Context(), user) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, updatedUser) +} + +func (a *Controller) deleteUser(ctx *gin.Context) { + id, err := primitive.ObjectIDFromHex(ctx.Param("id")) + if err != nil { + ctx.JSON(400, gin.H{"error": err.Error()}) + return + } + + err = a.logic.DeleteUser(ctx.Request.Context(), id) + if err != nil { + ctx.JSON(500, gin.H{"error": err.Error()}) + return + } + + ctx.JSON(200, gin.H{"message": "User deleted"}) +} diff --git a/API_GatewayWeb/HTTP_API/main.go b/API_GatewayWeb/HTTP_API/main.go new file mode 100644 index 0000000..37e1de8 --- /dev/null +++ b/API_GatewayWeb/HTTP_API/main.go @@ -0,0 +1,69 @@ +package HTTP_API + +import ( + "API_GatewayWeb/Logic" + "context" + "fmt" + "github.com/gin-gonic/gin" + "net/http" + "os" + "time" +) + +type Controller struct { + logic *Logic.Controller + done chan bool +} + +func New(logic *Logic.Controller) *Controller { + return &Controller{ + logic: logic, + } +} + +func (a *Controller) Start() { + + engine := gin.Default() + a.registerRoutes(engine) + + a.done = make(chan bool, 0) + + srv := &http.Server{ + Addr: ":8000", + Handler: engine, + ReadTimeout: 20 * time.Second, + WriteTimeout: 30 * time.Second, + } + + go func() { + + <-a.done + + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + srv.SetKeepAlivesEnabled(false) + if err := srv.Shutdown(ctx); err != nil { + fmt.Println(err.Error()) + } + + }() + + go func() { + + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + fmt.Println(err.Error()) + } + os.Exit(1) + + }() + +} + +func (a *Controller) Stop() { + a.done <- true +} + +func (a *Controller) Ping(c *gin.Context) { + c.JSON(http.StatusOK, "pong") +} diff --git a/API_GatewayWeb/Logic/Product.go b/API_GatewayWeb/Logic/Product.go new file mode 100644 index 0000000..2cc8c14 --- /dev/null +++ b/API_GatewayWeb/Logic/Product.go @@ -0,0 +1,142 @@ +package Logic + +import ( + "API_GatewayWeb/DataStructures" + "bytes" + "context" + "encoding/json" + "fmt" + "go.mongodb.org/mongo-driver/bson/primitive" + "net/http" +) + +func (c *Controller) GetAllProductsByRestaurantId(ctx context.Context, id primitive.ObjectID) (products []DataStructures.Product, err error) { + url := fmt.Sprintf("%s/product/restaurant/%s", getEnv("RESTAURANTS_API", "http://localhost:8082"), id.Hex()) + + request, err := http.NewRequest("GET", url, nil) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&products) + if err != nil { + fmt.Println(err.Error()) + return nil, err + } + + return +} + +func (c *Controller) GetProductById(ctx context.Context, id primitive.ObjectID) (product DataStructures.Product, err error) { + url := fmt.Sprintf("%s/product/%s", getEnv("RESTAURANTS_API", "http://localhost:8082"), id.Hex()) + + request, err := http.NewRequest("GET", url, nil) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&product) + if err != nil { + fmt.Println(err.Error()) + return + } + + return +} + +func (c *Controller) CreateProduct(ctx context.Context, product DataStructures.Product) (createdProduct DataStructures.Product, err error) { + url := fmt.Sprintf("%s/product", getEnv("RESTAURANTS_API", "http://localhost:8082")) + + body, err := json.Marshal(product) + if err != nil { + fmt.Println(err.Error()) + return + } + + request, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&createdProduct) + if err != nil { + fmt.Println(err.Error()) + return + } + + return +} + +func (c *Controller) UpdateProduct(ctx context.Context, product DataStructures.Product) (updatedProduct DataStructures.Product, err error) { + url := fmt.Sprintf("%s/product", getEnv("RESTAURANTS_API", "http://localhost:8082")) + + body, err := json.Marshal(product) + if err != nil { + fmt.Println(err.Error()) + return + } + + request, err := http.NewRequest("PUT", url, bytes.NewBuffer(body)) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&updatedProduct) + if err != nil { + fmt.Println(err.Error()) + return + } + + return +} + +func (c *Controller) DeleteProduct(ctx context.Context, id primitive.ObjectID) (err error) { + url := fmt.Sprintf("%s/product/%s", getEnv("RESTAURANTS_API", "http://localhost:8082"), id.Hex()) + + request, err := http.NewRequest("DELETE", url, nil) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + return +} diff --git a/API_GatewayWeb/Logic/Restaurant.go b/API_GatewayWeb/Logic/Restaurant.go new file mode 100644 index 0000000..ebf84b8 --- /dev/null +++ b/API_GatewayWeb/Logic/Restaurant.go @@ -0,0 +1,148 @@ +package Logic + +import ( + "API_GatewayWeb/DataStructures" + "bytes" + "context" + "encoding/json" + "fmt" + "go.mongodb.org/mongo-driver/bson/primitive" + "net/http" +) + +func (c *Controller) GetAllRestaurants(ctx context.Context) (restaurants []DataStructures.Restaurant, err error) { + url := fmt.Sprintf("%s/restaurant", getEnv("RESTAURANTS_API", "http://localhost:8082")) + + request, err := http.NewRequest("GET", url, nil) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&restaurants) + if err != nil { + fmt.Println(err.Error()) + return nil, err + } + + return +} + +func (c *Controller) GetRestaurantById(ctx context.Context, id primitive.ObjectID) (restaurant DataStructures.Restaurant, err error) { + url := fmt.Sprintf("%s/restaurant/%s", getEnv("RESTAURANTS_API", "http://localhost:8082"), id.Hex()) + + fmt.Println(url) + + request, err := http.NewRequest("GET", url, nil) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&restaurant) + if err != nil { + fmt.Println(err.Error()) + return + } + + return +} + +func (c *Controller) CreateRestaurant(ctx context.Context, restaurant DataStructures.Restaurant) (createdRestaurant DataStructures.Restaurant, err error) { + url := fmt.Sprintf("%s/restaurant", getEnv("RESTAURANTS_API", "http://localhost:8082")) + + body, err := json.Marshal(restaurant) + if err != nil { + fmt.Println(err.Error()) + return + } + + request, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) + if err != nil { + fmt.Println(err.Error()) + return + } + + request.Header.Set("Content-Type", "application/json") + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&createdRestaurant) + if err != nil { + fmt.Println(err.Error()) + return + } + + return +} + +func (c *Controller) UpdateRestaurant(ctx context.Context, restaurant DataStructures.Restaurant) (updatedRestaurant DataStructures.Restaurant, err error) { + url := fmt.Sprintf("%s/restaurant/%s", getEnv("RESTAURANTS_API", "http://localhost:8082"), restaurant.Id.Hex()) + + body, err := json.Marshal(restaurant) + if err != nil { + fmt.Println(err.Error()) + return + } + + request, err := http.NewRequest("PUT", url, bytes.NewBuffer(body)) + if err != nil { + fmt.Println(err.Error()) + return + } + + request.Header.Set("Content-Type", "application/json") + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&updatedRestaurant) + if err != nil { + fmt.Println(err.Error()) + return + } + + return +} + +func (c *Controller) DeleteRestaurant(ctx context.Context, id primitive.ObjectID) (err error) { + url := fmt.Sprintf("%s/restaurant/%s", getEnv("RESTAURANTS_API", "http://localhost:8082"), id.Hex()) + + request, err := http.NewRequest("DELETE", url, nil) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + return +} diff --git a/API_GatewayWeb/Logic/User.go b/API_GatewayWeb/Logic/User.go new file mode 100644 index 0000000..87b941c --- /dev/null +++ b/API_GatewayWeb/Logic/User.go @@ -0,0 +1,146 @@ +package Logic + +import ( + "API_GatewayWeb/DataStructures" + "bytes" + "context" + "encoding/json" + "fmt" + "go.mongodb.org/mongo-driver/bson/primitive" + "net/http" +) + +func (c *Controller) GetAllUsers(ctx context.Context) (users []DataStructures.User, err error) { + url := fmt.Sprintf("%s/users", getEnv("USERS_API", "http://localhost:8081/api/v1")) + + request, err := http.NewRequest("GET", url, nil) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&users) + if err != nil { + fmt.Println(err.Error()) + return nil, err + } + + return +} + +func (c *Controller) GetUserById(ctx context.Context, id primitive.ObjectID) (user DataStructures.User, err error) { + url := fmt.Sprintf("%s/users/%s", getEnv("USERS_API", "http://localhost:8081/api/v1"), user.Id.Hex()) + + request, err := http.NewRequest("GET", url, nil) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&user) + if err != nil { + fmt.Println(err.Error()) + return + } + + return +} + +func (c *Controller) CreateUser(ctx context.Context, user DataStructures.User) (createdUser DataStructures.User, err error) { + url := fmt.Sprintf("%s/users", getEnv("USERS_API", "http://localhost:8081/api/v1")) + + body, err := json.Marshal(user) + if err != nil { + fmt.Println(err.Error()) + return + } + + request, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) + if err != nil { + fmt.Println(err.Error()) + return + } + + request.Header.Set("Content-Type", "application/json") + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&createdUser) + if err != nil { + fmt.Println(err.Error()) + return + } + + return +} + +func (c *Controller) UpdateUser(ctx context.Context, user DataStructures.User) (updatedUser DataStructures.User, err error) { + url := fmt.Sprintf("%s/users/%s", getEnv("USERS_API", "http://localhost:8081/api/v1"), user.Id.Hex()) + + body, err := json.Marshal(user) + if err != nil { + fmt.Println(err.Error()) + return + } + + request, err := http.NewRequest("PUT", url, bytes.NewBuffer(body)) + if err != nil { + fmt.Println(err.Error()) + return + } + + request.Header.Set("Content-Type", "application/json") + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + err = json.NewDecoder(response.Body).Decode(&updatedUser) + if err != nil { + fmt.Println(err.Error()) + return + } + + return +} + +func (c *Controller) DeleteUser(ctx context.Context, id primitive.ObjectID) (err error) { + url := fmt.Sprintf("%s/users/%s", getEnv("USERS_API", "http://localhost:8081/api/v1"), id.Hex()) + + request, err := http.NewRequest("DELETE", url, nil) + if err != nil { + fmt.Println(err.Error()) + return + } + + response, err := c.httpClient.Do(request) + if err != nil { + fmt.Println(err.Error()) + return + } + defer response.Body.Close() + + return +} diff --git a/API_GatewayWeb/Logic/main.go b/API_GatewayWeb/Logic/main.go new file mode 100644 index 0000000..2cb07ca --- /dev/null +++ b/API_GatewayWeb/Logic/main.go @@ -0,0 +1,28 @@ +package Logic + +import ( + "API_GatewayWeb/gRPC_Client" + "net/http" + "os" +) + +type Controller struct { + httpClient *http.Client + grpc *gRPC_Client.Controller +} + +func New(grpc *gRPC_Client.Controller) (*Controller, error) { + httpClient := &http.Client{} + + return &Controller{ + httpClient: httpClient, + grpc: grpc, + }, nil +} + +func getEnv(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +} diff --git a/API_GatewayWeb/gRPC_Client/main.go b/API_GatewayWeb/gRPC_Client/main.go new file mode 100644 index 0000000..032e5e8 --- /dev/null +++ b/API_GatewayWeb/gRPC_Client/main.go @@ -0,0 +1,30 @@ +package gRPC_Client + +import ( + "API_GatewayWeb/DataStructures/com.blazc" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +type Controller struct { + Client com_blazc.OrderServiceClient +} + +func NewController() *Controller { + return &Controller{} +} + +func (c *Controller) EstablishConnection() (err error) { + + opts := []grpc.DialOption{ + grpc.WithTransportCredentials(insecure.NewCredentials()), + } + + conn, err := grpc.Dial("localhost:9000", opts...) + if err != nil { + return err + } + + c.Client = com_blazc.NewOrderServiceClient(conn) + return +} diff --git a/API_GatewayWeb/main.go b/API_GatewayWeb/main.go new file mode 100644 index 0000000..38d8044 --- /dev/null +++ b/API_GatewayWeb/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "API_GatewayWeb/HTTP_API" + "API_GatewayWeb/Logic" + "API_GatewayWeb/gRPC_Client" + "fmt" + "os" + "os/signal" +) + +func main() { + + grpcClient := gRPC_Client.NewController() + err := grpcClient.EstablishConnection() + if err != nil { + fmt.Println(err.Error()) + return + } + + fmt.Println("gRPC connection established") + + logicController, err := Logic.New(grpcClient) + if err != nil { + fmt.Println(err.Error()) + return + } + + httpAPI := HTTP_API.New(logicController) + httpAPI.Start() + + quit := make(chan os.Signal, 0) + signal.Notify(quit, os.Interrupt) + + <-quit + + httpAPI.Stop() +} + +func getEnv(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +}