Machines app

This commit is contained in:
Eden Kirin
2024-01-13 14:21:29 +01:00
parent 9dbbb21161
commit dd62931b16
12 changed files with 350 additions and 0 deletions

5
machines/app/api/dto.go Normal file
View File

@ -0,0 +1,5 @@
package api
type PingDto struct {
Message string `json:"message"`
}

View File

@ -0,0 +1,7 @@
package api
import "machines/app/db"
type GetMachinesResponse struct {
Machines []db.Machine `json:"machines"`
}

View File

@ -0,0 +1,54 @@
package api
import (
"fmt"
"machines/app/db"
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
const HOST = "0.0.0.0"
const PORT = 3000
func handlePing(c *gin.Context) {
c.JSON(
http.StatusOK,
PingDto{
Message: "Pong!",
},
)
}
func handleGetMachines(dbConn *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
machines := db.GetMachines(dbConn)
fmt.Printf("%+v\n", machines)
c.JSON(
http.StatusOK,
GetMachinesResponse{
Machines: machines,
},
)
}
}
func initRouter(dbConn *gorm.DB) *gin.Engine {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.GET("/ping", handlePing)
router.GET("/", handleGetMachines(dbConn))
// routes.GET("/machines/:machineId", handleGetMachineDetails)
return router
}
func Serve(dbConn *gorm.DB) {
serverAddr := fmt.Sprintf("%s:%d", HOST, PORT)
router := initRouter(dbConn)
router.Run(serverAddr)
}