From 9c7b79403e1d464fbfec0d1b01df6dbaeedb7977 Mon Sep 17 00:00:00 2001 From: Wim Brand Date: Mon, 9 Jul 2018 21:41:17 +0200 Subject: [PATCH] Just a whole bunch of fixing --- pixelapi/file.go | 2 +- pixelapi/list.go | 2 +- pixelapi/misc.go | 2 +- pixelapi/pixelapi.go | 6 +- pixelapi/user.go | 8 +- res/static/res/style/apidoc.css | 76 ------------------- res/static/res/style/form.css | 35 --------- res/static/res/style/layout.css | 61 +++++++-------- res/static/res/style/paste.css | 14 ---- res/static/res/style/season.css | 1 - res/template/404.html | 25 ++++++ res/template/500.html | 26 +++++++ res/template/account/file_manager.html | 24 ++++++ res/template/account/login.html | 8 +- res/template/account/register.html | 24 +++--- .../{filemanager.html => user_files.html} | 7 +- .../account/{user.html => user_home.html} | 5 +- res/template/apidoc.html | 25 +++++- res/template/error.html | 23 ------ res/template/fragments/menu.html | 2 +- res/template/paste.html | 38 ++++------ webcontroller/listViewer.go | 11 +-- webcontroller/templateData.go | 9 ++- webcontroller/userAccount.go | 58 -------------- webcontroller/webcontroller.go | 36 +++++---- 25 files changed, 208 insertions(+), 320 deletions(-) delete mode 100644 res/static/res/style/apidoc.css delete mode 100644 res/static/res/style/form.css delete mode 100644 res/static/res/style/paste.css delete mode 100644 res/static/res/style/season.css create mode 100644 res/template/404.html create mode 100644 res/template/500.html create mode 100644 res/template/account/file_manager.html rename res/template/account/{filemanager.html => user_files.html} (82%) rename res/template/account/{user.html => user_home.html} (87%) delete mode 100644 res/template/error.html diff --git a/pixelapi/file.go b/pixelapi/file.go index 4a19a50..dcfba47 100644 --- a/pixelapi/file.go +++ b/pixelapi/file.go @@ -26,7 +26,7 @@ type FileInfo struct { } // GetFileInfo gets the FileInfo from the pixeldrain API -func (p *PixelAPI) GetFileInfo(id string) (resp *FileInfo, err *Error) { +func (p *PixelAPI) GetFileInfo(id string) (resp *FileInfo, err error) { resp = &FileInfo{} err = p.jsonRequest("GET", p.apiEndpoint+"/file/"+id+"/info", resp) if err != nil { diff --git a/pixelapi/list.go b/pixelapi/list.go index 2ee3836..a75e8ac 100644 --- a/pixelapi/list.go +++ b/pixelapi/list.go @@ -27,7 +27,7 @@ type ListFile struct { // GetList get a List from the pixeldrain API. Errors will be available through // List.Error. Standard error checks apply. -func (p *PixelAPI) GetList(id string) (resp *List, err *Error) { +func (p *PixelAPI) GetList(id string) (resp *List, err error) { resp = &List{} err = p.jsonRequest("GET", p.apiEndpoint+"/list/"+id, resp) if err != nil { diff --git a/pixelapi/misc.go b/pixelapi/misc.go index 4c19157..d702ee8 100644 --- a/pixelapi/misc.go +++ b/pixelapi/misc.go @@ -4,7 +4,7 @@ type Recaptcha struct { SiteKey string `json:"site_key"` } -func (p *PixelAPI) GetRecaptcha() (resp *Recaptcha, err *Error) { +func (p *PixelAPI) GetRecaptcha() (resp *Recaptcha, err error) { err = p.jsonRequest("GET", p.apiEndpoint+"/misc/recpatcha", resp) if err != nil { return nil, err diff --git a/pixelapi/pixelapi.go b/pixelapi/pixelapi.go index 4adaf1a..cc52de0 100644 --- a/pixelapi/pixelapi.go +++ b/pixelapi/pixelapi.go @@ -45,7 +45,7 @@ type SuccessResponse struct { Success bool `json:"success"` } -func (p *PixelAPI) jsonRequest(method, url string, target interface{}) *Error { +func (p *PixelAPI) jsonRequest(method, url string, target interface{}) error { req, err := http.NewRequest(method, url, nil) if err != nil { return &Error{ @@ -116,7 +116,7 @@ func (p *PixelAPI) getRaw(url string) (io.ReadCloser, error) { return resp.Body, err } -func (p *PixelAPI) postForm(url string, vals url.Values, target interface{}) *Error { +func (p *PixelAPI) postForm(url string, vals url.Values, target interface{}) error { req, err := http.NewRequest("POST", url, strings.NewReader(vals.Encode())) if err != nil { return &Error{ @@ -145,7 +145,7 @@ func (p *PixelAPI) postForm(url string, vals url.Values, target interface{}) *Er return parseJSONResponse(resp, target) } -func parseJSONResponse(resp *http.Response, target interface{}) *Error { +func parseJSONResponse(resp *http.Response, target interface{}) error { var jdec = json.NewDecoder(resp.Body) var err error diff --git a/pixelapi/user.go b/pixelapi/user.go index f36f4ac..f2e92e4 100644 --- a/pixelapi/user.go +++ b/pixelapi/user.go @@ -21,7 +21,7 @@ type RegistrationError struct { // never be able to reset your password in case you forget it. captcha depends // on whether reCaptcha is enabled on the Pixeldrain server, this can be checked // through the GetRecaptcha function. -func (p *PixelAPI) UserRegister(username, email, password, captcha string) (resp *Registration, err *Error) { +func (p *PixelAPI) UserRegister(username, email, password, captcha string) (resp *Registration, err error) { resp = &Registration{} var form = url.Values{} form.Add("username", username) @@ -42,7 +42,7 @@ type UserInfo struct { } // UserInfo returns information about the logged in user. Requires an API key -func (p *PixelAPI) UserInfo() (resp *UserInfo, err *Error) { +func (p *PixelAPI) UserInfo() (resp *UserInfo, err error) { resp = &UserInfo{} err = p.jsonRequest("GET", p.apiEndpoint+"/user", resp) if err != nil { @@ -53,7 +53,7 @@ func (p *PixelAPI) UserInfo() (resp *UserInfo, err *Error) { // UserSessionDestroy destroys an API key so it can no longer be used to perform // actions -func (p *PixelAPI) UserSessionDestroy(key string) (resp *SuccessResponse, err *Error) { +func (p *PixelAPI) UserSessionDestroy(key string) (resp *SuccessResponse, err error) { resp = &SuccessResponse{} err = p.jsonRequest("DELETE", p.apiEndpoint+"/user/session", resp) if err != nil { @@ -67,7 +67,7 @@ type UserFiles struct { Files []FileInfo `json:"files"` } -func (p *PixelAPI) UserFiles(page, limit int) (resp *UserFiles, err *Error) { +func (p *PixelAPI) UserFiles(page, limit int) (resp *UserFiles, err error) { resp = &UserFiles{Files: make([]FileInfo, 0)} err = p.jsonRequest( "GET", diff --git a/res/static/res/style/apidoc.css b/res/static/res/style/apidoc.css deleted file mode 100644 index 364a7e9..0000000 --- a/res/static/res/style/apidoc.css +++ /dev/null @@ -1,76 +0,0 @@ -/* - Created on : Feb 19, 2017, 11:21:45 AM - Author : Fornax -*/ - -/* Global Definitions */ -.api_doc_details{ - border-top: 1px solid; - border-bottom: 1px solid; - margin-top: 10px; - margin: 15px -8px 15px -8px; -} -.api_doc_details > summary { - padding: 2px; - font-family: monospace; -} -.api_doc_details > summary > .method { - display: inline-block; - width: 80px; -} -.api_doc_details > div { - padding: 8px; -} - -table{ - border-collapse: collapse; - width: 100%; -} - -tr{ - border-bottom: 1px #333 solid; -} - -tr > td { - padding: 6px; -} - -pre{ - padding: 2px; - border-bottom: 1px #333 solid; - overflow-x: scroll; -} - -h3{ - border-bottom: 1px #777 solid; -} - -/* GET requests */ -.api_doc_details.request_get{ - border-color: rgb(54, 54, 255); - background-color: rgba(32, 32, 255, 0.2); -} - -/* POST requests */ -.api_doc_details.request_post{ - border-color: #00d000; - background-color: rgba(0, 255, 0, 0.05); -} - -/* DELETE requests */ -.api_doc_details.request_delete{ - border-color: #B00000; - background-color: rgba(255, 0, 0, 0.05); -} - -/* PUT requests */ -.api_doc_details.request_put{ - border-color: #B06000; - background-color: rgba(255, 128, 0, 0.05); -} - -/* PATCH requests */ -.api_doc_details.request_patch{ - border-color: #6000B0; - background-color: rgba(128, 0, 255, 0.1); -} \ No newline at end of file diff --git a/res/static/res/style/form.css b/res/static/res/style/form.css deleted file mode 100644 index 7208642..0000000 --- a/res/static/res/style/form.css +++ /dev/null @@ -1,35 +0,0 @@ -/* - Created on : Aug 27, 2015, 9:31:34 PM - Author : Fornax -*/ - - -.text-warning{ - position: relative; - width: auto; - height: auto; - background: #656762 no-repeat top center; - border: #9FCF6C ridge 4px; - margin: 10px; - font-size: 18px; - color: #ff9090; -} - -.text-info{ - position: relative; - width: auto; - height: auto; - background: #656762 no-repeat top center; - border: #9FCF6C ridge 4px; - margin: 10px; -} - -.g-recaptcha{ - overflow:hidden; - width:298px; - height:74px; -} - -.g-recaptcha > div > div > iframe{ - margin:-1px 0px 0px -2px; -} \ No newline at end of file diff --git a/res/static/res/style/layout.css b/res/static/res/style/layout.css index da20ba0..1852d25 100644 --- a/res/static/res/style/layout.css +++ b/res/static/res/style/layout.css @@ -7,6 +7,8 @@ --highlight_border: inset 0px 0px 5px 1px var(--highlight_color), 0px 0px 1px 0px var(--highlight_color); } +html{height: 100%;} + body{ background-color: #111; background-repeat: repeat; @@ -20,26 +22,9 @@ body{ color: var(--text_color); } -html{ - height: 100%; -} - /* Page layout elements */ -#header{ - position: relative; - width: auto; - height: auto; - margin: 0 -8px 0 -8px; - box-sizing: border-box; - overflow: hidden; - background-color: #222; - text-align: center; - z-index: 101; - border-bottom: #606060 solid 1px; -} - -#header_image{ +.header_image{ width: 100%; max-width: 1000px; margin-top: 15px; @@ -131,6 +116,8 @@ html{ /* Common elements */ +h3{border-bottom: 1px #777 solid;} /* Differentiate it a bit, else it just looks like bold text */ + hr{ height: 8px; border: none; @@ -142,16 +129,9 @@ hr{ width: 12px; /* for vertical scrollbars */ height: 12px; /* for horizontal scrollbars */ } -::-webkit-scrollbar-track{ - background: rgba(0, 0, 0, 0); -} -::-webkit-scrollbar-thumb{ - /*background: rgba(0, 0, 0, 0.5);*/ - background-color: #555; -} -::-webkit-scrollbar-corner{ - background: transparent; -} +::-webkit-scrollbar-track {background: rgba(0, 0, 0, 0);} +::-webkit-scrollbar-thumb {background-color: #555;} +::-webkit-scrollbar-corner{background: transparent;} a{ color: var(--highlight_color); @@ -162,6 +142,22 @@ a:hover{ color: var(--highlight_color); } +.form{ + margin-left: auto; + margin-right: auto; + text-align: left; + max-width: 30em; +} +table:not(.form) {border-collapse: collapse; width: 100%;} +tr:not(.form) {border-bottom: 1px #333 solid;} +tr > td {padding: 6px;} + +pre{ + padding: 2px; + border-bottom: 1px #333 solid; + overflow-x: scroll; +} + .big_button{ height: 40px; width: 40%; @@ -243,9 +239,7 @@ a:hover{ /* Form fields */ -.form_input { - width: 100%; -} +.form_input {width: 100%;} /* BUTTONS */ button, @@ -293,10 +287,7 @@ select:active{ .button_red:active {background: linear-gradient(#61152F, #821C40) !important;} /* Dropdown list of the select tag */ -option{ - background-color: #404040; - color: #FFFFFF; -} +option{background-color: #404040; color: #FFFFFF;} /* TEXT FIELDS */ textarea, diff --git a/res/static/res/style/paste.css b/res/static/res/style/paste.css deleted file mode 100644 index 7870583..0000000 --- a/res/static/res/style/paste.css +++ /dev/null @@ -1,14 +0,0 @@ -/* - Created on : Jul 1, 2015, 8:02:38 PM - Author : Fornax -*/ - -.textarea{ - position: absolute; - top: 0; - left: 0; - height: 100%; - width: 100%; - border: none !important; - background: #202020; -} \ No newline at end of file diff --git a/res/static/res/style/season.css b/res/static/res/style/season.css deleted file mode 100644 index efb97ed..0000000 --- a/res/static/res/style/season.css +++ /dev/null @@ -1 +0,0 @@ -/* Nothing to see here */ \ No newline at end of file diff --git a/res/template/404.html b/res/template/404.html new file mode 100644 index 0000000..f1e59c1 --- /dev/null +++ b/res/template/404.html @@ -0,0 +1,25 @@ +{{define "404"}} + + + {{template "meta_tags" "Not Found"}} + + + +
+ {{template "menu" .}} +
+

This page does not exist!

+ If you came here by a link from this very same website you can tell + me about it on twitter. +

+ Either way, there's nothing to see here, so you'll have to + head over to the home page. +
+
+ Bye! + {{template "footer"}} +
+ {{template "analytics"}} + + +{{end}} diff --git a/res/template/500.html b/res/template/500.html new file mode 100644 index 0000000..8bcf266 --- /dev/null +++ b/res/template/500.html @@ -0,0 +1,26 @@ +{{define "500"}} + + + {{template "meta_tags" "Internal Server Error"}} + + + +
+ {{template "menu" .}} +
+

You broke pixeldrain

+ Great job. +

+ But not to worry, the engineering team has been pulled out of bed to + have a look at the issue. You can try again in a few minutes (maybe + hours, who knows?), or go back to the home page and + start over. +
+
+ Bye! + {{template "footer"}} +
+ {{template "analytics"}} + + +{{end}} diff --git a/res/template/account/file_manager.html b/res/template/account/file_manager.html new file mode 100644 index 0000000..cd351f1 --- /dev/null +++ b/res/template/account/file_manager.html @@ -0,0 +1,24 @@ +{{define "file_manager"}} + + + {{template "meta_tags" "File Manager"}} + + + + + {{template "menu" .}} + +
+ {{range .Other.Files}} + + {{.FileName}} + {{.FileName}} +
+ {{.DateUpload}} +
+ {{end}} + + {{template "analytics"}} + + +{{end}} diff --git a/res/template/account/login.html b/res/template/account/login.html index abbe13c..81141b9 100644 --- a/res/template/account/login.html +++ b/res/template/account/login.html @@ -12,16 +12,16 @@

Log in to your PixelDrain account

- - +
+ - + - +
Username
Password
diff --git a/res/template/account/register.html b/res/template/account/register.html index 0821051..dc62de4 100644 --- a/res/template/account/register.html +++ b/res/template/account/register.html @@ -14,36 +14,36 @@
- + - - + + - - + - + - + - + - + - + + +
Username
used for logging into your account

used for logging into your account

E-mail address
+
not required. your e-mail address will only be used for password resets and important account notifications

Password
Password verification
you need to enter your password twice so we can verify that you have not made any typing errors

Turing test
(Click the white box) @@ -52,14 +52,18 @@
the reCaptcha turing test verifies that you are not an evil robot that is trying to flood the website with fake accounts

+ +

diff --git a/res/template/account/filemanager.html b/res/template/account/user_files.html similarity index 82% rename from res/template/account/filemanager.html rename to res/template/account/user_files.html index 40cae17..322aaf7 100644 --- a/res/template/account/filemanager.html +++ b/res/template/account/user_files.html @@ -1,7 +1,7 @@ -{{define "file_manager"}} +{{define "user_files"}} - {{template "meta_tags" "File Manager"}} + {{template "meta_tags" "Files"}} @@ -14,7 +14,8 @@
- {{range .Other.Files}} + {{$files := .PixelAPI.UserFiles 0 1000}} + {{range $files.Files}} {{.FileName}} {{.FileName}} diff --git a/res/template/account/user.html b/res/template/account/user_home.html similarity index 87% rename from res/template/account/user.html rename to res/template/account/user_home.html index 602e10e..4d4f602 100644 --- a/res/template/account/user.html +++ b/res/template/account/user_home.html @@ -12,7 +12,8 @@

Your most recently uploaded files:


diff --git a/res/template/apidoc.html b/res/template/apidoc.html index f36e139..e070b2b 100644 --- a/res/template/apidoc.html +++ b/res/template/apidoc.html @@ -2,7 +2,30 @@ {{template "meta_tags" "API Documentation"}} - + diff --git a/res/template/error.html b/res/template/error.html deleted file mode 100644 index aa73502..0000000 --- a/res/template/error.html +++ /dev/null @@ -1,23 +0,0 @@ -{{define "error"}} - - - {{template "meta_tags" "Error"}} - - - -
- {{template "menu" .}} -
-

Some Error occurred

- Either you made a mistake, or I made a mistake. -

- Either way, there's nothing to see here, so you'll have to head over to the home page. -
-
- Bye! - {{template "footer"}} -
- {{template "analytics"}} - - -{{end}} diff --git a/res/template/fragments/menu.html b/res/template/fragments/menu.html index 7ecb480..86460bc 100644 --- a/res/template/fragments/menu.html +++ b/res/template/fragments/menu.html @@ -1,7 +1,7 @@ {{define "menu"}} @@ -57,4 +49,4 @@ {{template "analytics"}} -{{end}} \ No newline at end of file +{{end}} diff --git a/webcontroller/listViewer.go b/webcontroller/listViewer.go index 5e2a5b9..4cf6bf8 100644 --- a/webcontroller/listViewer.go +++ b/webcontroller/listViewer.go @@ -4,22 +4,23 @@ import ( "fmt" "net/http" + "fornaxian.com/pixeldrain-web/pixelapi" + "github.com/Fornaxian/log" "github.com/julienschmidt/httprouter" ) // ServeListViewer controller for GET /l/:id func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - var list, aerr = wc.api.GetList(p.ByName("id")) - if aerr != nil { - if aerr.ReqError { - log.Error("API request error occurred: %s", aerr.Value) + var list, err = wc.api.GetList(p.ByName("id")) + if err != nil { + if (err.(pixelapi.Error)).ReqError { + log.Error("API request error occurred: %s", (err.(pixelapi.Error)).Value) } wc.serveNotFound(w, r) return } - var err error var ogData OGData listdata := map[string]interface{}{ "id": list.ID, diff --git a/webcontroller/templateData.go b/webcontroller/templateData.go index 2c99f85..e44f085 100644 --- a/webcontroller/templateData.go +++ b/webcontroller/templateData.go @@ -15,6 +15,7 @@ type TemplateData struct { Authenticated bool Username string APIEndpoint template.URL + PixelAPI *pixelapi.PixelAPI Other interface{} Title string @@ -28,11 +29,11 @@ func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) } if key, err := wc.getAPIKey(r); err == nil { - var api = pixelapi.New(wc.conf.APIURLInternal, key) - uinf, err := api.UserInfo() + t.PixelAPI = pixelapi.New(wc.conf.APIURLInternal, key) + uinf, err := t.PixelAPI.UserInfo() if err != nil { // This session key doesn't work, delete it - log.Debug("Invalid API key '%s' passed", key) + log.Debug("Session check for key '%s' failed: %s", key, err) http.SetCookie(w, &http.Cookie{ Name: "pd_auth_key", Value: "", @@ -45,6 +46,8 @@ func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) // Authentication succeeded t.Authenticated = true t.Username = uinf.Username + } else { + t.PixelAPI = pixelapi.New(wc.conf.APIURLInternal, "") } return t diff --git a/webcontroller/userAccount.go b/webcontroller/userAccount.go index 0fc7705..17f6392 100644 --- a/webcontroller/userAccount.go +++ b/webcontroller/userAccount.go @@ -23,61 +23,3 @@ func (wc *WebController) serveLogout( http.Redirect(w, r, "/", http.StatusSeeOther) } - -func (wc *WebController) serveUserHome( - w http.ResponseWriter, - r *http.Request, - p httprouter.Params, -) { - var key string - var err error - if key, err = wc.getAPIKey(r); err != nil { - http.Redirect(w, r, "/", http.StatusSeeOther) - return - } - - var api = pixelapi.New(wc.conf.APIURLInternal, key) - files, aerr := api.UserFiles(0, 18) - if aerr != nil { - log.Error("Cannot get user files: %v", aerr) - wc.serveNotFound(w, r) - return - } - - templateData := wc.newTemplateData(w, r) - templateData.Other = files - - err = wc.templates.Get().ExecuteTemplate(w, "user_home", templateData) - if err != nil { - log.Error("Failed to execute template: %s", err) - } -} - -func (wc *WebController) serveUserFiles( - w http.ResponseWriter, - r *http.Request, - p httprouter.Params, -) { - var key string - var err error - if key, err = wc.getAPIKey(r); err != nil { - http.Redirect(w, r, "/", http.StatusTemporaryRedirect) - return - } - - var api = pixelapi.New(wc.conf.APIURLInternal, key) - files, aerr := api.UserFiles(0, 1000) - if aerr != nil { - log.Error("Cannot get user files: %v", aerr) - wc.serveNotFound(w, r) - return - } - - templateData := wc.newTemplateData(w, r) - templateData.Other = files - - err = wc.templates.Get().ExecuteTemplate(w, "file_manager", templateData) - if err != nil { - log.Error("Failed to execute template: %s", err) - } -} diff --git a/webcontroller/webcontroller.go b/webcontroller/webcontroller.go index 28d5050..3327126 100644 --- a/webcontroller/webcontroller.go +++ b/webcontroller/webcontroller.go @@ -36,39 +36,43 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon // Serve static files r.ServeFiles(prefix+"/res/*filepath", http.Dir(wc.staticResourceDir+"/res")) - r.GET(prefix+"/" /* */, wc.serveTemplate("home")) + r.GET(prefix+"/" /* */, wc.serveTemplate("home", false)) r.GET(prefix+"/favicon.ico" /* */, wc.serveFile("/favicon.ico")) r.GET(prefix+"/global.css" /* */, wc.globalCSSHandler) - r.GET(prefix+"/api" /* */, wc.serveTemplate("apidoc")) - r.GET(prefix+"/history" /* */, wc.serveTemplate("history_cookies")) + r.GET(prefix+"/api" /* */, wc.serveTemplate("apidoc", false)) + r.GET(prefix+"/history" /* */, wc.serveTemplate("history_cookies", false)) r.GET(prefix+"/u/:id" /* */, wc.serveFileViewer) r.GET(prefix+"/u/:id/preview" /**/, wc.serveFilePreview) r.GET(prefix+"/l/:id" /* */, wc.serveListViewer) - r.GET(prefix+"/t" /* */, wc.serveTemplate("paste")) + r.GET(prefix+"/t" /* */, wc.serveTemplate("paste", false)) - r.GET(prefix+"/register" /* */, wc.serveTemplate("register")) - r.GET(prefix+"/login" /* */, wc.serveTemplate("login")) - r.GET(prefix+"/logout" /* */, wc.serveTemplate("logout")) + r.GET(prefix+"/register" /* */, wc.serveTemplate("register", false)) + r.GET(prefix+"/login" /* */, wc.serveTemplate("login", false)) + r.GET(prefix+"/logout" /* */, wc.serveTemplate("logout", true)) r.POST(prefix+"/logout" /* */, wc.serveLogout) - r.GET(prefix+"/user" /* */, wc.serveUserHome) - r.GET(prefix+"/files" /* */, wc.serveUserFiles) + r.GET(prefix+"/user" /* */, wc.serveTemplate("user_home", true)) + r.GET(prefix+"/user/files" /* */, wc.serveTemplate("user_files", true)) r.NotFound = http.HandlerFunc(wc.serveNotFound) return wc } -func (wc *WebController) ReloadTemplates() { - wc.templates.ParseTemplates(false) -} - -func (wc *WebController) serveTemplate(tpl string) httprouter.Handle { +func (wc *WebController) serveTemplate( + tpl string, + requireAuth bool, +) httprouter.Handle { return func( w http.ResponseWriter, r *http.Request, p httprouter.Params, ) { - err := wc.templates.Get().ExecuteTemplate(w, tpl, wc.newTemplateData(w, r)) + var tpld = wc.newTemplateData(w, r) + if requireAuth && !tpld.Authenticated { + http.Redirect(w, r, "/login", http.StatusSeeOther) + return + } + err := wc.templates.Get().ExecuteTemplate(w, tpl, tpld) if err != nil { log.Error("Error executing template '%s': %s", tpl, err) } @@ -87,7 +91,7 @@ func (wc *WebController) serveFile(path string) httprouter.Handle { func (wc *WebController) serveNotFound(w http.ResponseWriter, r *http.Request) { log.Debug("Not Found: %s", r.URL) - wc.templates.Get().ExecuteTemplate(w, "error", wc.newTemplateData(w, r)) + wc.templates.Get().ExecuteTemplate(w, "404", wc.newTemplateData(w, r)) } func (wc *WebController) getAPIKey(r *http.Request) (key string, err error) {