repeater/internal/api/router.go

69 lines
1.6 KiB
Go

package api
import (
"embed"
"io/fs"
"net/http"
"github.com/gin-gonic/gin"
"github.com/nemunaire/repeater/internal/api/handlers"
"github.com/nemunaire/repeater/internal/config"
"github.com/nemunaire/repeater/internal/models"
)
// SetupRouter creates and configures the Gin router
func SetupRouter(status *models.SystemStatus, cfg *config.Config, assets embed.FS) *gin.Engine {
// Set Gin to release mode (can be overridden with GIN_MODE env var)
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
// API routes
api := r.Group("/api")
{
// WiFi endpoints
wifi := api.Group("/wifi")
{
wifi.GET("/networks", handlers.GetWiFiNetworks)
wifi.GET("/scan", handlers.ScanWiFi)
wifi.POST("/connect", handlers.ConnectWiFi)
wifi.POST("/disconnect", handlers.DisconnectWiFi)
}
// Hotspot endpoints
hotspot := api.Group("/hotspot")
{
hotspot.POST("/config", handlers.ConfigureHotspot)
hotspot.POST("/toggle", func(c *gin.Context) {
handlers.ToggleHotspot(c, status)
})
}
// Device endpoints
api.GET("/devices", func(c *gin.Context) {
handlers.GetDevices(c, cfg)
})
// Status endpoint
api.GET("/status", func(c *gin.Context) {
handlers.GetStatus(c, status)
})
// Log endpoints
api.GET("/logs", handlers.GetLogs)
api.DELETE("/logs", handlers.ClearLogs)
}
// WebSocket endpoints
r.GET("/ws/logs", handlers.WebSocketLogs)
r.GET("/ws/wifi", handlers.WebSocketWifi)
// Serve static files
sub, err := fs.Sub(assets, "static")
if err != nil {
panic("Unable to access static directory: " + err.Error())
}
r.NoRoute(gin.WrapH(http.FileServer(http.FS(sub))))
return r
}