Machines app
This commit is contained in:
54
machines/app/api/router.go
Normal file
54
machines/app/api/router.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user