-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbasicauth.go
46 lines (38 loc) · 1.05 KB
/
basicauth.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
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"net/http"
"os"
"github.com/goburrow/melon"
"github.com/goburrow/melon/auth"
"github.com/goburrow/melon/core"
)
type app struct{}
func (*app) Initialize(*core.Bootstrap) {
}
func (*app) Run(conf interface{}, env *core.Environment) error {
authFunc := func(usr, pwd string) (auth.Principal, error) {
if usr == "admin" && pwd == "123" {
return auth.NewPrincipal(usr), nil
}
return nil, nil
}
indexFunc := func(w http.ResponseWriter, r *http.Request) {
principal := auth.Must(r)
fmt.Fprintln(w, "Hello", principal.Name())
}
authenticator := auth.NewBasicAuthenticator(authFunc)
env.Server.Register(auth.NewFilter(authenticator))
env.Server.Router.Handle("GET", "/", http.HandlerFunc(indexFunc))
return nil
}
// Run it:
// $ go run basicauth.go server config.json
//
// Open http://localhost:8080/ in a browser, it should show a password prompt.
// Use username: admin, password: 123. "Hello admin" can be seen in browser.
func main() {
if err := melon.Run(&app{}, os.Args[1:]); err != nil {
panic(err)
}
}