-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
113 lines (96 loc) · 2.69 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
package phphttpd
import (
"bytes"
_ "embed"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"text/template"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:embed assets/default.conf
var DefaultHTTPDConfTemplate string
type HttpdConfig struct {
ServerAdmin string
DisableHTTPSRedirect bool
AppRoot string
WebDirectory string
FpmSocket string
UserInclude string
}
type Config struct {
logger scribe.Emitter
}
func NewConfig(logger scribe.Emitter) Config {
return Config{
logger: logger,
}
}
func (c Config) Write(layerPath, workingDir string) (string, error) {
tmpl, err := template.New("httpd.conf").Parse(DefaultHTTPDConfTemplate)
if err != nil {
return "", fmt.Errorf("failed to parse HTTPD config template: %w", err)
}
// Configuration set by this buildpack
// If there's a user-provided HTTPD conf, include it in the base configuration.
userPath := filepath.Join(workingDir, ".httpd.conf.d", "*.conf")
_, err = os.Stat(filepath.Join(workingDir, ".httpd.conf.d"))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
// untested
return "", fmt.Errorf("failed to stat %s/.httpd.conf.d: %w", workingDir, err)
}
userPath = ""
}
if userPath != "" {
c.logger.Debug.Subprocess(fmt.Sprintf("Including user-provided HTTPD configuration from: %s", userPath))
}
serverAdmin := os.Getenv("BP_PHP_SERVER_ADMIN")
if serverAdmin == "" {
serverAdmin = "admin@localhost"
}
c.logger.Debug.Subprocess(fmt.Sprintf("Server admin: %s", serverAdmin))
webDir := os.Getenv("BP_PHP_WEB_DIR")
if webDir == "" {
webDir = "htdocs"
}
c.logger.Debug.Subprocess(fmt.Sprintf("Web directory: %s", webDir))
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 pase $BP_PHP_ENABLE_HTTPS_REDIRECT into boolean: %w", err)
}
}
c.logger.Debug.Subprocess(fmt.Sprintf("Enable HTTPS redirect: %t", enableHTTPSRedirect))
fpmSocket := "127.0.0.1:9000"
data := HttpdConfig{
ServerAdmin: serverAdmin,
AppRoot: workingDir,
WebDirectory: webDir,
FpmSocket: fpmSocket,
DisableHTTPSRedirect: !enableHTTPSRedirect,
UserInclude: userPath,
}
var b bytes.Buffer
err = tmpl.Execute(&b, data)
if err != nil {
// not tested
return "", err
}
f, err := os.OpenFile(filepath.Join(layerPath, "httpd.conf"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
return "", err
}
defer f.Close()
_, err = io.Copy(f, &b)
if err != nil {
// not tested
return "", err
}
return f.Name(), nil
}