th7/api/http/gin.go

60 lines
1.5 KiB
Go
Raw Normal View History

2023-12-05 17:22:12 +00:00
package http
import (
"fmt"
"github.com/gin-gonic/gin"
"th7/data/config"
"th7/ports"
)
type GinAdapter struct {
router *gin.Engine
corePort ports.CorePort
cfg config.Config
port int
}
func (g *GinAdapter) IndexHandler(c *gin.Context) {
2023-12-06 19:39:51 +00:00
c.File("./static/index.html")
2023-12-05 17:22:12 +00:00
}
func NewGinAdapter(corePort ports.CorePort, cfg config.Config) *GinAdapter {
var adapter GinAdapter
adapter.corePort = corePort
adapter.port = cfg.API[0].Port
adapter.cfg = cfg
//gin.SetMode(gin.ReleaseMode)
adapter.router = gin.New()
// API
2023-12-06 19:39:51 +00:00
adapter.router.GET("/v1/data/channel/:id", adapter.GetChannelByIDHandler)
adapter.router.GET("/v1/data/channels", adapter.GetChannelsHandler)
adapter.router.GET("/v1/data/ratio", adapter.GetRatioHandler)
adapter.router.GET("/v1/data", adapter.GetDataHandler)
2023-12-05 17:22:12 +00:00
2023-12-06 19:39:51 +00:00
adapter.router.GET("/v1/config/channel/:id", adapter.GetConfigChannelByIdHandler)
adapter.router.GET("/v1/config/channels", adapter.GetConfigChannelsHandler)
adapter.router.GET("/v1/config/device", adapter.GetConfigDeviceHandler)
adapter.router.GET("/v1/config/db", adapter.GetConfigDBHandler)
2023-12-05 17:22:12 +00:00
2023-12-06 19:39:51 +00:00
adapter.router.POST("/v1/config/device", adapter.SetConfigDeviceHandler)
adapter.router.POST("/v1/config/channel/:id", adapter.SetConfigChannelByIdHandler)
adapter.router.POST("/v1/config/db", adapter.SetConfigDBHandler)
2023-12-05 17:22:12 +00:00
2023-12-06 19:39:51 +00:00
// TH7 demo code. html file and javascript
2023-12-05 17:22:12 +00:00
adapter.router.GET("/", adapter.IndexHandler)
2023-12-06 19:39:51 +00:00
adapter.router.Static("/assets", "./static")
2023-12-05 17:22:12 +00:00
return &adapter
}
func (g *GinAdapter) Run() {
portStr := fmt.Sprintf(":%d", g.port)
g.router.Run(portStr)
}