-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
41 lines (33 loc) · 861 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/savaki/dynastore"
)
func main() {
store, err := dynastore.New(dynastore.Path("/"), dynastore.HTTPOnly(), dynastore.MaxAge(900))
if err != nil {
log.Fatalln(err)
}
router := mux.NewRouter()
router.Path("/").HandlerFunc(withSession(store, "blah", hello))
fmt.Println("Starting server on port 3001")
log.Fatalln(http.ListenAndServe(":3001", router))
}
func withSession(store sessions.Store, name string, fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
session, _ := store.Get(req, name)
if session.IsNew {
session.Save(req, w)
}
defer session.Save(req, w)
fn(w, req)
}
}
func hello(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "hello world")
}