Compare commits
3 Commits
018fd310cb
...
e8bf362c6b
| Author | SHA1 | Date | |
|---|---|---|---|
| e8bf362c6b | |||
| a27461cca8 | |||
| 1f3195d992 |
@ -18,11 +18,13 @@ services:
|
||||
context: ./machines
|
||||
dockerfile: Dockerfile
|
||||
environment:
|
||||
- APPPORT=3000
|
||||
- DBHOST=db
|
||||
- DBPORT=5432
|
||||
- DBNAME=komponiranje
|
||||
- DBUSER=pero
|
||||
- DBPASSWORD=pero.000
|
||||
- PRODUCTSAPPURL=http://products-service:3000
|
||||
# ports:
|
||||
# - 3000:3000
|
||||
depends_on:
|
||||
@ -33,6 +35,7 @@ services:
|
||||
context: ./products
|
||||
dockerfile: Dockerfile
|
||||
environment:
|
||||
- APPPORT=3000
|
||||
- DBHOST=db
|
||||
- DBPORT=5432
|
||||
- DBNAME=komponiranje
|
||||
|
||||
@ -2,6 +2,8 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"machines/app/cfg"
|
||||
"machines/app/db"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@ -10,9 +12,6 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const HOST = "0.0.0.0"
|
||||
const PORT = 3000
|
||||
|
||||
func handlePing(c *gin.Context) {
|
||||
c.JSON(
|
||||
http.StatusOK,
|
||||
@ -56,6 +55,44 @@ 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))
|
||||
}
|
||||
}
|
||||
|
||||
func initRouter(dbConn *gorm.DB) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
router := gin.Default()
|
||||
@ -65,13 +102,15 @@ func initRouter(dbConn *gorm.DB) *gin.Engine {
|
||||
routes.GET("/ping", handlePing)
|
||||
routes.GET("", handleGetMachines(dbConn))
|
||||
routes.GET("/:machineId", handleGetMachine(dbConn))
|
||||
routes.GET("/:machineId/products", handleGetMachineProducts(dbConn))
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func Serve(dbConn *gorm.DB) {
|
||||
serverAddr := fmt.Sprintf("%s:%d", HOST, PORT)
|
||||
serverAddr := fmt.Sprintf("%s:%d", cfg.Config.AppHost, cfg.Config.AppPort)
|
||||
fmt.Printf("Starting serving on %s\n", serverAddr)
|
||||
|
||||
router := initRouter(dbConn)
|
||||
router.Run(serverAddr)
|
||||
|
||||
@ -5,11 +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"`
|
||||
ProductsAppUrl string `default:"http://localhost:3001"`
|
||||
}
|
||||
|
||||
const ENV_PREFIX = ""
|
||||
|
||||
@ -4,6 +4,7 @@ go 1.21.5
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/kelseyhightower/envconfig v1.4.0
|
||||
gorm.io/driver/postgres v1.5.4
|
||||
gorm.io/gorm v1.25.5
|
||||
)
|
||||
@ -24,7 +25,6 @@ require (
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/kelseyhightower/envconfig v1.4.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
|
||||
@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"products/app/cfg"
|
||||
"products/app/db"
|
||||
"strconv"
|
||||
|
||||
@ -10,9 +11,6 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const HOST = "0.0.0.0"
|
||||
const PORT = 3000
|
||||
|
||||
func handlePing(c *gin.Context) {
|
||||
c.JSON(
|
||||
http.StatusOK,
|
||||
@ -24,7 +22,22 @@ func handlePing(c *gin.Context) {
|
||||
|
||||
func handleGetProducts(dbConn *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
products := db.GetProducts(dbConn)
|
||||
var (
|
||||
machineId *int = nil
|
||||
err error
|
||||
mId int
|
||||
)
|
||||
|
||||
machineIdStr, ok := c.GetQuery("machineId")
|
||||
if ok && len(machineIdStr) > 0 {
|
||||
if mId, err = strconv.Atoi(machineIdStr); err != nil {
|
||||
raiseBadRequestError(c, "Invalid machineId filter")
|
||||
return
|
||||
}
|
||||
machineId = &mId
|
||||
}
|
||||
|
||||
products := db.GetProducts(dbConn, machineId)
|
||||
|
||||
c.JSON(
|
||||
http.StatusOK,
|
||||
@ -71,7 +84,8 @@ func initRouter(dbConn *gorm.DB) *gin.Engine {
|
||||
}
|
||||
|
||||
func Serve(dbConn *gorm.DB) {
|
||||
serverAddr := fmt.Sprintf("%s:%d", HOST, PORT)
|
||||
serverAddr := fmt.Sprintf("%s:%d", cfg.Config.AppHost, cfg.Config.AppPort)
|
||||
fmt.Printf("Starting serving on %s\n", serverAddr)
|
||||
|
||||
router := initRouter(dbConn)
|
||||
router.Run(serverAddr)
|
||||
|
||||
@ -5,6 +5,8 @@ import (
|
||||
)
|
||||
|
||||
type configStruct struct {
|
||||
AppHost string `default:"0.0.0.0"`
|
||||
AppPort int `default:"3001"`
|
||||
DbHost string `default:"localhost"`
|
||||
DbPort int `default:"55432"`
|
||||
DbName string `default:"komponiranje"`
|
||||
|
||||
@ -2,10 +2,19 @@ package db
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
func GetProducts(dbConn *gorm.DB) *[]Product {
|
||||
var products []Product
|
||||
func GetProducts(dbConn *gorm.DB, machineId *int) *[]Product {
|
||||
var (
|
||||
products []Product
|
||||
query = dbConn
|
||||
)
|
||||
|
||||
dbConn.Order("name").Find(&products)
|
||||
if machineId != nil {
|
||||
query = query.
|
||||
Joins("LEFT JOIN machine_products mp ON mp.product_id = products.id").
|
||||
Where("mp.machine_id = ?", machineId)
|
||||
}
|
||||
|
||||
query.Order("name").Find(&products)
|
||||
|
||||
return &products
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user