init
This commit is contained in:
25
network/http/handlers/channel.go
Normal file
25
network/http/handlers/channel.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"go_oxspeak_server/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ChannelHandler struct {
|
||||
*Handler
|
||||
}
|
||||
|
||||
func AddChannelRoutes(rg *gin.RouterGroup, h *Handler) {
|
||||
channel := rg.Group("/channel")
|
||||
|
||||
handler := &ChannelHandler{h}
|
||||
channel.GET("/:id", handler.getChannels)
|
||||
|
||||
}
|
||||
|
||||
func (h *Handler) getChannels(c *gin.Context) {
|
||||
var users []models.User
|
||||
h.DB.Find(&users)
|
||||
|
||||
}
|
||||
31
network/http/handlers/main.go
Normal file
31
network/http/handlers/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"go_oxspeak_server/database"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func CreateRouter() *gin.Engine {
|
||||
router := gin.Default()
|
||||
|
||||
handler := &Handler{DB: database.DB}
|
||||
|
||||
api := router.Group("/api")
|
||||
{
|
||||
AddChannelRoutes(api, handler)
|
||||
}
|
||||
|
||||
router.GET("/health", handler.healthcheck)
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func (h *Handler) healthcheck(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"status": "ok"})
|
||||
}
|
||||
1
network/http/handlers/server.go
Normal file
1
network/http/handlers/server.go
Normal file
@@ -0,0 +1 @@
|
||||
package handlers
|
||||
32
network/http/middleware.go
Normal file
32
network/http/middleware.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// CORSMiddleware configure les headers CORS
|
||||
func CORSMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(204)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// LoggingMiddleware personnalisé (optionnel, Gin en a un par défaut)
|
||||
func LoggingMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
log.Printf("[HTTP] %s %s", c.Request.Method, c.Request.URL.Path)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
27
network/http/server.go
Normal file
27
network/http/server.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"go_oxspeak_server/network/http/handlers"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
router *gin.Engine
|
||||
addr string
|
||||
}
|
||||
|
||||
func NewServer(addr string) *Server {
|
||||
router := handlers.CreateRouter()
|
||||
|
||||
s := &Server{
|
||||
router: router,
|
||||
addr: addr,
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) Start() error {
|
||||
return s.router.Run(s.addr)
|
||||
}
|
||||
Reference in New Issue
Block a user