/
fcgi.go
85 lines (75 loc) · 1.82 KB
/
fcgi.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
82
83
84
85
package wwhttp
import (
"context"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"net"
"net/http"
"net/http/fcgi"
"os"
"path"
)
type FcgiServer struct {
Router http.Handler
SocketPath string
// Dev only, should not be used in production.
GlobalRwx bool
}
func NewFcgiServerFromEnv(router http.Handler) *FcgiServer {
fcgiSocket := os.Getenv("WHALEBLAZER_FCGI_SOCK")
if fcgiSocket == "" {
panic("WHALEBLAZER_FCGI_SOCK is not defined")
}
return &FcgiServer{
Router: router,
SocketPath: fcgiSocket,
GlobalRwx: os.Getenv("FCGI_SOCK_GLOBAL_RWX") == "1",
}
}
func (srv *FcgiServer) Start(ctx context.Context) error {
if srv.SocketPath == "" {
return errors.Errorf("SocketPath cannot be empty")
}
if err := os.MkdirAll(path.Dir(srv.SocketPath), 0770); err != nil {
return err
}
listener, err := net.Listen("unix", srv.SocketPath)
if err != nil {
return err
}
// HACK: chmod the socket file for local dev so apache can write to the socket
// regardless of the user we run as.
// In production, the server should be run as the apache user instead.
if srv.GlobalRwx {
if err := os.Chmod(path.Dir(srv.SocketPath), 0777); err != nil {
return err
}
if err := os.Chmod(srv.SocketPath, 0777); err != nil {
return err
}
}
// Start server.
gos, gosCtx := errgroup.WithContext(ctx)
gos.Go(func() error {
return fcgi.Serve(listener, srv.Router)
})
// Close when cancelled.
closed := false
gos.Go(func() error {
select {
case <-ctx.Done():
closed = true
case <-gosCtx.Done():
}
_ = listener.Close()
return nil
})
// Wait.
err = gos.Wait()
// Suppress 'closing' error if we initiated the close.
// @todo would be nice to close gracefully like http.Server.Shutdown().
if err != nil && (!closed || !errors.Is(err, net.ErrClosed)) {
return err
}
return http.ErrServerClosed
}