-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_executor.go
33 lines (27 loc) · 1 KB
/
template_executor.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
package version2
import (
"bytes"
"path"
"text/template"
)
// TemplateExecutor executes NGINX configuration templates.
type TemplateExecutor struct {
virtualServerTemplate *template.Template
}
// NewTemplateExecutor creates a TemplateExecutor.
func NewTemplateExecutor(virtualServerTemplatePath string) (*TemplateExecutor, error) {
// template name must be the base name of the template file https://golang.org/pkg/text/template/#Template.ParseFiles
vsTemplate, err := template.New(path.Base(virtualServerTemplatePath)).ParseFiles(virtualServerTemplatePath)
if err != nil {
return nil, err
}
return &TemplateExecutor{
virtualServerTemplate: vsTemplate,
}, nil
}
// ExecuteVirtualServerTemplate generates the content of an NGINX configuration file for a VirtualServer resource.
func (te *TemplateExecutor) ExecuteVirtualServerTemplate(cfg *VirtualServerConfig) ([]byte, error) {
var configBuffer bytes.Buffer
err := te.virtualServerTemplate.Execute(&configBuffer, cfg)
return configBuffer.Bytes(), err
}