forked from zeebo/gostbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
35 lines (29 loc) · 767 Bytes
/
http.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
package main
import (
"net/http"
"thegoods.biz/httpbuf"
)
type handler func(http.ResponseWriter, *http.Request, *Context) error
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
//create the context
ctx, err := NewContext(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer ctx.Close()
//run the handler and grab the error, and report it
buf := new(httpbuf.Buffer)
err = h(buf, req, ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//save the session
if err = ctx.Session.Save(req, buf); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//apply the buffered response to the writer
buf.Apply(w)
}