Igris/internal/api/http/setup.go

51 lines
1.4 KiB
Go

package http
import (
"backend/config"
"backend/docs"
httpHandlers "backend/internal/api/http/handlers"
"backend/internal/api/http/middlewares"
"backend/internal/app"
"fmt"
"log"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
httpSwagger "github.com/swaggo/http-swagger"
)
func Run(cfg config.Server, app *app.AppContainer) {
fiberApp := fiber.New()
// Serve static files (HTML, CSS, JS)
fiberApp.Static("/", "./")
api := fiberApp.Group("/api")
// register routes here
registerPublicRoutes(api, app)
docs.SwaggerInfo.Host = fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
docs.SwaggerInfo.BasePath = "/api"
docs.SwaggerInfo.Schemes = []string{"http", "https"}
api.Get("/swagger/*", adaptor.HTTPHandler(httpSwagger.Handler()))
api.Get("/hello", middlewares.JWTAuthMiddleware([]byte("Secret")), func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
log.Printf("Server starting on %s:%d", cfg.Host, cfg.Port)
log.Fatal(fiberApp.Listen(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)))
}
func registerPublicRoutes(router fiber.Router, app *app.AppContainer) {
authgroup := router.Group("/auth")
authService := app.AuthService()
authHandler := httpHandlers.NewAuthHandler(&authService)
// Register auth routes
authgroup.Post("/challenge", authHandler.GenerateChallenge)
authgroup.Post("/authenticate", authHandler.Authenticate)
}