init
This commit is contained in:
63
main.go
Normal file
63
main.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// Hardcoded redirect rules
|
||||
var (
|
||||
// Host-based redirects
|
||||
hostRedirects = map[string]string{
|
||||
"mail.terrible.dev": "https://mail.tommyparnell.com",
|
||||
}
|
||||
|
||||
// Path-based redirects
|
||||
pathRedirects = map[string]string{
|
||||
"/test": "https://blog.terrible.dev",
|
||||
}
|
||||
)
|
||||
|
||||
func redirectHandler(c *fiber.Ctx) error {
|
||||
// Get the actual host, checking X-Forwarded-Host first
|
||||
host := c.Get("X-Forwarded-Host")
|
||||
if host == "" {
|
||||
host = c.Hostname()
|
||||
}
|
||||
|
||||
// Check for host-based redirects first
|
||||
if redirectURL, exists := hostRedirects[host]; exists {
|
||||
return c.Redirect(redirectURL, fiber.StatusMovedPermanently)
|
||||
}
|
||||
|
||||
// Get the actual path, checking X-Forwarded-Uri first
|
||||
path := c.Get("X-Forwarded-Uri")
|
||||
if path == "" {
|
||||
path = c.Path()
|
||||
}
|
||||
|
||||
// Check for path-based redirects
|
||||
if redirectURL, exists := pathRedirects[path]; exists {
|
||||
return c.Redirect(redirectURL, fiber.StatusMovedPermanently)
|
||||
}
|
||||
|
||||
// If no redirect rule matches, return 404
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Create Fiber app with minimal configuration for lowest memory usage
|
||||
app := fiber.New(fiber.Config{
|
||||
Prefork: false,
|
||||
DisableKeepalive: false,
|
||||
ServerHeader: "",
|
||||
AppName: "redirect",
|
||||
})
|
||||
|
||||
// Use a single handler function to minimize memory overhead
|
||||
app.All("*", redirectHandler)
|
||||
|
||||
// Start server on port 8080
|
||||
if err := app.Listen(":8080"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user