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

51 lines
1.4 KiB
Go
Raw Normal View History

2022-07-15 04:26:12 +00:00
package nginx
2022-07-21 08:02:07 +00:00
import (
"fmt"
"npm/internal/config"
"npm/internal/entity/host"
"npm/internal/logger"
)
2022-07-15 04:26:12 +00:00
// ConfigureHost will attempt to write nginx conf and reload nginx
func ConfigureHost(h host.Model) error {
// nolint: errcheck, gosec
2022-07-21 08:02:07 +00:00
h.Expand([]string{"certificate", "hosttemplate"})
data := TemplateData{
ConfDir: fmt.Sprintf("%s/nginx/hosts", config.Configuration.DataFolder),
DataDir: config.Configuration.DataFolder,
Host: h.GetTemplate(),
Certificate: h.Certificate.GetTemplate(),
2022-07-21 08:02:07 +00:00
}
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)
}
2022-07-15 04:26:12 +00:00
// nolint: errcheck, gosec
2022-07-21 08:02:07 +00:00
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)
2022-07-15 04:26:12 +00:00
}