-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhttp.go
More file actions
31 lines (24 loc) · 762 Bytes
/
http.go
File metadata and controls
31 lines (24 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main
import (
"net/http"
"github.com/urfave/negroni"
)
func writeInternalServerErr(w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Internal Server Error!"))
}
func writeBadRequest(w http.ResponseWriter, message string) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - " + message))
}
func basicAuth(cfg Config) negroni.HandlerFunc {
return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
user, pass, _ := r.BasicAuth()
if cfg.Auth.Username != user || cfg.Auth.Password != pass {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(w, "Unauthorized.", http.StatusUnauthorized)
return
}
next(w, r)
})
}