Last active
May 15, 2016 20:43
-
-
Save kpurdon/915ae7007c756665d5e7b6fe5437e0e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"github.com/gorilla/context" | |
) | |
var ( | |
validUser = "test" | |
validPassword = "abc123" | |
) | |
func Auth(next http.Handler) http.Handler { | |
fn := func(w http.ResponseWriter, r *http.Request) { | |
user := r.FormValue("user") | |
if user == "" { | |
http.Error(w, "MISSING_ARG_USER", 400) | |
return | |
} | |
if user != validUser { | |
http.Error(w, "NOT_AUTHORIZED", 403) | |
return | |
} | |
password := r.FormValue("password") | |
if password == "" { | |
http.Error(w, "MISSING_ARG_PASSWORD", 400) | |
return | |
} | |
if password != validPassword { | |
http.Error(w, "NOT_AUTHORIZED", 403) | |
return | |
} | |
context.Set(r, "user", user) | |
next.ServeHTTP(w, r) | |
} | |
return http.HandlerFunc(fn) | |
} | |
func ping(w http.ResponseWriter, r *http.Request) { | |
user, ok := context.GetOk(r, "user") | |
if !ok { | |
http.Error(w, "INTERNAL_ERROR", 500) | |
} | |
log.Printf("current user: %s", user) | |
w.Write([]byte("OK\n")) | |
} | |
func main() { | |
http.Handle("/ping", Auth(http.HandlerFunc(ping))) | |
log.Println("listening on 8080") | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment