-
Notifications
You must be signed in to change notification settings - Fork 14
/
default_config_generator.go
79 lines (61 loc) · 1.84 KB
/
default_config_generator.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
package nginx
import (
"bytes"
_ "embed"
"fmt"
"io"
"os"
"path/filepath"
"text/template"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:embed assets/default.conf
var DefaultConfigTemplate string
type DefaultConfigGenerator struct {
logs scribe.Emitter
}
func NewDefaultConfigGenerator(logs scribe.Emitter) DefaultConfigGenerator {
return DefaultConfigGenerator{logs: logs}
}
func (g DefaultConfigGenerator) Generate(config Configuration) error {
g.logs.Process("Generating %s", config.NGINXConfLocation)
t := template.Must(template.New("template.conf").Delims("$((", "))").Parse(DefaultConfigTemplate))
if !filepath.IsAbs(config.WebServerRoot) {
config.WebServerRoot = filepath.Join(`{{ env "APP_ROOT" }}`, config.WebServerRoot)
}
g.logs.Subprocess("Setting server root directory to '%s'", config.WebServerRoot)
if config.WebServerLocationPath == "" {
config.WebServerLocationPath = "/"
}
g.logs.Subprocess("Setting server location path to '%s'", config.WebServerLocationPath)
if config.WebServerEnablePushState {
g.logs.Subprocess("Enabling push state routing")
}
if config.WebServerForceHTTPS {
g.logs.Subprocess("Setting server to redirect HTTP requests to HTTPS")
}
if config.BasicAuthFile != "" {
g.logs.Subprocess("Enabling basic authentication with .htpasswd credentials")
}
if config.NGINXStubStatusPort != "" {
g.logs.Subprocess("Enabling basic status information with stub_status module")
}
g.logs.Break()
var b bytes.Buffer
err := t.Execute(&b, config)
if err != nil {
// not tested
return err
}
f, err := os.OpenFile(config.NGINXConfLocation, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create %s: %w", config.NGINXConfLocation, err)
}
defer f.Close()
_, err = io.Copy(f, &b)
if err != nil {
// not tested
return err
}
return nil
}