App config
This commit is contained in:
@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"machines/app/cfg"
|
||||||
"machines/app/db"
|
"machines/app/db"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -10,9 +11,6 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
const HOST = "0.0.0.0"
|
|
||||||
const PORT = 3000
|
|
||||||
|
|
||||||
func handlePing(c *gin.Context) {
|
func handlePing(c *gin.Context) {
|
||||||
c.JSON(
|
c.JSON(
|
||||||
http.StatusOK,
|
http.StatusOK,
|
||||||
@ -56,6 +54,11 @@ func handleGetMachine(dbConn *gorm.DB) gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleGetMachineProducts(dbConn *gorm.DB) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func initRouter(dbConn *gorm.DB) *gin.Engine {
|
func initRouter(dbConn *gorm.DB) *gin.Engine {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
@ -65,13 +68,15 @@ func initRouter(dbConn *gorm.DB) *gin.Engine {
|
|||||||
routes.GET("/ping", handlePing)
|
routes.GET("/ping", handlePing)
|
||||||
routes.GET("", handleGetMachines(dbConn))
|
routes.GET("", handleGetMachines(dbConn))
|
||||||
routes.GET("/:machineId", handleGetMachine(dbConn))
|
routes.GET("/:machineId", handleGetMachine(dbConn))
|
||||||
|
routes.GET("/:machineId/products", handleGetMachineProducts(dbConn))
|
||||||
}
|
}
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
func Serve(dbConn *gorm.DB) {
|
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 := initRouter(dbConn)
|
||||||
router.Run(serverAddr)
|
router.Run(serverAddr)
|
||||||
|
|||||||
@ -5,11 +5,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type configStruct struct {
|
type configStruct struct {
|
||||||
DbHost string `default:"localhost"`
|
AppHost string `default:"0.0.0.0"`
|
||||||
DbPort int `default:"55432"`
|
AppPort int `default:"3000"`
|
||||||
DbName string `default:"komponiranje"`
|
DbHost string `default:"localhost"`
|
||||||
DbUser string `default:"pero"`
|
DbPort int `default:"55432"`
|
||||||
DbPassword string `default:"pero.000"`
|
DbName string `default:"komponiranje"`
|
||||||
|
DbUser string `default:"pero"`
|
||||||
|
DbPassword string `default:"pero.000"`
|
||||||
|
ProductsAppHost string `default:"localhost"`
|
||||||
|
ProductsAppPort int `default:"3001"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const ENV_PREFIX = ""
|
const ENV_PREFIX = ""
|
||||||
|
|||||||
@ -4,6 +4,7 @@ go 1.21.5
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.9.1
|
github.com/gin-gonic/gin v1.9.1
|
||||||
|
github.com/kelseyhightower/envconfig v1.4.0
|
||||||
gorm.io/driver/postgres v1.5.4
|
gorm.io/driver/postgres v1.5.4
|
||||||
gorm.io/gorm v1.25.5
|
gorm.io/gorm v1.25.5
|
||||||
)
|
)
|
||||||
@ -24,7 +25,6 @@ require (
|
|||||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.5 // indirect
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // 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/klauspost/cpuid/v2 v2.2.4 // indirect
|
||||||
github.com/kr/text v0.2.0 // indirect
|
github.com/kr/text v0.2.0 // indirect
|
||||||
github.com/leodido/go-urn v1.2.4 // indirect
|
github.com/leodido/go-urn v1.2.4 // indirect
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"products/app/cfg"
|
||||||
"products/app/db"
|
"products/app/db"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@ -10,9 +11,6 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
const HOST = "0.0.0.0"
|
|
||||||
const PORT = 3000
|
|
||||||
|
|
||||||
func handlePing(c *gin.Context) {
|
func handlePing(c *gin.Context) {
|
||||||
c.JSON(
|
c.JSON(
|
||||||
http.StatusOK,
|
http.StatusOK,
|
||||||
@ -86,7 +84,8 @@ func initRouter(dbConn *gorm.DB) *gin.Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Serve(dbConn *gorm.DB) {
|
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 := initRouter(dbConn)
|
||||||
router.Run(serverAddr)
|
router.Run(serverAddr)
|
||||||
|
|||||||
@ -5,6 +5,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type configStruct struct {
|
type configStruct struct {
|
||||||
|
AppHost string `default:"0.0.0.0"`
|
||||||
|
AppPort int `default:"3001"`
|
||||||
DbHost string `default:"localhost"`
|
DbHost string `default:"localhost"`
|
||||||
DbPort int `default:"55432"`
|
DbPort int `default:"55432"`
|
||||||
DbName string `default:"komponiranje"`
|
DbName string `default:"komponiranje"`
|
||||||
|
|||||||
Reference in New Issue
Block a user