mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-18 19:16:58 +00:00
Allow serving custom error page (#393)
It might be useful for those self-hosting a Codeberg Pages instance to be able to serve a different error page than the one embedded with go:embed Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/393 Co-authored-by: Gnarwhal <git.aspect893@passmail.net> Co-committed-by: Gnarwhal <git.aspect893@passmail.net>
This commit is contained in:
parent
bef866faae
commit
23a8e83e80
3 changed files with 33 additions and 6 deletions
20
html/html.go
20
html/html.go
|
@ -3,6 +3,8 @@ package html
|
|||
import (
|
||||
_ "embed"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"text/template" // do not use html/template here, we sanitize the message before passing it to the template
|
||||
|
||||
"codeberg.org/codeberg/pages/server/context"
|
||||
|
@ -14,7 +16,7 @@ import (
|
|||
var errorPage string
|
||||
|
||||
var (
|
||||
errorTemplate = template.Must(template.New("error").Parse(errorPage))
|
||||
errorTemplate = template.Must(template.New("error").Parse(loadCustomTemplateOrDefault()))
|
||||
sanitizer = createBlueMondayPolicy()
|
||||
)
|
||||
|
||||
|
@ -51,3 +53,19 @@ func createBlueMondayPolicy() *bluemonday.Policy {
|
|||
|
||||
return p
|
||||
}
|
||||
|
||||
func loadCustomTemplateOrDefault() string {
|
||||
contents, err := os.ReadFile("custom/error.html")
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
wd, wdErr := os.Getwd()
|
||||
if wdErr != nil {
|
||||
log.Err(err).Msg("could not load custom error page 'custom/error.html'")
|
||||
} else {
|
||||
log.Err(err).Msgf("could not load custom error page '%v'", path.Join(wd, "custom/error.html"))
|
||||
}
|
||||
}
|
||||
return errorPage
|
||||
}
|
||||
return string(contents)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue