-
Notifications
You must be signed in to change notification settings - Fork 4
/
saver.go
56 lines (44 loc) · 966 Bytes
/
saver.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
package session
import (
"bufio"
"errors"
"net"
"net/http"
"sync"
"github.com/gorilla/sessions"
)
// saver takes care of automatically saving the session
// when the response is written, this avoids having to
// call session.Save() in every handler.
type saver struct {
w http.ResponseWriter
req *http.Request
store *sessions.Session
moot sync.Mutex
}
func (s *saver) Header() http.Header {
s.moot.Lock()
defer s.moot.Unlock()
s.store.Save(s.req, s.w)
return s.w.Header()
}
func (s *saver) WriteHeader(code int) {
s.moot.Lock()
defer s.moot.Unlock()
s.store.Save(s.req, s.w)
s.w.WriteHeader(code)
}
func (s *saver) Write(b []byte) (int, error) {
s.moot.Lock()
defer s.moot.Unlock()
s.store.Save(s.req, s.w)
n, err := s.w.Write(b)
return n, err
}
func (s *saver) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := s.w.(http.Hijacker)
if !ok {
return nil, nil, errors.New("hijack not supported")
}
return h.Hijack()
}