Auto Redirect
All checks were successful
release-nightly / build-docker (push) Successful in 3m33s

This commit is contained in:
LittleSheep 2024-01-01 01:42:32 +08:00
parent 90ac125886
commit 9a5c5e9fca
3 changed files with 43 additions and 25 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/config
/letsencrypt

View File

@ -2,6 +2,7 @@ package hypertext
import (
jsoniter "github.com/json-iterator/go"
"strings"
"time"
"github.com/gofiber/fiber/v2"
@ -46,8 +47,23 @@ func RunServer(app *fiber.App, ports []string, securedPorts []string, pem string
for _, port := range ports {
port := port
go func() {
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 tls ports.")
log.Panic().Err(err).Msg("An error occurred when listening hypertext common ports.")
}
}
}()
}

View File

@ -3,8 +3,9 @@ debug:
hypertext:
sideload_ports:
- :81
sideload_secured_ports: []
sideload_secured_ports: [ ]
certificate:
redirect: false
sideload_key: ./cert.key
sideload_pem: ./cert.pem
key: ./cert.key
@ -14,7 +15,7 @@ hypertext:
max_qps: -1
ports:
- :8000
secured_ports: []
secured_ports: [ ]
paths:
configs: ./config
performance: