From 8b0db47e298a2cd13122a7d0ee060738cd575ae9 Mon Sep 17 00:00:00 2001 From: William Clark Date: Thu, 8 Dec 2022 09:58:51 +0000 Subject: [PATCH] redone web endpoints --- web/gin.go | 97 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 30 deletions(-) diff --git a/web/gin.go b/web/gin.go index 26b1041..bb7a227 100644 --- a/web/gin.go +++ b/web/gin.go @@ -4,9 +4,11 @@ import ( "fmt" "net/http" "strconv" + "time" "github.com/gin-gonic/gin" + "th7/data/core" "th7/ports" ) @@ -16,39 +18,69 @@ type GinAdapter struct { port int } -func (g GinAdapter) registerEndpoints() { +func (g *GinAdapter) getRatio() core.Ratio { + return g.corePort.GetRatio() +} - g.router.GET("/ratio", func(c *gin.Context) { - table := g.corePort.GetRatio() - c.JSON(http.StatusOK, table) +func (g *GinAdapter) RatioHandler(c *gin.Context) { + ratio := g.getRatio() + c.JSON(http.StatusOK, ratio) +} + +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) +} + +func (g *GinAdapter) getTimestamp() string { + return time.Now().Format(time.RFC1123) +} + +func (g *GinAdapter) TimestampHandler(c *gin.Context) { + ts := g.getTimestamp() + c.JSON(http.StatusOK, gin.H{ + "time": ts, }) +} - g.router.GET("/channels", func(c *gin.Context) { - channels := g.corePort.GetChannels() - c.JSON(http.StatusOK, channels) +func (g *GinAdapter) DataHandler(c *gin.Context) { + timestamp := g.getTimestamp() + ratio := g.getRatio() + channels := g.getChannels() + + c.JSON(http.StatusOK, gin.H{ + "time": timestamp, + "ratio": ratio, + "channels": channels, }) - - g.router.GET("/channel/:id", func(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.corePort.GetChannel(id) - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{ - "error": err, - }) - return - } - - c.JSON(http.StatusOK, channel) - - }) - } func NewGinAdapter(corePort ports.CorePort, port int) *GinAdapter { @@ -58,7 +90,12 @@ func NewGinAdapter(corePort ports.CorePort, port int) *GinAdapter { adapter.port = port adapter.router = gin.Default() - adapter.registerEndpoints() + + 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) return &adapter }