2023-11-16 15:06:59 +00:00
|
|
|
package hypertext
|
|
|
|
|
|
|
|
import (
|
2023-12-16 04:35:42 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2023-12-31 17:42:32 +00:00
|
|
|
"strings"
|
2023-11-25 15:06:23 +00:00
|
|
|
"time"
|
|
|
|
|
2023-11-16 15:06:59 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/limiter"
|
2023-11-25 15:06:23 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
2023-11-16 15:06:59 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitServer() *fiber.App {
|
|
|
|
app := fiber.New(fiber.Config{
|
|
|
|
AppName: "RoadSign",
|
2023-12-16 04:03:25 +00:00
|
|
|
ServerHeader: "RoadSign",
|
2023-11-16 15:06:59 +00:00
|
|
|
DisableStartupMessage: true,
|
|
|
|
EnableIPValidation: true,
|
2023-12-16 04:35:42 +00:00
|
|
|
JSONDecoder: jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal,
|
|
|
|
JSONEncoder: jsoniter.ConfigCompatibleWithStandardLibrary.Marshal,
|
2023-11-16 15:06:59 +00:00
|
|
|
Prefork: viper.GetBool("performance.prefork"),
|
|
|
|
BodyLimit: viper.GetInt("hypertext.limitation.max_body_size"),
|
|
|
|
})
|
|
|
|
|
2023-11-30 14:35:40 +00:00
|
|
|
if viper.GetBool("performance.request_logging") {
|
|
|
|
app.Use(logger.New(logger.Config{
|
|
|
|
Output: log.Logger,
|
|
|
|
Format: "[Proxies] [${time}] ${status} - ${latency} ${method} ${path}\n",
|
|
|
|
}))
|
|
|
|
}
|
2023-11-25 15:06:23 +00:00
|
|
|
|
2023-11-16 15:06:59 +00:00
|
|
|
if viper.GetInt("hypertext.limitation.max_qps") > 0 {
|
|
|
|
app.Use(limiter.New(limiter.Config{
|
|
|
|
Max: viper.GetInt("hypertext.limitation.max_qps"),
|
|
|
|
Expiration: 1 * time.Second,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
UseProxies(app)
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:23:40 +00:00
|
|
|
func RunServer(app *fiber.App, ports []string, securedPorts []string, pem string, key string) {
|
|
|
|
for _, port := range ports {
|
2023-11-16 15:06:59 +00:00
|
|
|
port := port
|
|
|
|
go func() {
|
2023-12-31 17:42:32 +00:00
|
|
|
if viper.GetBool("hypertext.certificate.redirect") {
|
|
|
|
redirector := fiber.New(fiber.Config{
|
|
|
|
AppName: "RoadSign",
|
|
|
|
ServerHeader: "RoadSign",
|
|
|
|
DisableStartupMessage: true,
|
|
|
|
EnableIPValidation: true,
|
|
|
|
})
|
|
|
|
redirector.All("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.Redirect(strings.ReplaceAll(string(c.Request().URI().FullURI()), "http", "https"))
|
|
|
|
})
|
|
|
|
if err := redirector.Listen(port); err != nil {
|
|
|
|
log.Panic().Err(err).Msg("An error occurred when listening hypertext common ports.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := app.Listen(port); err != nil {
|
|
|
|
log.Panic().Err(err).Msg("An error occurred when listening hypertext common ports.")
|
|
|
|
}
|
2023-11-16 15:06:59 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:23:40 +00:00
|
|
|
for _, port := range securedPorts {
|
2023-11-16 15:06:59 +00:00
|
|
|
port := port
|
|
|
|
go func() {
|
|
|
|
if err := app.ListenTLS(port, pem, key); err != nil {
|
|
|
|
log.Panic().Err(err).Msg("An error occurred when listening hypertext tls ports.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|