-
Notifications
You must be signed in to change notification settings - Fork 0
/
escapezoo.go
50 lines (39 loc) · 1.12 KB
/
escapezoo.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 (
"net/http"
"os"
"github.com/gorilla/sessions"
)
var sessionKey = []byte(os.Getenv("SESSION_KEY"))
func main() {
http.HandleFunc("/", MyHandler)
http.ListenAndServe(":8080", nil)
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
// Get the cookie named "cookiepath" from the request
cookie, err := r.Cookie("zoo")
if err != nil {
http.Error(w, "Cookie not found", http.StatusBadRequest)
return
}
// Use the value of the "cookiepath" cookie as the cookie path for the session store
store := sessions.NewFilesystemStore(cookie.Value, sessionKey)
// Get a session from the store
session, err := store.Get(r, "gorilla")
if err != nil {
http.Error(w, "Error getting session", http.StatusInternalServerError)
return
}
// Set some session values
session.Values["foo"] = "bar"
session.Values[42] = 43
// Save the session
err = session.Save(r, w)
if err != nil {
http.Error(w, "Error saving session", http.StatusInternalServerError)
return
}
// Respond with success message
w.Header().Set("Content-Type", "text/plain")
//w.Write([]byte("Session data saved successfully!"))
}