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 8180461d..7808b059 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 00000000..fc179dd9 --- /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 e2fd49c8..a4dc914f 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 dc3ce5e4..a2785455 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) {