go-urlsh/internal/app/fiber.go

49 lines
1.3 KiB
Go

package app
import (
"strconv"
"code.lila.network/adoralaura/go-urlsh/internal/config"
"code.lila.network/adoralaura/go-urlsh/internal/misc"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/template/html/v2"
"github.com/rs/zerolog"
"github.com/uptrace/bun"
)
func SetupFiber(db *bun.DB, logger *zerolog.Logger, config *config.Config) error {
var prod = false
var template_engine = html.New("./views", ".tmpl")
fiberapp := fiber.New(fiber.Config{
AppName: "go-urlsh",
ProxyHeader: config.ProxyHeader,
Prefork: prod,
ErrorHandler: misc.HandleError,
Views: template_engine,
CompressedFileSuffix: ".gz",
})
fiberapp.Use(etag.New(etag.Config{Weak: false}))
fiberapp.Use(compress.New())
fiberapp.Use(recover.New())
addWebRoutes(fiberapp)
v1 := fiberapp.Group("/api/v1")
v1.Use(cors.New(cors.Config{AllowOrigins: "*"}))
addApiRoutes(v1, db, logger, config)
listenerr := fiberapp.Listen(config.HostIP.String() + ":" + strconv.Itoa(config.HostPort))
if listenerr != nil {
return listenerr
}
return nil
}