tlm-login-server/internal/app/fiber.go

73 lines
1.8 KiB
Go
Raw Permalink Normal View History

2023-12-01 21:19:44 +01:00
package app
import (
2023-12-06 23:25:34 +01:00
"code.lila.network/lauralani/tlm-login-server/internal/handlers"
2023-12-01 21:19:44 +01: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/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/rs/zerolog"
"os"
)
func SetupFiber() error {
var prod = false
var port = "3000"
var ip = ""
var proxyheader = "X-Forwarded-For"
//var template_engine = html.New("./views", ".tmpl")
if os.Getenv("PROD") == "true" {
prod = true
}
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
if os.Getenv("LISTEN") != "" {
ip = os.Getenv("LISTEN")
}
if os.Getenv("PROXYHEADER") != "" {
proxyheader = os.Getenv("PROXYHEADER")
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if os.Getenv("DEBUG") != "" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
fiberapp := fiber.New(fiber.Config{
AppName: "tlm-login-server",
ProxyHeader: proxyheader,
Prefork: prod,
ErrorHandler: HandleError,
//Views: template_engine,
CompressedFileSuffix: ".gz",
})
fiberapp.Use(logger.New(logger.Config{Format: "[${ip}]:${port} ${status} ${method} ${path}\n"}))
//fiberapp.Use(etag.New(etag.Config{Weak: false}))
fiberapp.Use(compress.New())
fiberapp.Use(recover.New())
//fiberapp.Static("/admin/", "./web")
2023-12-06 23:25:34 +01:00
fiberapp.Add("POST", "/api/users/register", handlers.POSTUserRegister)
fiberapp.Add("POST", "/api/users/login", handlers.POSTUserLogin)
fiberapp.Add("GET", "/confirm/email", handlers.GETUserEmailConfirm)
2023-12-01 21:19:44 +01:00
v1 := fiberapp.Group("/api/v1")
v1.Use(cors.New(cors.Config{AllowOrigins: "*"}))
fiberapp.Static("/", "./static")
2023-12-01 21:19:44 +01:00
listenerr := fiberapp.Listen(ip + ":" + port)
if listenerr != nil {
return listenerr
}
return nil
}