-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-config.go
80 lines (68 loc) · 1.85 KB
/
generate-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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/anhoffa/starchasers-nginx-config-creator/utils"
"os"
"text/template"
)
func generateNginxConfig(jsonConfig []byte) (string, error) {
var config Config
err := json.Unmarshal(jsonConfig, &config)
if err != nil {
return "", fmt.Errorf("failed to unmarshall the supplied json: %w", err)
}
tmplBytes, err := os.ReadFile("nginxTemplate.tmpl")
if err != nil {
return "", fmt.Errorf("failed to read the template file: %w", err)
}
tmpl, err := template.New("nginxTemplate.tmpl").Parse(string(tmplBytes))
if err != nil {
return "", fmt.Errorf("failed to parse the template: %w", err)
}
var result bytes.Buffer
err = tmpl.Execute(&result, prepareTemplateParams(&config))
if err != nil {
return "", fmt.Errorf("failed to execute the template: %w", err)
}
return result.String(), nil
}
func prepareTemplateParams(config *Config) TemplateParams {
var containers []Container
var existingNames = map[string]bool{}
for _, domain := range config.Domains {
_, exists := existingNames[domain.ContainerName]
if !exists {
existingNames[domain.ContainerName] = true
containers = append(containers, Container{ContainerName: domain.ContainerName, Ip: domain.Ip})
}
}
return TemplateParams{
Containers: containers,
Domains: config.Domains,
}
}
func validateNginxConfig(config string) error {
f, err := os.CreateTemp("", "nginx-conf-test-")
if err != nil {
return err
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
log.Warnw("Error removing temporary file", "error", err)
}
}(f.Name())
if _, err := f.WriteString(config); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
if err := utils.RedirectCmdOutput("nginx", "-t", "-c", f.Name()); err != nil {
return err
}
log.Info("Nginx configuration is valid")
return nil
}