aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/zenazn/goji/example/middleware.go
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2016-12-05 08:16:58 +0000
committerFelix Hanley <felix@userspace.com.au>2016-12-05 08:16:58 +0000
commitb049991a46a2f619344bd6e915745703864d0134 (patch)
treeec1d3897a7b69c7c63a3774d4c42dfbb8cb46432 /vendor/github.com/zenazn/goji/example/middleware.go
parente1c3d6f7db06d592538f1162b2b6b9f1b6efa330 (diff)
downloadgo-dict2rest-b049991a46a2f619344bd6e915745703864d0134.tar.gz
go-dict2rest-b049991a46a2f619344bd6e915745703864d0134.tar.bz2
Clean up source structure and update vendor versionsHEADmaster
Diffstat (limited to 'vendor/github.com/zenazn/goji/example/middleware.go')
-rw-r--r--vendor/github.com/zenazn/goji/example/middleware.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/vendor/github.com/zenazn/goji/example/middleware.go b/vendor/github.com/zenazn/goji/example/middleware.go
new file mode 100644
index 0000000..9652ebb
--- /dev/null
+++ b/vendor/github.com/zenazn/goji/example/middleware.go
@@ -0,0 +1,47 @@
+package main
+
+import (
+ "encoding/base64"
+ "net/http"
+ "strings"
+
+ "github.com/zenazn/goji/web"
+)
+
+// PlainText sets the content-type of responses to text/plain.
+func PlainText(h http.Handler) http.Handler {
+ fn := func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/plain")
+ h.ServeHTTP(w, r)
+ }
+ return http.HandlerFunc(fn)
+}
+
+// Nobody will ever guess this!
+const Password = "admin:admin"
+
+// SuperSecure is HTTP Basic Auth middleware for super-secret admin page. Shhhh!
+func SuperSecure(c *web.C, h http.Handler) http.Handler {
+ fn := func(w http.ResponseWriter, r *http.Request) {
+ auth := r.Header.Get("Authorization")
+ if !strings.HasPrefix(auth, "Basic ") {
+ pleaseAuth(w)
+ return
+ }
+
+ password, err := base64.StdEncoding.DecodeString(auth[6:])
+ if err != nil || string(password) != Password {
+ pleaseAuth(w)
+ return
+ }
+
+ h.ServeHTTP(w, r)
+ }
+ return http.HandlerFunc(fn)
+}
+
+func pleaseAuth(w http.ResponseWriter) {
+ w.Header().Set("WWW-Authenticate", `Basic realm="Gritter"`)
+ w.WriteHeader(http.StatusUnauthorized)
+ w.Write([]byte("Go away!\n"))
+}