th7/web/gin.go

129 lines
2.6 KiB
Go
Raw Normal View History

2022-11-12 14:20:29 +00:00
package web
import (
2022-12-05 14:25:48 +00:00
"fmt"
2022-11-12 14:20:29 +00:00
"net/http"
2022-11-12 22:43:16 +00:00
"strconv"
2022-12-08 09:58:51 +00:00
"time"
2022-11-12 14:20:29 +00:00
"github.com/gin-gonic/gin"
2023-11-22 11:35:15 +00:00
"th7/data/config"
2022-12-08 09:58:51 +00:00
"th7/data/core"
2022-11-12 14:20:29 +00:00
"th7/ports"
)
type GinAdapter struct {
router *gin.Engine
corePort ports.CorePort
2023-11-22 11:35:15 +00:00
cfg config.Config
2022-12-05 14:25:48 +00:00
port int
2022-11-12 14:20:29 +00:00
}
2022-12-08 09:58:51 +00:00
func (g *GinAdapter) getRatio() core.Ratio {
return g.corePort.GetRatio()
}
2022-11-12 14:20:29 +00:00
2022-12-08 09:58:51 +00:00
func (g *GinAdapter) RatioHandler(c *gin.Context) {
ratio := g.getRatio()
c.JSON(http.StatusOK, ratio)
}
2022-12-05 13:19:54 +00:00
2022-12-08 09:58:51 +00:00
func (g *GinAdapter) getChannels() []core.Channel {
return g.corePort.GetChannels()
}
func (g *GinAdapter) ChannelsHandler(c *gin.Context) {
channels := g.getChannels()
c.JSON(http.StatusOK, channels)
}
func (g *GinAdapter) getChannelByID(id int) (core.Channel, error) {
return g.corePort.GetChannel(id)
}
func (g *GinAdapter) ChannelByIDHandler(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err,
})
return
}
channel, err := g.getChannelByID(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err,
})
return
}
c.JSON(http.StatusOK, channel)
}
2022-11-12 22:43:16 +00:00
2022-12-08 09:58:51 +00:00
func (g *GinAdapter) getTimestamp() string {
return time.Now().Format(time.RFC1123)
}
2022-11-12 22:43:16 +00:00
2022-12-08 09:58:51 +00:00
func (g *GinAdapter) TimestampHandler(c *gin.Context) {
ts := g.getTimestamp()
c.JSON(http.StatusOK, gin.H{
"time": ts,
2022-11-12 22:43:16 +00:00
})
2022-12-08 09:58:51 +00:00
}
func (g *GinAdapter) DataHandler(c *gin.Context) {
timestamp := g.getTimestamp()
ratio := g.getRatio()
channels := g.getChannels()
2022-11-12 22:43:16 +00:00
2022-12-08 09:58:51 +00:00
c.JSON(http.StatusOK, gin.H{
"time": timestamp,
"ratio": ratio,
"channels": channels,
})
2022-11-12 14:20:29 +00:00
}
2022-12-08 10:53:25 +00:00
func (g *GinAdapter) IndexHandler(c *gin.Context) {
c.HTML(http.StatusOK, "page_index", gin.H{
"title": "TH7",
})
}
2023-11-22 11:35:15 +00:00
func (g *GinAdapter) ConfigHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"config": g.cfg,
})
}
func NewGinAdapter(corePort ports.CorePort, cfg config.Config) *GinAdapter {
2022-11-12 14:20:29 +00:00
var adapter GinAdapter
adapter.corePort = corePort
2023-11-22 11:35:15 +00:00
adapter.port = cfg.Board.Port
adapter.cfg = cfg
2022-11-12 14:20:29 +00:00
2023-11-21 14:45:14 +00:00
//adapter.router = gin.Default()
adapter.router = gin.New()
2022-12-08 09:58:51 +00:00
2023-11-21 14:45:14 +00:00
// API
2022-12-08 09:58:51 +00:00
adapter.router.GET("/ratio", adapter.RatioHandler)
adapter.router.GET("/channels", adapter.ChannelsHandler)
adapter.router.GET("/channel/:id", adapter.ChannelByIDHandler)
adapter.router.GET("/time", adapter.TimestampHandler)
adapter.router.GET("/data", adapter.DataHandler)
2023-11-22 11:35:15 +00:00
adapter.router.GET("/config", adapter.ConfigHandler)
2022-11-12 14:20:29 +00:00
2022-12-08 10:53:25 +00:00
adapter.router.LoadHTMLGlob("./templates/*.tmpl")
adapter.router.Static("/assets", "./static")
adapter.router.GET("/", adapter.IndexHandler)
2022-11-12 14:20:29 +00:00
return &adapter
}
func (g *GinAdapter) Run() {
2022-12-05 14:25:48 +00:00
portStr := fmt.Sprintf(":%d", g.port)
g.router.Run(portStr)
2022-11-12 14:20:29 +00:00
}