Routes package

This commit is contained in:
Jan Zípek 2022-08-13 23:39:18 +02:00
parent f6de3fdfbf
commit 9d5c19f671
Signed by: kamen
GPG Key ID: A17882625B33AC31
4 changed files with 17 additions and 13 deletions

11
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"basic-sensor-receiver/app" "basic-sensor-receiver/app"
"basic-sensor-receiver/routes"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -23,11 +24,11 @@ func main() {
protected.Static("/js", "client/js") protected.Static("/js", "client/js")
protected.Static("/css", "client/css") protected.Static("/css", "client/css")
protected.GET("/api/sensors", server.GetSensors()) protected.GET("/api/sensors", routes.GetSensors(server))
protected.GET("/api/sensors/:sensor/values", server.HandleGetSensorValues()) protected.GET("/api/sensors/:sensor/values", routes.HandleGetSensorValues(server))
protected.POST("/api/sensors/:sensor/values", server.HandlePostSensorValues()) protected.POST("/api/sensors/:sensor/values", routes.HandlePostSensorValues(server))
protected.GET("/api/sensors/:sensor/config", server.GetSensorConfig()) protected.GET("/api/sensors/:sensor/config", routes.GetSensorConfig(server))
protected.PUT("/api/sensors/:sensor/config/:key", server.HandlePutSensorConfig()) protected.PUT("/api/sensors/:sensor/config/:key", routes.HandlePutSensorConfig(server))
router.Run(fmt.Sprintf("%s:%d", server.Config.Ip, server.Config.Port)) router.Run(fmt.Sprintf("%s:%d", server.Config.Ip, server.Config.Port))
} }

View File

@ -1,6 +1,7 @@
package app package routes
import ( import (
"basic-sensor-receiver/app"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -10,7 +11,7 @@ type sensorConfigValue struct {
Value string `json:"value"` Value string `json:"value"`
} }
func (s *Server) HandlePutSensorConfig() gin.HandlerFunc { func HandlePutSensorConfig(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
var configValue sensorConfigValue var configValue sensorConfigValue
sensor := c.Param("sensor") sensor := c.Param("sensor")
@ -30,7 +31,7 @@ func (s *Server) HandlePutSensorConfig() gin.HandlerFunc {
} }
} }
func (s *Server) GetSensorConfig() gin.HandlerFunc { func GetSensorConfig(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
sensor := c.Param("sensor") sensor := c.Param("sensor")

View File

@ -1,6 +1,7 @@
package app package routes
import ( import (
"basic-sensor-receiver/app"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -15,7 +16,7 @@ type getSensorQuery struct {
To int64 `form:"to"` To int64 `form:"to"`
} }
func (s *Server) HandlePostSensorValues() gin.HandlerFunc { func HandlePostSensorValues(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
var newValue postSensorValueBody var newValue postSensorValueBody
sensor := c.Param("sensor") sensor := c.Param("sensor")
@ -34,7 +35,7 @@ func (s *Server) HandlePostSensorValues() gin.HandlerFunc {
} }
} }
func (s *Server) HandleGetSensorValues() gin.HandlerFunc { func HandleGetSensorValues(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
var query getSensorQuery var query getSensorQuery

View File

@ -1,12 +1,13 @@
package app package routes
import ( import (
"basic-sensor-receiver/app"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func (s *Server) GetSensors() gin.HandlerFunc { func GetSensors(s *app.Server) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
sensors, err := s.Services.Sensors.GetList() sensors, err := s.Services.Sensors.GetList()