nginx-proxy-manager/backend/internal/nginx/templates.go

53 lines
1.4 KiB
Go
Raw Normal View History

package nginx
import (
2022-07-21 08:02:07 +00:00
"fmt"
"os"
2022-07-21 08:02:07 +00:00
"npm/internal/entity/certificate"
"npm/internal/entity/host"
"npm/internal/entity/upstream"
2022-07-21 08:02:07 +00:00
"npm/internal/logger"
"npm/internal/util"
"github.com/aymerick/raymond"
)
// TemplateData is a struct
2022-07-21 08:02:07 +00:00
type TemplateData struct {
ConfDir string
DataDir string
Host host.Template
Certificate certificate.Template
Upstream upstream.Model
2022-07-21 08:02:07 +00:00
}
func generateHostConfig(template string, data TemplateData) (string, error) {
logger.Debug("Rendering Template - Template: %s", template)
logger.Debug("Rendering Template - Data: %+v", data)
2022-07-21 08:02:07 +00:00
return raymond.Render(template, data)
// todo: apply some post processing to this config, stripe trailing whitespace from lines and then remove groups of 2+ \n's so the config looks nicer
2022-07-21 08:02:07 +00:00
}
2022-07-21 08:02:07 +00:00
func writeTemplate(filename, template string, data TemplateData) error {
output, err := generateHostConfig(template, data)
if err != nil {
2022-07-21 08:02:07 +00:00
output = fmt.Sprintf("# Template Error: %s", err.Error())
}
2022-07-21 08:02:07 +00:00
// Write it. This will also write an error comment if generation failed
// nolint: gosec
writeErr := writeConfigFile(filename, util.CleanupWhitespace(output))
if err != nil {
return err
}
2022-07-21 08:02:07 +00:00
return writeErr
}
2022-07-21 08:02:07 +00:00
func writeConfigFile(filename, content string) error {
logger.Debug("Writing %s with:\n%s", filename, content)
// nolint: gosec
return os.WriteFile(filename, []byte(content), 0644)
}