Interservice communication
This commit is contained in:
@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"machines/app/cfg"
|
||||
"machines/app/db"
|
||||
"net/http"
|
||||
@ -56,6 +57,39 @@ func handleGetMachine(dbConn *gorm.DB) gin.HandlerFunc {
|
||||
|
||||
func handleGetMachineProducts(dbConn *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
machineId, err := strconv.Atoi(c.Param("machineId"))
|
||||
if err != nil {
|
||||
raiseBadRequestError(c, "Invalid machineId parameter")
|
||||
return
|
||||
}
|
||||
|
||||
machine, err := db.GetMachine(dbConn, machineId)
|
||||
if err != nil {
|
||||
raiseNotFoundError(c, "Machine not found")
|
||||
return
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/products?machineId=%d", cfg.Config.ProductsAppUrl, machine.Id)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
raiseInternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if resp.Body != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
raiseInternalError(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("Content-Type", "application/json; charset=utf-8")
|
||||
c.Writer.WriteString(string(body))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user