mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-18 18:39:42 +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
|
@ -84,6 +84,15 @@ This will trigger a build of the PR which will build a docker image to be used f
|
||||||
- `PAGES_DOMAIN` (default: `codeberg.page`): main domain for pages.
|
- `PAGES_DOMAIN` (default: `codeberg.page`): main domain for pages.
|
||||||
- `RAW_DOMAIN` (default: `raw.codeberg.page`): domain for raw resources (must be subdomain of `PAGES_DOMAIN`).
|
- `RAW_DOMAIN` (default: `raw.codeberg.page`): domain for raw resources (must be subdomain of `PAGES_DOMAIN`).
|
||||||
|
|
||||||
|
### Custom Error Page
|
||||||
|
|
||||||
|
A custom error page template can be served by creating `custom/error.html`.
|
||||||
|
Data available to the template includes:
|
||||||
|
|
||||||
|
- `{{ .StatusCode }}`: The HTTP status code (e.g. 404)
|
||||||
|
- `{{ .StatusText }}`: The textual name associated with the status code (e.g. Not Found)
|
||||||
|
- `{{ .Message }}`: The reason for the error
|
||||||
|
|
||||||
## Contributing to the development
|
## Contributing to the development
|
||||||
|
|
||||||
The Codeberg team is very open to your contribution.
|
The Codeberg team is very open to your contribution.
|
||||||
|
|
10
flake.lock
10
flake.lock
|
@ -5,11 +5,11 @@
|
||||||
"systems": "systems"
|
"systems": "systems"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1726560853,
|
"lastModified": 1731533236,
|
||||||
"narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=",
|
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||||
"owner": "numtide",
|
"owner": "numtide",
|
||||||
"repo": "flake-utils",
|
"repo": "flake-utils",
|
||||||
"rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a",
|
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
@ -20,8 +20,8 @@
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 0,
|
"lastModified": 0,
|
||||||
"narHash": "sha256-8fnFlXBgM/uSvBlLWjZ0Z0sOdRBesyNdH0+esxqizGc=",
|
"narHash": "sha256-x07g4NcqGP6mQn6AISXJaks9sQYDjZmTMBlKIvajvyc=",
|
||||||
"path": "/nix/store/rqicl40jqw5d93l43s1xq0bm6yy0gpx4-source",
|
"path": "/nix/store/2w8kz6zh3aq80f1dypiin222fry1rv51-source",
|
||||||
"type": "path"
|
"type": "path"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|
20
html/html.go
20
html/html.go
|
@ -3,6 +3,8 @@ package html
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"text/template" // do not use html/template here, we sanitize the message before passing it to the template
|
"text/template" // do not use html/template here, we sanitize the message before passing it to the template
|
||||||
|
|
||||||
"codeberg.org/codeberg/pages/server/context"
|
"codeberg.org/codeberg/pages/server/context"
|
||||||
|
@ -14,7 +16,7 @@ import (
|
||||||
var errorPage string
|
var errorPage string
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errorTemplate = template.Must(template.New("error").Parse(errorPage))
|
errorTemplate = template.Must(template.New("error").Parse(loadCustomTemplateOrDefault()))
|
||||||
sanitizer = createBlueMondayPolicy()
|
sanitizer = createBlueMondayPolicy()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,3 +53,19 @@ func createBlueMondayPolicy() *bluemonday.Policy {
|
||||||
|
|
||||||
return p
|
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…
Reference in a new issue