2024-01-06 17:56:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-06-22 05:04:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/api"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
|
2024-04-25 14:08:49 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/i18n"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/admin"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/ui"
|
2024-01-26 17:11:32 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-01-30 11:20:12 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
2024-04-20 06:05:50 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/favicon"
|
2024-01-30 11:20:12 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/idempotency"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
2024-04-20 06:05:50 +00:00
|
|
|
"github.com/gofiber/template/html/v2"
|
2024-01-26 17:11:32 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-01-06 17:56:32 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2024-01-26 17:11:32 +00:00
|
|
|
var A *fiber.App
|
2024-01-06 17:56:32 +00:00
|
|
|
|
2024-01-26 17:11:32 +00:00
|
|
|
func NewServer() {
|
2024-04-20 06:05:50 +00:00
|
|
|
templates := html.NewFileSystem(http.FS(pkg.FS), ".gohtml")
|
|
|
|
|
2024-01-26 17:11:32 +00:00
|
|
|
A = fiber.New(fiber.Config{
|
|
|
|
DisableStartupMessage: true,
|
|
|
|
EnableIPValidation: true,
|
2024-04-20 06:05:50 +00:00
|
|
|
ServerHeader: "Hydrogen.Passport",
|
|
|
|
AppName: "Hydrogen.Passport",
|
2024-01-31 03:34:34 +00:00
|
|
|
ProxyHeader: fiber.HeaderXForwardedFor,
|
2024-01-26 17:11:32 +00:00
|
|
|
JSONEncoder: jsoniter.ConfigCompatibleWithStandardLibrary.Marshal,
|
|
|
|
JSONDecoder: jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal,
|
2024-03-22 16:28:27 +00:00
|
|
|
EnablePrintRoutes: viper.GetBool("debug.print_routes"),
|
2024-04-20 06:05:50 +00:00
|
|
|
Views: templates,
|
|
|
|
ViewsLayout: "views/index",
|
2024-01-26 17:11:32 +00:00
|
|
|
})
|
2024-01-06 17:56:32 +00:00
|
|
|
|
2024-01-30 11:20:12 +00:00
|
|
|
A.Use(idempotency.New())
|
|
|
|
A.Use(cors.New(cors.Config{
|
|
|
|
AllowCredentials: true,
|
|
|
|
AllowMethods: strings.Join([]string{
|
|
|
|
fiber.MethodGet,
|
|
|
|
fiber.MethodPost,
|
|
|
|
fiber.MethodHead,
|
|
|
|
fiber.MethodOptions,
|
|
|
|
fiber.MethodPut,
|
|
|
|
fiber.MethodDelete,
|
|
|
|
fiber.MethodPatch,
|
|
|
|
}, ","),
|
|
|
|
AllowOriginsFunc: func(origin string) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
A.Use(logger.New(logger.Config{
|
|
|
|
Format: "${status} | ${latency} | ${method} ${path}\n",
|
|
|
|
Output: log.Logger,
|
2024-01-30 10:55:55 +00:00
|
|
|
}))
|
|
|
|
|
2024-06-22 05:04:21 +00:00
|
|
|
A.Use(exts.AuthMiddleware)
|
2024-04-20 06:05:50 +00:00
|
|
|
A.Use(i18n.I18nMiddleware)
|
|
|
|
|
|
|
|
A.Use(favicon.New(favicon.Config{
|
|
|
|
FileSystem: http.FS(pkg.FS),
|
|
|
|
File: "views/favicon.png",
|
|
|
|
URL: "/favicon.png",
|
2024-01-30 11:20:12 +00:00
|
|
|
}))
|
2024-04-20 06:05:50 +00:00
|
|
|
|
2024-06-22 05:04:21 +00:00
|
|
|
api.MapAPIs(A)
|
|
|
|
admin.MapAdminEndpoints(A)
|
|
|
|
ui.MapUserInterface(A)
|
2024-01-06 17:56:32 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 17:11:32 +00:00
|
|
|
func Listen() {
|
|
|
|
if err := A.Listen(viper.GetString("bind")); err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("An error occurred when starting server...")
|
2024-01-06 17:56:32 +00:00
|
|
|
}
|
|
|
|
}
|