pages-server/server/setup.go

28 lines
845 B
Go
Raw Normal View History

2021-12-05 14:09:21 +00:00
package server
import (
"net/http"
2022-08-28 14:21:37 +00:00
"strings"
"codeberg.org/codeberg/pages/server/cache"
2022-08-28 14:21:37 +00:00
"codeberg.org/codeberg/pages/server/context"
"codeberg.org/codeberg/pages/server/utils"
2021-12-05 14:09:21 +00:00
)
2022-08-28 14:21:37 +00:00
func SetupHTTPACMEChallengeServer(challengeCache cache.SetGetKey) http.HandlerFunc {
challengePath := "/.well-known/acme-challenge/"
2022-08-28 14:21:37 +00:00
return func(w http.ResponseWriter, req *http.Request) {
ctx := context.New(w, req)
if strings.HasPrefix(ctx.Path(), challengePath) {
challenge, ok := challengeCache.Get(utils.TrimHostPort(ctx.Host()) + "/" + string(strings.TrimPrefix(ctx.Path(), challengePath)))
if !ok || challenge == nil {
ctx.String("no challenge for this token", http.StatusNotFound)
}
2022-08-28 14:21:37 +00:00
ctx.String(challenge.(string))
} else {
ctx.Redirect("https://"+string(ctx.Host())+string(ctx.Path()), http.StatusMovedPermanently)
}
}
2021-12-05 14:09:21 +00:00
}