Interservice communication
This commit is contained in:
@ -18,11 +18,13 @@ services:
|
|||||||
context: ./machines
|
context: ./machines
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
environment:
|
environment:
|
||||||
|
- APPPORT=3000
|
||||||
- DBHOST=db
|
- DBHOST=db
|
||||||
- DBPORT=5432
|
- DBPORT=5432
|
||||||
- DBNAME=komponiranje
|
- DBNAME=komponiranje
|
||||||
- DBUSER=pero
|
- DBUSER=pero
|
||||||
- DBPASSWORD=pero.000
|
- DBPASSWORD=pero.000
|
||||||
|
- PRODUCTSAPPURL=http://products-service:3000
|
||||||
# ports:
|
# ports:
|
||||||
# - 3000:3000
|
# - 3000:3000
|
||||||
depends_on:
|
depends_on:
|
||||||
@ -33,6 +35,7 @@ services:
|
|||||||
context: ./products
|
context: ./products
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
environment:
|
environment:
|
||||||
|
- APPPORT=3000
|
||||||
- DBHOST=db
|
- DBHOST=db
|
||||||
- DBPORT=5432
|
- DBPORT=5432
|
||||||
- DBNAME=komponiranje
|
- DBNAME=komponiranje
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"machines/app/cfg"
|
"machines/app/cfg"
|
||||||
"machines/app/db"
|
"machines/app/db"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -56,6 +57,39 @@ func handleGetMachine(dbConn *gorm.DB) gin.HandlerFunc {
|
|||||||
|
|
||||||
func handleGetMachineProducts(dbConn *gorm.DB) gin.HandlerFunc {
|
func handleGetMachineProducts(dbConn *gorm.DB) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,8 +12,7 @@ type configStruct struct {
|
|||||||
DbName string `default:"komponiranje"`
|
DbName string `default:"komponiranje"`
|
||||||
DbUser string `default:"pero"`
|
DbUser string `default:"pero"`
|
||||||
DbPassword string `default:"pero.000"`
|
DbPassword string `default:"pero.000"`
|
||||||
ProductsAppHost string `default:"localhost"`
|
ProductsAppUrl string `default:"http://localhost:3001"`
|
||||||
ProductsAppPort int `default:"3001"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ENV_PREFIX = ""
|
const ENV_PREFIX = ""
|
||||||
|
|||||||
Reference in New Issue
Block a user