From 185bd807500d235999c9df820788fd19057cfbff Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Wed, 24 May 2023 23:50:10 +0400 Subject: [PATCH] error50x made into universal error page. Also Error() handler was made that checks for cURL and Wget, and if find, then use built-in plaintext http.Error, and web.errorXXX otherwise. --- internal/http/handlers.go | 9 +++++++++ web/templates/{error50x.jade => errorXXX.jade} | 4 ++-- web/web.go | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) rename web/templates/{error50x.jade => errorXXX.jade} (78%) diff --git a/internal/http/handlers.go b/internal/http/handlers.go index 68f70dc..d09e7a3 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -233,6 +233,15 @@ func NotFound(w http.ResponseWriter, r *http.Request) { } } +func Error(w http.ResponseWriter, r *http.Request, code int, reason string) { + if strings.Contains(r.UserAgent(), "curl") || strings.Contains(r.UserAgent(), "Wget") { + http.Error(w, reason, code) + return + } + + web.ErrorXXX(utils.MainSite(r.Host), code, reason, w) +} + func RobotsTxt(w http.ResponseWriter, r *http.Request) { data, _ := web.AssetsGetFile("robots.txt") w.Write(data) diff --git a/web/templates/error50x.jade b/web/templates/errorXXX.jade similarity index 78% rename from web/templates/error50x.jade rename to web/templates/errorXXX.jade index 23e35e6..f9fb9c6 100644 --- a/web/templates/error50x.jade +++ b/web/templates/errorXXX.jade @@ -1,7 +1,7 @@ extends base.jade block head - :go:func Error50x(mainSite string, code int, errorMsg string) + :go:func ErrorXXX(mainSite string, code int, errorMsg string) style(type="text/css"). #error { font-size: 3.5rem; @@ -12,7 +12,7 @@ block head #error h1 { font-size: 8rem; } block header - h1 Internal Server Error + h1 Еггог block body section#error diff --git a/web/web.go b/web/web.go index 756bc32..5300acf 100644 --- a/web/web.go +++ b/web/web.go @@ -11,7 +11,7 @@ import ( //go:generate $GOPATH/bin/jade -pkg=web -writer templates/deleted.jade //go:generate $GOPATH/bin/jade -pkg=web -writer templates/uploaded.jade //go:generate $GOPATH/bin/jade -pkg=web -writer templates/error404.jade -//go:generate $GOPATH/bin/jade -pkg=web -writer templates/error50x.jade +//go:generate $GOPATH/bin/jade -pkg=web -writer templates/errorXXX.jade //go:embed assets var assetsDir embed.FS