forked from apex/up-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
34 lines (27 loc) · 734 Bytes
/
main.go
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
32
33
34
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
addr := ":" + os.Getenv("PORT")
http.Handle("/", protect(http.HandlerFunc(hello)))
log.Fatal(http.ListenAndServe(addr, nil))
}
func protect(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
match := user == "tobi" && pass == "ferret"
if !ok || !match {
w.Header().Set("WWW-Authenticate", `Basic realm="Ferret Land"`)
http.Error(w, `Unauthorized: Use "tobi" and "ferret" :)`, http.StatusUnauthorized)
return
}
h.ServeHTTP(w, r)
})
}
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Access to the secret ferret land granted!")
}