2023-04-14 11:58:32 -04:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2023-04-14 17:14:27 -04:00
|
|
|
"fmt"
|
2023-04-14 16:07:57 -04:00
|
|
|
|
2023-04-14 11:58:32 -04:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-05-23 18:49:58 -04:00
|
|
|
slogfiber "github.com/samber/slog-fiber"
|
2023-05-23 18:44:05 -04:00
|
|
|
"go.fifitido.net/ytdl-web/config"
|
|
|
|
"golang.org/x/exp/slog"
|
2023-04-14 11:58:32 -04:00
|
|
|
)
|
|
|
|
|
2023-05-23 18:44:05 -04:00
|
|
|
func Serve(cfg *config.Config) error {
|
2023-04-14 11:58:32 -04:00
|
|
|
engine := ViewsEngine()
|
2023-05-23 18:44:05 -04:00
|
|
|
app := fiber.New(fiber.Config{
|
|
|
|
Views: engine,
|
|
|
|
EnableTrustedProxyCheck: true,
|
|
|
|
TrustedProxies: cfg.HTTP.TrustedProxies,
|
|
|
|
DisableStartupMessage: true,
|
|
|
|
})
|
2023-04-24 08:14:36 -04:00
|
|
|
routes := &routes{}
|
2023-04-14 11:58:32 -04:00
|
|
|
|
2023-05-23 18:49:58 -04:00
|
|
|
logger := slog.With("module", "web")
|
|
|
|
|
|
|
|
app.Use(slogfiber.New(logger))
|
|
|
|
|
2023-04-24 08:14:36 -04:00
|
|
|
routes.Register(app)
|
2023-04-14 11:58:32 -04:00
|
|
|
|
2023-05-23 18:44:05 -04:00
|
|
|
listenAddr := fmt.Sprintf("%s:%d", cfg.HTTP.Listen, cfg.HTTP.Port)
|
|
|
|
|
2023-05-23 18:49:58 -04:00
|
|
|
logger.Info("Starting HTTP server", slog.String("host", cfg.HTTP.Listen), slog.Int("port", cfg.HTTP.Port))
|
2023-04-14 17:14:27 -04:00
|
|
|
|
|
|
|
return app.Listen(listenAddr)
|
2023-04-14 11:58:32 -04:00
|
|
|
}
|