th7/web/gin.go

61 lines
953 B
Go
Raw Normal View History

2022-11-12 14:20:29 +00:00
package web
import (
"net/http"
2022-11-12 22:43:16 +00:00
"strconv"
2022-11-12 14:20:29 +00:00
"github.com/gin-gonic/gin"
"th7/ports"
)
type GinAdapter struct {
router *gin.Engine
corePort ports.CorePort
}
func (g GinAdapter) registerEndpoints() {
2022-11-12 22:43:16 +00:00
g.router.GET("/channels", func(c *gin.Context) {
2022-11-16 20:07:38 +00:00
channels := g.corePort.GetChannels()
c.JSON(http.StatusOK, channels)
2022-11-12 14:20:29 +00:00
})
2022-11-12 22:43:16 +00:00
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)
})
2022-11-12 14:20:29 +00:00
}
func NewGinAdapter(corePort ports.CorePort) *GinAdapter {
var adapter GinAdapter
adapter.corePort = corePort
adapter.router = gin.Default()
adapter.registerEndpoints()
return &adapter
}
func (g *GinAdapter) Run() {
g.router.Run()
}