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

64 lines
1.5 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"
)
2023-01-06 01:42:02 +00:00
type Config struct {
Ipv4 bool
Ipv6 bool
}
// TemplateData is a struct
2022-07-21 08:02:07 +00:00
type TemplateData struct {
ConfDir string
2023-01-06 01:42:02 +00:00
Config Config
2022-07-21 08:02:07 +00:00
DataDir string
Host host.Template
Certificate certificate.Template
Upstream upstream.Model
2022-07-21 08:02:07 +00:00
}
2023-01-06 01:42:02 +00:00
func renderTemplate(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)
}
func writeTemplate(filename, template string, data TemplateData, errorInfo string) error {
2023-01-06 01:42:02 +00:00
output, err := renderTemplate(template, data)
if err != nil {
errorInfo = err.Error()
}
output = util.CleanupWhitespace(output)
// Write some given error information to the end
if errorInfo != "" {
output = fmt.Sprintf("%s\n\n# =========================\n# ERROR:\n# %s\n# ========================\n", output, errorInfo)
}
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, 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)
}