Passport/pkg/internal/http/server.go

73 lines
1.9 KiB
Go
Raw Normal View History

2024-10-27 04:50:07 +00:00
package http
2024-01-06 17:56:32 +00:00
import (
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/authkit"
"strings"
"git.solsynth.dev/hypernet/passport/pkg/internal/http/admin"
"git.solsynth.dev/hypernet/passport/pkg/internal/http/api"
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"
"github.com/gofiber/fiber/v2/middleware/idempotency"
"github.com/gofiber/fiber/v2/middleware/logger"
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-10-27 04:50:07 +00:00
type App struct {
app *fiber.App
}
2024-04-20 06:05:50 +00:00
var IReader *sec.InternalTokenReader
2024-10-27 04:50:07 +00:00
func NewServer() *App {
app := fiber.New(fiber.Config{
2024-01-26 17:11:32 +00:00
DisableStartupMessage: true,
EnableIPValidation: true,
2024-10-26 16:06:23 +00:00
ServerHeader: "Hypernet.Passport",
AppName: "Hypernet.Passport",
ProxyHeader: fiber.HeaderXForwardedFor,
2024-01-26 17:11:32 +00:00
JSONEncoder: jsoniter.ConfigCompatibleWithStandardLibrary.Marshal,
JSONDecoder: jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal,
EnablePrintRoutes: viper.GetBool("debug.print_routes"),
2024-01-26 17:11:32 +00:00
})
2024-01-06 17:56:32 +00:00
app.Use(idempotency.New())
app.Use(cors.New(cors.Config{
2024-01-30 11:20:12 +00:00
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
},
}))
app.Use(logger.New(logger.Config{
2024-01-30 11:20:12 +00:00
Format: "${status} | ${latency} | ${method} ${path}\n",
Output: log.Logger,
2024-01-30 10:55:55 +00:00
}))
app.Use(sec.ContextMiddleware(IReader))
app.Use(authkit.ParseAccountMiddleware)
admin.MapAdminAPIs(app, "/api/admin")
api.MapAPIs(app, "/api")
2024-10-27 04:50:07 +00:00
return &App{app}
2024-01-06 17:56:32 +00:00
}
2024-10-27 04:50:07 +00:00
func (v *App) Listen() {
if err := v.app.Listen(viper.GetString("bind")); err != nil {
2024-10-27 04:50:07 +00:00
log.Fatal().Err(err).Msg("An error occurred when starting http...")
2024-01-06 17:56:32 +00:00
}
}