Files
new-domain-redir/main.go
Snowykami abfbb698f2
Some checks failed
Build and Push Container Image / build-and-push-and-deploy (push) Failing after 1m51s
add GitHub Actions workflow and Dockerfile for building and pushing container image
2025-06-16 00:43:17 +08:00

44 lines
1.1 KiB
Go

package main
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"strings"
)
const (
OldDomain = "liteyuki.icu"
NewDomain = "liteyuki.org"
)
func main() {
h := server.Default()
h.Use(func(ctx context.Context, c *app.RequestContext) {
host := string(c.Host())
if strings.HasSuffix(host, OldDomain) {
newHost := strings.TrimSuffix(host, OldDomain) + NewDomain
scheme := "https"
if proto := c.Request.Header.Get("X-Forwarded-Proto"); len(proto) > 0 {
scheme = string(proto)
} else if forwarded := c.Request.Header.Get("Forwarded"); len(forwarded) > 0 {
// Forwarded: proto=https;host=xxx
for _, part := range strings.Split(string(forwarded), ";") {
if strings.HasPrefix(part, "proto=") {
scheme = strings.TrimPrefix(part, "proto=")
break
}
}
}
newURL := scheme + "://" + newHost + string(c.Request.RequestURI())
c.Redirect(301, []byte(newURL))
c.Abort()
return
}
})
h.GET("*", func(ctx context.Context, c *app.RequestContext) {
c.JSON(200, map[string]interface{}{"message": "pong"})
})
h.Spin()
}