mirror of
https://github.com/tarampampam/error-pages.git
synced 2024-08-30 18:22:40 +00:00
35 lines
1.0 KiB
Go
35 lines
1.0 KiB
Go
package errorpage
|
|
|
|
import (
|
|
"github.com/tarampampam/error-pages/internal/config"
|
|
"github.com/tarampampam/error-pages/internal/http/core"
|
|
"github.com/tarampampam/error-pages/internal/options"
|
|
"github.com/tarampampam/error-pages/internal/tpl"
|
|
"github.com/valyala/fasthttp"
|
|
)
|
|
|
|
type (
|
|
templatePicker interface {
|
|
// Pick the template name for responding.
|
|
Pick() string
|
|
}
|
|
|
|
renderer interface {
|
|
Render(content []byte, props tpl.Properties) ([]byte, error)
|
|
}
|
|
)
|
|
|
|
// NewHandler creates handler for error pages serving.
|
|
func NewHandler(cfg *config.Config, p templatePicker, rdr renderer, opt options.ErrorPage) fasthttp.RequestHandler {
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
core.SetClientFormat(ctx, core.PlainTextContentType) // default content type
|
|
|
|
if code, ok := ctx.UserValue("code").(string); ok {
|
|
core.RespondWithErrorPage(ctx, cfg, p, rdr, code, fasthttp.StatusOK, opt)
|
|
} else { // will never occur
|
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
|
_, _ = ctx.WriteString("cannot extract requested code from the request")
|
|
}
|
|
}
|
|
}
|