-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (61 loc) · 1.79 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"log"
"net/http"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
redisStore "gopkg.in/boj/redistore.v1"
)
var (
sesh *redisStore.RediStore
seshErr error
)
var issueSession = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get a session.
session, err := sesh.Get(r, "session-key")
if err != nil {
log.Printf("Err getting user session: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if session == nil {
session = &sessions.Session{}
}
// Set some session values.
session.Values["UserID"] = "fakeUserID"
session.Values["JSON"] = ""
// Save.
if err = sessions.Save(r, w); err != nil {
log.Printf("Err saving user session: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Println(w.Header())
w.WriteHeader(http.StatusOK)
})
var requiresSession = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get a session.
session, err := sesh.Get(r, "session-key")
if err != nil {
log.Printf("Err getting user session: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if session == nil || session.Values["UserID"] == nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
w.WriteHeader(http.StatusOK)
})
func main() {
// Fetch new store.
sesh, seshErr = redisStore.NewRediStore(0, "tcp", ":6379", "", []byte("DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw=="))
if seshErr != nil {
panic(seshErr)
}
defer sesh.Close()
http.HandleFunc("/issue", issueSession)
http.HandleFunc("/require", requiresSession)
log.Println("Listening on localhost:8080")
log.Fatal(http.ListenAndServe("127.0.0.1:8080", context.ClearHandler(http.DefaultServeMux)))
}