From 94bd05751ef87b62c952eca206bda026f713d30c Mon Sep 17 00:00:00 2001 From: Asaf Gartner Date: Tue, 4 May 2021 16:23:02 +0300 Subject: [PATCH] Added our 404 page --- src/config/config.go.example | 4 ++++ src/templates/src/404.html | 13 +++++++++++++ src/website/requesthandling.go | 10 ++++++++++ src/website/routes.go | 15 +++++++++++---- 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/templates/src/404.html diff --git a/src/config/config.go.example b/src/config/config.go.example index 8180461..7808b05 100644 --- a/src/config/config.go.example +++ b/src/config/config.go.example @@ -13,4 +13,8 @@ var Config = HMNConfig{ MinConn: 2, // Keep these low for dev, high for production MaxConn: 2, }, + Auth: AuthConfig{ + CookieDomain: ".handmade.local", + CookieSecure: false, + }, } diff --git a/src/templates/src/404.html b/src/templates/src/404.html new file mode 100644 index 0000000..fc179dd --- /dev/null +++ b/src/templates/src/404.html @@ -0,0 +1,13 @@ +{{ template "base.html" . }} + +{{ define "content" }} +
+

+ Hi there, {{ if .User }}{{ .User.Name }}{{ else }}visitor{{ end }}! +

+

+ We couldn't find what you were looking for:
+ {{ .Wanted }} +

+
+{{ end }} diff --git a/src/website/requesthandling.go b/src/website/requesthandling.go index e2fd49c..a4dc914 100644 --- a/src/website/requesthandling.go +++ b/src/website/requesthandling.go @@ -133,6 +133,16 @@ func (c *RequestContext) URL() *url.URL { return c.Req.URL } +func (c *RequestContext) FullUrl() string { + var scheme string // TODO(asaf): BEFORE RELEASE!! Fetch scheme from X-Forwarded-* headers or Forwarded header + if c.Req.TLS != nil { + scheme = "https://" + } else { + scheme = "http://" + } + return scheme + c.Req.Host + c.Req.URL.String() +} + func (c *RequestContext) GetFormValues() (url.Values, error) { err := c.Req.ParseForm() if err != nil { diff --git a/src/website/routes.go b/src/website/routes.go index dc3ce5e..a278545 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -1,7 +1,6 @@ package website import ( - "bytes" "context" "errors" "fmt" @@ -90,6 +89,7 @@ func getBaseData(c *RequestContext) templates.BaseData { if c.CurrentUser != nil { templateUser = &templates.User{ Username: c.CurrentUser.Username, + Name: c.CurrentUser.Name, Email: c.CurrentUser.Email, IsSuperuser: c.CurrentUser.IsSuperuser, IsStaff: c.CurrentUser.IsStaff, @@ -166,10 +166,17 @@ func ProjectCSS(c *RequestContext) ResponseData { } func FourOhFour(c *RequestContext) ResponseData { - return ResponseData{ - StatusCode: http.StatusNotFound, - Body: bytes.NewBufferString("go away\n"), + templateData := struct { + templates.BaseData + Wanted string + }{ + BaseData: getBaseData(c), + Wanted: c.FullUrl(), } + var res ResponseData + res.StatusCode = http.StatusNotFound + res.WriteTemplate("404.html", templateData, c.Perf) + return res } func LoadCommonWebsiteData(c *RequestContext) (bool, ResponseData) {