2021-09-29 15:38:50 +00:00
|
|
|
package errorpage
|
|
|
|
|
|
|
|
import (
|
2022-01-27 12:29:49 +00:00
|
|
|
"github.com/tarampampam/error-pages/internal/config"
|
|
|
|
"github.com/tarampampam/error-pages/internal/http/core"
|
2021-09-29 15:38:50 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
2022-01-27 12:29:49 +00:00
|
|
|
type templatePicker interface {
|
|
|
|
// Pick the template name for responding.
|
|
|
|
Pick() string
|
|
|
|
}
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2021-10-06 17:38:00 +00:00
|
|
|
// NewHandler creates handler for error pages serving.
|
2022-01-27 12:29:49 +00:00
|
|
|
func NewHandler(cfg *config.Config, p templatePicker, showRequestDetails bool) fasthttp.RequestHandler {
|
2021-09-29 15:38:50 +00:00
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
2022-01-27 12:29:49 +00:00
|
|
|
core.SetClientFormat(ctx, core.PlainTextContentType) // default content type
|
2021-09-29 15:38:50 +00:00
|
|
|
|
|
|
|
if code, ok := ctx.UserValue("code").(string); ok {
|
2022-01-27 12:29:49 +00:00
|
|
|
core.RespondWithErrorPage(ctx, cfg, p, code, fasthttp.StatusOK, showRequestDetails)
|
|
|
|
} else { // will never occur
|
2021-10-06 17:38:00 +00:00
|
|
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
2022-01-27 12:29:49 +00:00
|
|
|
_, _ = ctx.WriteString("cannot extract requested code from the request")
|
2021-09-29 15:38:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|