Skip to content
Permalink
Browse files

11-Facelift add 404

  • Loading branch information...
bonfy committed Oct 13, 2018
1 parent ca5ccba commit f90f32b1233c1e76dfbdba1c89649fbdd05d46cb
Showing with 71 additions and 1 deletion.
  1. +12 −1 controller/home.go
  2. +53 −0 templates/404.html
  3. +6 −0 vm/notfound.go
@@ -15,6 +15,8 @@ type home struct{}

func (h home) registerRoutes() {
r := mux.NewRouter()
r.NotFoundHandler = http.HandlerFunc(notfoundHandler)

r.HandleFunc("/logout", middleAuth(logoutHandler))
r.HandleFunc("/login", loginHandler)
r.HandleFunc("/register", registerHandler)
@@ -25,6 +27,7 @@ func (h home) registerRoutes() {
r.HandleFunc("/explore", middleAuth(exploreHandler))
r.HandleFunc("/reset_password_request", resetPasswordRequestHandler)
r.HandleFunc("/reset_password/{token}", resetPasswordHandler)
r.HandleFunc("/404", notfoundHandler)
r.HandleFunc("/", middleAuth(indexHandler))

http.Handle("/", r)
@@ -130,7 +133,8 @@ func profileHandler(w http.ResponseWriter, r *http.Request) {
v, err := vop.GetVM(sUser, pUser, page, pageLimit)
if err != nil {
msg := fmt.Sprintf("user ( %s ) does not exist", pUser)
w.Write([]byte(msg))
setFlash(w, r, msg)
http.Redirect(w, r, "/404", http.StatusSeeOther)
return
}
templates[tpName].Execute(w, &v)
@@ -272,3 +276,10 @@ func resetPasswordHandler(w http.ResponseWriter, r *http.Request) {
}
}
}

func notfoundHandler(w http.ResponseWriter, r *http.Request) {
flash := getFlash(w, r)
message := vm.NotFoundMessage{Flash: flash}
tpl, _ := template.ParseFiles("templates/404.html")
tpl.Execute(w, &message)
}
@@ -0,0 +1,53 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Page Not Found</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
line-height: 1.2;
margin: 0;
}
html {
color: #888;
display: table;
font-family: sans-serif;
height: 100%;
text-align: center;
width: 100%;
}
body {
display: table-cell;
vertical-align: middle;
margin: 2em auto;
}
h1 {
color: #555;
font-size: 2em;
font-weight: 400;
}
p {
margin: 0 auto;
width: 280px;
}
@media only screen and (max-width: 280px) {
body, p {
width: 95%;
}
h1 {
font-size: 1.5em;
margin: 0 0 0.3em;
}
}
</style>
</head>
<body>
{{if .Flash}}
<h1>{{.Flash}}</h1>
{{else}}
<h1>Page Not Found</h1>
{{end}}
<p>Sorry, but the page you were trying to view does not exist.</p>
</body>
</html>
@@ -0,0 +1,6 @@
package vm

// NotFoundMessage struct
type NotFoundMessage struct {
Flash string
}

0 comments on commit f90f32b

Please sign in to comment.
You can’t perform that action at this time.