2024-01-06 17:56:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-10-23 16:46:59 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
2024-07-16 10:09:18 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-06-24 15:06:20 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/admin"
|
2024-06-22 05:04:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/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-06-24 15:06:20 +00:00
|
|
|
type HTTPApp struct {
|
|
|
|
app *fiber.App
|
|
|
|
}
|
2024-04-20 06:05:50 +00:00
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
var IReader *sec.InternalTokenReader
|
|
|
|
|
2024-06-24 15:06:20 +00:00
|
|
|
func NewServer() *HTTPApp {
|
|
|
|
app := fiber.New(fiber.Config{
|
2024-01-26 17:11:32 +00:00
|
|
|
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-01-26 17:11:32 +00:00
|
|
|
})
|
2024-01-06 17:56:32 +00:00
|
|
|
|
2024-06-24 15:06:20 +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
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
2024-06-24 15:06:20 +00:00
|
|
|
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
|
|
|
}))
|
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
app.Use(sec.ContextMiddleware(IReader))
|
2024-06-24 15:06:20 +00:00
|
|
|
|
2024-07-16 10:09:18 +00:00
|
|
|
admin.MapAdminAPIs(app, "/api/admin")
|
|
|
|
api.MapAPIs(app, "/api")
|
2024-06-24 15:06:20 +00:00
|
|
|
|
|
|
|
return &HTTPApp{app}
|
2024-01-06 17:56:32 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 15:06:20 +00:00
|
|
|
func (v *HTTPApp) Listen() {
|
|
|
|
if err := v.app.Listen(viper.GetString("bind")); err != nil {
|
2024-01-26 17:11:32 +00:00
|
|
|
log.Fatal().Err(err).Msg("An error occurred when starting server...")
|
2024-01-06 17:56:32 +00:00
|
|
|
}
|
|
|
|
}
|