-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-handler.go
97 lines (81 loc) · 2.4 KB
/
php-handler.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
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @author Patrick Jahns <[email protected]>
*
* @copyright Copyright (c) 2019, Patrick Jahns.
* @license Apache
*
*/
package main
import (
"bytes"
"github.com/nuclio/nuclio-sdk-go"
"github.com/tomasen/fcgi_client"
"github.com/yookoala/gofast/tools/phpfpm"
"io/ioutil"
"os"
"strings"
)
func Handler(context *nuclio.Context, event nuclio.Event) (interface{}, error) {
context.Logger.Info("invocation started")
fpmSocket := "/var/task/fpm.sock"
fpmConfFile := "/var/task/php-fpm.conf"
fpm := phpfpm.NewProcess(os.Getenv("PHP_FPM_BIN"))
fpm.SetDatadir("/var/task")
fpm.ConfigFile = fpmConfFile
fpm.Listen = fpmSocket
if _, err := os.Stat(fpmSocket); os.IsNotExist(err) {
context.Logger.Info("starting fpm")
err := fpm.Start()
if err != nil {
context.Logger.Error("err:", err)
return nuclio.Response{StatusCode: 500}, nil
}
}
context.Logger.Info("connecting to fcgi")
fcgi, err := fcgiclient.Dial("unix", fpmSocket)
if err != nil {
context.Logger.Error("err:", err)
return nuclio.Response{StatusCode: 500}, nil
}
defer fcgi.Close()
scriptFilename := os.Getenv("PHP_SCRIPT")
env := map[string]string{
"CONTENT_LENGTH": event.GetHeaderString(string("Content-Length")),
"CONTENT_TYPE": event.GetContentType(),
"REQUEST_METHOD": event.GetMethod(),
"PATH_INFO": event.GetPath(),
"REQUEST_URI": "/" + event.GetHeaderString("X-Nuclio-Path"),
"SCRIPT_FILENAME": scriptFilename,
"SERVER_SOFTWARE": "nuclio-go / fcgiclient ",
}
for header, _ := range event.GetHeaders() {
env["HTTP_"+strings.Replace(strings.ToUpper(header), "-", "_", -1)] = event.GetHeaderString(header)
}
reqBody := bytes.NewReader(event.GetBody())
context.Logger.Info("sending request to fpm", env)
resp, err := fcgi.Request(env, reqBody)
if err != nil {
context.Logger.Error("err:", err)
return nuclio.Response{StatusCode: 500}, nil
}
respHeaders := make(map[string]interface{})
for k, _ := range resp.Header {
respHeaders[k] = resp.Header.Get(k)
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
context.Logger.Error("err:", err)
return nuclio.Response{StatusCode: 500}, nil
}
defer resp.Body.Close()
contentType := resp.Header.Get("Content-Type")
if len(contentType) == 0 {
contentType = "text/plain"
}
return nuclio.Response{
StatusCode: resp.StatusCode,
ContentType: contentType,
Body: []byte(content),
Headers: respHeaders,
}, nil
}