From 3909ea714baf72fb7541a13b49ccf544b92e3837 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Sat, 19 Jun 2021 16:59:42 +0930 Subject: [PATCH] Add error page and favicon --- Webserver/Index.go | 27 +++++++++++++++++++++++++-- Webserver/Post.go | 25 ++++++++++++++++--------- Webserver/ServeFile.go | 17 +++++++++++------ Webserver/Webserver.go | 2 ++ web/css/main.css | 4 +++- web/html/error.gohtml | 3 +++ web/html/header.gohtml | 2 +- web/html/index-links.gohtml | 8 ++++++++ web/html/index.gohtml | 7 +++++++ web/html/post.gohtml | 7 ++++++- web/static/1622082948272.jpg | Bin 0 -> 646140 bytes web/static/favicon-16x16.png | Bin 0 -> 183 bytes web/static/favicon-32x32.png | Bin 0 -> 310 bytes web/static/favicon.ico | Bin 0 -> 15406 bytes 14 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 web/html/error.gohtml create mode 100644 web/html/index-links.gohtml create mode 100644 web/static/1622082948272.jpg create mode 100644 web/static/favicon-16x16.png create mode 100644 web/static/favicon-32x32.png create mode 100644 web/static/favicon.ico diff --git a/Webserver/Index.go b/Webserver/Index.go index 38f355a..c43f828 100644 --- a/Webserver/Index.go +++ b/Webserver/Index.go @@ -1,6 +1,7 @@ package Webserver import ( + "log" "net/http" "PersonalWebsite/Database" @@ -15,9 +16,31 @@ func ViewIndex(w http.ResponseWriter, r *http.Request) { v["PageView"] = "index-intro.gohtml" v["Posts"], e = Database.GetPostsList(5, 0) if e != nil { - // TODO: Handle this - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) } ServeTemplate(w, r, "html/index.gohtml", v) } + +func ViewLinks(w http.ResponseWriter, r *http.Request) { + var ( + v = make(map[string]interface{}) + ) + + v["PageView"] = "index-links.gohtml" + + ServeTemplate(w, r, "html/index.gohtml", v) + +} + +func ViewError(w http.ResponseWriter, r *http.Request) { + var ( + v = make(map[string]interface{}) + ) + + v["PageView"] = "error.gohtml" + + ServeTemplate(w, r, "html/index.gohtml", v) + +} diff --git a/Webserver/Post.go b/Webserver/Post.go index 91cbfbe..8bde5b6 100644 --- a/Webserver/Post.go +++ b/Webserver/Post.go @@ -6,6 +6,7 @@ import ( "strconv" "PersonalWebsite/Database" + "PersonalWebsite/Helper" "github.com/gorilla/mux" ) @@ -22,9 +23,15 @@ func ViewPost(w http.ResponseWriter, r *http.Request) { post, e = Database.GetPostById(urlParams["id"]) if e != nil { - // TODO: Forward 404 - log.Println("Could not get post") - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) + return + } + + post.Body, e = Helper.GetFileContents(post.HtmlPath) + if e != nil { + log.Println(e) + http.Redirect(w, r, "/error", 302) return } @@ -58,8 +65,8 @@ func ViewPostList(w http.ResponseWriter, r *http.Request, subject string) { } else { page, e = strconv.Atoi(keys[0]) if e != nil { - // TODO: Handle this - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) return } } @@ -68,15 +75,15 @@ func ViewPostList(w http.ResponseWriter, r *http.Request, subject string) { posts, e = Database.GetPostsListBySubject(limit, offset, subject) if e != nil { - // TODO: Handle this - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) return } postsCount, e = Database.GetPostCountBySubject(subject) if e != nil { - // TODO: Handle this - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) return } diff --git a/Webserver/ServeFile.go b/Webserver/ServeFile.go index 9b81784..311fdba 100644 --- a/Webserver/ServeFile.go +++ b/Webserver/ServeFile.go @@ -1,6 +1,7 @@ package Webserver import ( + "log" "net/http" "path" "text/template" @@ -18,9 +19,11 @@ var ( "html/header.gohtml", "html/sidebar.gohtml", "html/index-intro.gohtml", + "html/index-links.gohtml", "html/index-post-list.gohtml", "html/post-list.gohtml", "html/post.gohtml", + "html/error.gohtml", } ) @@ -42,8 +45,9 @@ func ServeTemplate(w http.ResponseWriter, r *http.Request, mainFile string, v ma v["SidebarLinks"], e = Database.GetAllSidebarLinks() if e != nil { - // TODO: Handle - panic(e) + log.Println(e) + http.Redirect(w, r, "/error", 302) + return } files = []string{webRootJoin(mainFile)} @@ -64,14 +68,15 @@ func ServeTemplate(w http.ResponseWriter, r *http.Request, mainFile string, v ma }, ).ParseFiles(files...) if e != nil { - // TODO: Handle this - panic(e) + log.Println(e) + http.Redirect(w, r, "/error", 302) } w.Header().Set("Content-type", "text/html") e = tpl.Execute(w, v) if e != nil { - // TODO: Handle this - panic(e) + log.Println(e) + http.Redirect(w, r, "/error", 302) + return } } diff --git a/Webserver/Webserver.go b/Webserver/Webserver.go index 0777543..34d3f50 100644 --- a/Webserver/Webserver.go +++ b/Webserver/Webserver.go @@ -58,9 +58,11 @@ func Start() error { // Interface endpoints r.HandleFunc("/", ViewIndex) + r.HandleFunc("/links", ViewLinks) r.HandleFunc("/programming", ViewPostListProgramming) r.HandleFunc("/pentesting", ViewPostListPentesting) r.HandleFunc("/post/{id}", ViewPost) + r.HandleFunc("/error", ViewError) // Administration r.HandleFunc("/admin", AdminView) diff --git a/web/css/main.css b/web/css/main.css index 26f0c88..9f04fb3 100644 --- a/web/css/main.css +++ b/web/css/main.css @@ -243,7 +243,9 @@ pre::-webkit-scrollbar-color { } .post-icon { - max-width: 200px !important; + max-width: 50% !important; + max-height: 20em !important; + padding-bottom: 2em; } .icon-div { diff --git a/web/html/error.gohtml b/web/html/error.gohtml new file mode 100644 index 0000000..276dfd6 --- /dev/null +++ b/web/html/error.gohtml @@ -0,0 +1,3 @@ +

Whoops! It looks like you've run into an error!

+ +

Please click here to go back to the home screen

diff --git a/web/html/header.gohtml b/web/html/header.gohtml index 2ecdb50..4a3035a 100644 --- a/web/html/header.gohtml +++ b/web/html/header.gohtml @@ -7,7 +7,7 @@