mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 06:16:58 +00:00
start ...
This commit is contained in:
parent
9076bc3f75
commit
6f56fa97a1
11 changed files with 519 additions and 13 deletions
|
@ -2,23 +2,26 @@ package html
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// ReturnErrorPage sets the response status code and writes NotFoundPage to the response body, with "%status" replaced
|
||||
// with the provided status code.
|
||||
func ReturnErrorPage(ctx *fasthttp.RequestCtx, code int) {
|
||||
ctx.Response.SetStatusCode(code)
|
||||
ctx.Response.Header.SetContentType("text/html; charset=utf-8")
|
||||
message := fasthttp.StatusMessage(code)
|
||||
if code == fasthttp.StatusMisdirectedRequest {
|
||||
func errorMessage(statusCode int) string {
|
||||
message := http.StatusText(statusCode)
|
||||
|
||||
switch statusCode {
|
||||
case http.StatusMisdirectedRequest:
|
||||
message += " - domain not specified in <code>.domains</code> file"
|
||||
}
|
||||
if code == fasthttp.StatusFailedDependency {
|
||||
case http.StatusFailedDependency:
|
||||
message += " - target repo/branch doesn't exist or is private"
|
||||
}
|
||||
// TODO: use template engine?
|
||||
ctx.Response.SetBody(bytes.ReplaceAll(NotFoundPage, []byte("%status"), []byte(strconv.Itoa(code)+" "+message)))
|
||||
|
||||
return message
|
||||
}
|
||||
|
||||
// TODO: use template engine?
|
||||
func errorBody(statusCode int) []byte {
|
||||
return bytes.ReplaceAll(NotFoundPage,
|
||||
[]byte("%status"),
|
||||
[]byte(strconv.Itoa(statusCode)+" "+errorMessage(statusCode)))
|
||||
}
|
||||
|
|
20
html/error_fasthttp.go
Normal file
20
html/error_fasthttp.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
//go:build fasthttp
|
||||
|
||||
package html
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// ReturnErrorPage sets the response status code and writes NotFoundPage to the response body, with "%status" replaced
|
||||
// with the provided status code.
|
||||
func ReturnErrorPage(ctx *fasthttp.RequestCtx, code int) {
|
||||
ctx.Response.SetStatusCode(code)
|
||||
ctx.Response.Header.SetContentType("text/html; charset=utf-8")
|
||||
|
||||
// TODO: use template engine?
|
||||
ctx.Response.SetBody(errorBody(code))
|
||||
}
|
18
html/error_std.go
Normal file
18
html/error_std.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
//go:build !fasthttp
|
||||
|
||||
package html
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ReturnErrorPage sets the response status code and writes NotFoundPage to the response body, with "%status" replaced
|
||||
// with the provided status code.
|
||||
func ReturnErrorPage(resp *http.Response, code int) {
|
||||
resp.StatusCode = code
|
||||
resp.Header.Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
resp.Body = io.NopCloser(bytes.NewReader(errorBody(code)))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue