-
Notifications
You must be signed in to change notification settings - Fork 50
/
host.go
81 lines (70 loc) · 2.05 KB
/
host.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
77
78
79
80
81
package gofast
import (
"bytes"
"log"
"net/http"
)
// Handler is implements http.Handler and provide logger changing method.
type Handler interface {
http.Handler
SetLogger(logger *log.Logger)
}
// NewHandler returns the default Handler implementation. This default Handler
// act as the "web server" component in fastcgi specification, which connects
// fastcgi "application" through the network/address and passthrough I/O as
// specified.
func NewHandler(sessionHandler SessionHandler, clientFactory ClientFactory) Handler {
return &defaultHandler{
sessionHandler: sessionHandler,
newClient: clientFactory,
}
}
// defaultHandler implements Handler
type defaultHandler struct {
sessionHandler SessionHandler
newClient ClientFactory
logger *log.Logger
}
// SetLogger implements Handler
func (h *defaultHandler) SetLogger(logger *log.Logger) {
h.logger = logger
}
// ServeHTTP implements http.Handler
func (h *defaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// TODO: separate dial logic to pool client / connection
c, err := h.newClient()
if err != nil {
http.Error(w, "failed to connect to FastCGI application", http.StatusBadGateway)
log.Printf("gofast: unable to connect to FastCGI application. %s",
err.Error())
return
}
// defer closing with error reporting
defer func() {
if c == nil {
return
}
// signal to close the client
// or the pool to return the client
if err = c.Close(); err != nil {
log.Printf("gofast: error closing client: %s",
err.Error())
}
}()
// handle the session
resp, err := h.sessionHandler(c, NewRequest(r))
if err != nil {
http.Error(w, "failed to process request", http.StatusInternalServerError)
log.Printf("gofast: unable to process request %s",
err.Error())
return
}
errBuffer := new(bytes.Buffer)
if err = resp.WriteTo(w, errBuffer); err != nil {
log.Printf("gofast: problem writing error buffer to response - %s", err)
}
if errBuffer.Len() > 0 {
log.Printf("gofast: error stream from application process %s",
errBuffer.String())
}
}