mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-07-04 17:06:49 +00:00
Write host template on save
This commit is contained in:
@ -1,13 +1,51 @@
|
||||
package nginx
|
||||
|
||||
import "npm/internal/entity/host"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"npm/internal/config"
|
||||
"npm/internal/entity/host"
|
||||
"npm/internal/logger"
|
||||
)
|
||||
|
||||
// ConfigureHost will attempt to write nginx conf and reload nginx
|
||||
func ConfigureHost(h host.Model) error {
|
||||
// nolint: errcheck, gosec
|
||||
h.Expand([]string{"certificate"})
|
||||
h.Expand([]string{"certificate", "hosttemplate"})
|
||||
|
||||
data := TemplateData{
|
||||
ConfDir: fmt.Sprintf("%s/nginx/hosts", config.Configuration.DataFolder),
|
||||
DataDir: config.Configuration.DataFolder,
|
||||
CertsDir: config.Configuration.Acmesh.CertHome,
|
||||
Host: &h,
|
||||
Certificate: h.Certificate,
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("%s/host_%d.conf", data.ConfDir, h.ID)
|
||||
|
||||
// Write the config to disk
|
||||
err := writeTemplate(filename, h.HostTemplate.Template, data)
|
||||
if err != nil {
|
||||
// this configuration failed somehow
|
||||
h.Status = host.StatusError
|
||||
h.ErrorMessage = fmt.Sprintf("Template generation failed: %s", err.Error())
|
||||
logger.Debug(h.ErrorMessage)
|
||||
return h.Save(true)
|
||||
}
|
||||
|
||||
// nolint: errcheck, gosec
|
||||
reloadNginx()
|
||||
return nil
|
||||
if err := reloadNginx(); err != nil {
|
||||
// reloading nginx failed, likely due to this host having a problem
|
||||
h.Status = host.StatusError
|
||||
h.ErrorMessage = fmt.Sprintf("Nginx configuation error: %s", err.Error())
|
||||
writeConfigFile(filename, fmt.Sprintf("# %s", h.ErrorMessage))
|
||||
logger.Debug(h.ErrorMessage)
|
||||
} else {
|
||||
// All good
|
||||
h.Status = host.StatusOK
|
||||
h.ErrorMessage = ""
|
||||
logger.Debug("ConfigureHost OK: %+v", h)
|
||||
}
|
||||
|
||||
return h.Save(true)
|
||||
}
|
||||
|
Reference in New Issue
Block a user