th7/api/http/gin.go

63 lines
1.6 KiB
Go
Raw Normal View History

2023-12-05 17:22:12 +00:00
package http
import (
"fmt"
"net/http"
"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) {
c.HTML(http.StatusOK, "page_index", gin.H{
"title": "TH7",
})
}
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
adapter.router.GET("/data/channel/:id", adapter.GetChannelByIDHandler)
adapter.router.GET("/data/channels", adapter.GetChannelsHandler)
adapter.router.GET("/data/ratio", adapter.GetRatioHandler)
adapter.router.GET("/data", adapter.GetDataHandler)
adapter.router.GET("/config/channel/:id", adapter.GetConfigChannelByIdHandler)
adapter.router.GET("/config/channels", adapter.GetConfigChannelsHandler)
adapter.router.GET("/config/device", adapter.GetConfigDeviceHandler)
adapter.router.GET("/config/db", adapter.GetConfigDBHandler)
adapter.router.POST("/config/device", adapter.SetConfigDeviceHandler)
adapter.router.POST("/config/channel/:id", adapter.SetConfigChannelByIdHandler)
adapter.router.POST("/config/db", adapter.SetConfigDBHandler)
adapter.router.LoadHTMLGlob("./templates/*.tmpl")
adapter.router.Static("/assets", "./static")
adapter.router.GET("/", adapter.IndexHandler)
return &adapter
}
func (g *GinAdapter) Run() {
portStr := fmt.Sprintf(":%d", g.port)
g.router.Run(portStr)
}