2023-04-24 07:52:54 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-09-30 13:19:01 +02:00
|
|
|
"strconv"
|
2024-05-04 17:06:01 +02:00
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
"code.lila.network/adoralaura/go-urlsh/internal/config"
|
2024-05-04 17:06:01 +02:00
|
|
|
"code.lila.network/adoralaura/go-urlsh/internal/misc"
|
|
|
|
|
2023-04-24 07:52:54 +02:00
|
|
|
"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"
|
2024-05-04 17:06:01 +02:00
|
|
|
"github.com/gofiber/template/html/v2"
|
2024-09-30 13:19:01 +02:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/uptrace/bun"
|
2023-04-24 07:52:54 +02:00
|
|
|
)
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
func SetupFiber(db *bun.DB, logger *zerolog.Logger, config *config.Config) error {
|
2023-04-24 07:52:54 +02:00
|
|
|
var prod = false
|
|
|
|
|
2023-04-27 12:34:24 +02:00
|
|
|
var template_engine = html.New("./views", ".tmpl")
|
|
|
|
|
2023-04-24 07:52:54 +02:00
|
|
|
fiberapp := fiber.New(fiber.Config{
|
2023-04-28 18:24:09 +02:00
|
|
|
AppName: "go-urlsh",
|
2024-09-30 13:19:01 +02:00
|
|
|
ProxyHeader: config.ProxyHeader,
|
2023-04-28 18:24:09 +02:00
|
|
|
Prefork: prod,
|
2024-05-04 17:06:01 +02:00
|
|
|
ErrorHandler: misc.HandleError,
|
2023-04-28 18:24:09 +02:00
|
|
|
Views: template_engine,
|
|
|
|
CompressedFileSuffix: ".gz",
|
2023-04-24 07:52:54 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
fiberapp.Use(etag.New(etag.Config{Weak: false}))
|
|
|
|
fiberapp.Use(compress.New())
|
|
|
|
fiberapp.Use(recover.New())
|
|
|
|
|
2024-05-04 17:06:01 +02:00
|
|
|
addWebRoutes(fiberapp)
|
2023-04-27 19:03:28 +02:00
|
|
|
|
2023-04-24 07:52:54 +02:00
|
|
|
v1 := fiberapp.Group("/api/v1")
|
|
|
|
v1.Use(cors.New(cors.Config{AllowOrigins: "*"}))
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
addApiRoutes(v1, db, logger, config)
|
2023-04-27 10:37:42 +02:00
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
listenerr := fiberapp.Listen(config.HostIP.String() + ":" + strconv.Itoa(config.HostPort))
|
2023-04-24 07:52:54 +02:00
|
|
|
if listenerr != nil {
|
|
|
|
return listenerr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|