-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (39 loc) · 1009 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"net/http"
"github.com/chmike/securecookie"
"github.com/rs/zerolog/log"
)
type helloHandler struct {
}
func (h *helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sess := SessionGet(r.Context())
if !sess.IsValid() {
if user, pwd, ok := r.BasicAuth(); ok {
if user == "test" && pwd == "1234" {
sess.updated(42)
} else {
log.Error().Msgf("bad user (%s) and password (%s)", user, pwd)
w.Header().Set("WWW-Authenticate", "Basic")
w.WriteHeader(401)
return
}
} else {
w.Header().Set("WWW-Authenticate", "Basic")
w.WriteHeader(401)
return
}
}
fmt.Fprintf(w, "hello with secure cookie!")
}
func main() {
hello := helloHandler{}
key := securecookie.MustGenerateRandomKey()
mw := Middleware("localhost", "/", &log.Logger, key)
http.Handle("/", mw(&hello))
log.Info().Msg("server starting up")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Error().Err(err).Msg("server startup error")
}
}