mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 22:06:57 +00:00
implement custom 404 pages
This commit is contained in:
parent
a2c5376d9a
commit
832a4ecd63
1 changed files with 24 additions and 2 deletions
|
@ -22,6 +22,11 @@ var upstreamIndexPages = []string{
|
||||||
"index.html",
|
"index.html",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// upstreamNotFoundPages lists pages that may be considered as custom 404 Not Found pages.
|
||||||
|
var upstreamNotFoundPages = []string{
|
||||||
|
"404.html",
|
||||||
|
}
|
||||||
|
|
||||||
// Options provides various options for the upstream request.
|
// Options provides various options for the upstream request.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
TargetOwner,
|
TargetOwner,
|
||||||
|
@ -124,6 +129,21 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ctx.Response.SetStatusCode(fasthttp.StatusNotFound)
|
ctx.Response.SetStatusCode(fasthttp.StatusNotFound)
|
||||||
|
if o.TryIndexPages {
|
||||||
|
// copy the o struct & try if a not found page exists
|
||||||
|
optionsForNotFoundPages := *o
|
||||||
|
optionsForNotFoundPages.TryIndexPages = false
|
||||||
|
optionsForNotFoundPages.appendTrailingSlash = false
|
||||||
|
for _, notFoundPage := range upstreamNotFoundPages {
|
||||||
|
optionsForNotFoundPages.TargetPath = "/" + notFoundPage
|
||||||
|
if optionsForNotFoundPages.Upstream(ctx, giteaRoot, giteaAPIToken, branchTimestampCache, fileResponseCache) {
|
||||||
|
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(o.BranchTimestamp.Unix(), 10), fileResponse{
|
||||||
|
exists: false,
|
||||||
|
}, fileCacheTimeout)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if res != nil {
|
if res != nil {
|
||||||
// Update cache if the request is fresh
|
// Update cache if the request is fresh
|
||||||
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(o.BranchTimestamp.Unix(), 10), fileResponse{
|
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(o.BranchTimestamp.Unix(), 10), fileResponse{
|
||||||
|
@ -166,8 +186,10 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st
|
||||||
}
|
}
|
||||||
ctx.Response.Header.SetContentType(mimeType)
|
ctx.Response.Header.SetContentType(mimeType)
|
||||||
|
|
||||||
// Everything's okay so far
|
if ( ctx.Response.StatusCode() != fasthttp.StatusNotFound ) {
|
||||||
ctx.Response.SetStatusCode(fasthttp.StatusOK)
|
// Everything's okay so far
|
||||||
|
ctx.Response.SetStatusCode(fasthttp.StatusOK)
|
||||||
|
}
|
||||||
ctx.Response.Header.SetLastModified(o.BranchTimestamp)
|
ctx.Response.Header.SetLastModified(o.BranchTimestamp)
|
||||||
|
|
||||||
log.Debug().Msg("response preparations")
|
log.Debug().Msg("response preparations")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue