add GitHub Actions workflow and Dockerfile for building and pushing container image
Some checks failed
Build and Push Container Image / build-and-push-and-deploy (push) Failing after 1m51s
Some checks failed
Build and Push Container Image / build-and-push-and-deploy (push) Failing after 1m51s
This commit is contained in:
43
main.go
Normal file
43
main.go
Normal file
@ -0,0 +1,43 @@
|
||||
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()
|
||||
}
|
Reference in New Issue
Block a user