Interservice communication

This commit is contained in:
Eden Kirin
2024-01-14 22:56:06 +01:00
parent a27461cca8
commit e8bf362c6b
3 changed files with 45 additions and 9 deletions

View File

@ -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))
}
}

View File

@ -5,15 +5,14 @@ import (
)
type configStruct struct {
AppHost string `default:"0.0.0.0"`
AppPort int `default:"3000"`
DbHost string `default:"localhost"`
DbPort int `default:"55432"`
DbName string `default:"komponiranje"`
DbUser string `default:"pero"`
DbPassword string `default:"pero.000"`
ProductsAppHost string `default:"localhost"`
ProductsAppPort int `default:"3001"`
AppHost string `default:"0.0.0.0"`
AppPort int `default:"3000"`
DbHost string `default:"localhost"`
DbPort int `default:"55432"`
DbName string `default:"komponiranje"`
DbUser string `default:"pero"`
DbPassword string `default:"pero.000"`
ProductsAppUrl string `default:"http://localhost:3001"`
}
const ENV_PREFIX = ""