th7/web/gin.go

61 lines
953 B
Go

package web
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"th7/ports"
)
type GinAdapter struct {
router *gin.Engine
corePort ports.CorePort
}
func (g GinAdapter) registerEndpoints() {
g.router.GET("/channels", func(c *gin.Context) {
channels := g.corePort.GetChannels()
c.JSON(http.StatusOK, 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) *GinAdapter {
var adapter GinAdapter
adapter.corePort = corePort
adapter.router = gin.Default()
adapter.registerEndpoints()
return &adapter
}
func (g *GinAdapter) Run() {
g.router.Run()
}