Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 18, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/1da6acd6db362392a033 to your computer and use it in GitHub Desktop.
Save lettergram/1da6acd6db362392a033 to your computer and use it in GitHub Desktop.
// Shows user requested page
func viewHandler(w http.ResponseWriter, r *http.Request) {
// Parses URL to obtain title of file to add to .body
title := r.URL.Path[len("/"):]
// Load templatized page, given title
p, err := loadPage(title, r)
// If they do not have a cookie force them to login
if err != nil && !cookies.IsLoggedIn(r) {
http.Redirect(w, r, "/home", http.StatusFound)
return
// If they do have a cookie, they are logged in
} else if err != nil {
http.Redirect(w, r, "/login-succeeded", http.StatusFound)
return
}
// Generate template t
t, _ := template.ParseFiles("view.html")
// Write the template attributes of p (from load page) to t
t.Execute(w, p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment