-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
193 lines (161 loc) · 4.75 KB
/
config.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package phpnginx
import (
"bytes"
_ "embed"
"fmt"
"os"
"path/filepath"
"strconv"
"text/template"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:embed assets/nginx.conf
var NGINXConfTemplate string
//go:embed assets/nginx-fpm.conf
var NGINXFPMConfTemplate string
type NginxConfig struct {
UserServerConf string
UserHttpConf string
EnableHTTPS bool
DisableHTTPSRedirect bool
AppRoot string
WebDirectory string
FpmSocket string
}
type NginxFpmConfig struct {
FpmSocket string
}
type NginxConfigWriter struct {
logger scribe.Emitter
}
type NginxFpmConfigWriter struct {
logger scribe.Emitter
}
func NewNginxConfigWriter(logger scribe.Emitter) NginxConfigWriter {
return NginxConfigWriter{
logger: logger,
}
}
func NewFpmNginxConfigWriter(logger scribe.Emitter) NginxFpmConfigWriter {
return NginxFpmConfigWriter{
logger: logger,
}
}
func (c NginxConfigWriter) Write(workingDir string) (string, error) {
tmpl, err := template.New("nginx.conf").Parse(NGINXConfTemplate)
if err != nil {
return "", fmt.Errorf("failed to parse Nginx config template: %w", err)
}
data := NginxConfig{
AppRoot: workingDir,
}
var configFiles []string
// Configuration set by this buildpack
// If there's a user-provided Nginx conf, include it in the base configuration.
userServerConf := filepath.Join(workingDir, ".nginx.conf.d", "*-server.conf")
userServerMatches, err := filepath.Glob(userServerConf)
if err != nil {
// untested
return "", fmt.Errorf("failed to glob %s: %w", userServerConf, err)
}
if len(userServerMatches) > 0 {
data.UserServerConf = userServerConf
configFiles = append(configFiles, userServerMatches...)
c.logger.Debug.Subprocess(fmt.Sprintf("Including user-provided Nginx server configuration from: %s", userServerConf))
}
userHttpConf := filepath.Join(workingDir, ".nginx.conf.d", "*-http.conf")
userHttpMatches, err := filepath.Glob(userHttpConf)
if err != nil {
// untested
return "", fmt.Errorf("failed to glob %s: %w", userHttpConf, err)
}
if len(userHttpMatches) > 0 {
data.UserHttpConf = userHttpConf
configFiles = append(configFiles, userHttpMatches...)
c.logger.Debug.Subprocess(fmt.Sprintf("Including user-provided Nginx HTTP configuration from: %s", userHttpConf))
}
webDir := os.Getenv("BP_PHP_WEB_DIR")
if webDir == "" {
webDir = "htdocs"
}
data.WebDirectory = webDir
c.logger.Debug.Subprocess(fmt.Sprintf("Web directory: %s", webDir))
enableHTTPS := false
enableHTTPSStr, ok := os.LookupEnv("BP_PHP_NGINX_ENABLE_HTTPS")
if ok {
enableHTTPS, err = strconv.ParseBool(enableHTTPSStr)
if err != nil {
return "", fmt.Errorf("failed to parse $BP_PHP_NGINX_ENABLE_HTTPS into boolean: %w", err)
}
}
data.EnableHTTPS = enableHTTPS
c.logger.Debug.Subprocess(fmt.Sprintf("Enable NGINX HTTPS: %t", enableHTTPS))
enableHTTPSRedirect := true
enableHTTPSRedirectStr, ok := os.LookupEnv("BP_PHP_ENABLE_HTTPS_REDIRECT")
if ok {
enableHTTPSRedirect, err = strconv.ParseBool(enableHTTPSRedirectStr)
if err != nil {
return "", fmt.Errorf("failed to parse $BP_PHP_ENABLE_HTTPS_REDIRECT into boolean: %w", err)
}
}
data.DisableHTTPSRedirect = !enableHTTPSRedirect
c.logger.Debug.Subprocess(fmt.Sprintf("Enable HTTPS redirect: %t", enableHTTPSRedirect))
fpmSocket := "/tmp/php-fpm.socket"
data.FpmSocket = fpmSocket
c.logger.Debug.Subprocess(fmt.Sprintf("FPM socket: %s", fpmSocket))
var b bytes.Buffer
err = tmpl.Execute(&b, data)
if err != nil {
// not tested
return "", err
}
path := filepath.Join(workingDir, "nginx.conf")
err = os.WriteFile(path, b.Bytes(), 0600)
if err != nil {
return "", err
}
for _, file := range append(configFiles, path) {
info, err := os.Stat(file)
if err != nil {
return "", err
}
err = os.Chmod(file, info.Mode()|0060)
if err != nil {
return "", err
}
}
return path, nil
}
func (c NginxFpmConfigWriter) Write(workingDir string) (string, error) {
tmpl, err := template.New("nginx-fpm.conf").Parse(NGINXFPMConfTemplate)
if err != nil {
return "", fmt.Errorf("failed to parse Nginx-Fpm config template: %w", err)
}
// Configuration set by this buildpack
// If there's a user-provided Nginx conf, include it in the base configuration.
fpmSocket := "/tmp/php-fpm.socket"
c.logger.Debug.Subprocess(fmt.Sprintf("FPM socket: %s", fpmSocket))
data := NginxFpmConfig{
FpmSocket: fpmSocket,
}
var b bytes.Buffer
err = tmpl.Execute(&b, data)
if err != nil {
// not tested
return "", err
}
path := filepath.Join(workingDir, ".php.fpm.bp", "nginx-fpm.conf")
err = os.WriteFile(path, b.Bytes(), 0600)
if err != nil {
return "", err
}
info, err := os.Stat(path)
if err != nil {
return "", err
}
err = os.Chmod(path, info.Mode()|0040)
if err != nil {
return "", err
}
return path, nil
}