Skip to content

Instantly share code, notes, and snippets.

@joncalhoun
Last active August 8, 2018 12:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joncalhoun/ebf07cb652a91af6b481afc912ab8bff to your computer and use it in GitHub Desktop.
Save joncalhoun/ebf07cb652a91af6b481afc912ab8bff to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"net/http"
)
func main() {
dashboard := http.NewServeMux()
dashboard.HandleFunc("/dashboard/hi", printHi)
dashboard.HandleFunc("/dashboard/bye", printBye)
mux := http.NewServeMux()
mux.Handle("/dashboard/", requireUser(dashboard))
mux.HandleFunc("/", home)
http.ListenAndServe(":3000", addRequestID(mux))
}
func home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Homepage...")
}
func printHi(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hi! Your request ID is:", r.Context().Value("request_id"))
}
func printBye(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Bye! Your request ID is:", r.Context().Value("request_id"))
}
var requestID = 0
func nextRequestID() int {
requestID++
return requestID
}
func addRequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "request_id", nextRequestID())
next.ServeHTTP(w, r.WithContext(ctx))
})
}
type User struct{}
func lookupUser(r *http.Request) *User {
return &User{}
}
func requireUser(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := lookupUser(r)
if user == nil {
// No user so redirect to login
http.Redirect(w, r, "/login", http.StatusFound)
return
}
next.ServeHTTP(w, r)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment