nginx-proxy-manager-zh/backend/internal/validator/hosts.go

50 lines
1.6 KiB
Go
Raw Normal View History

package validator
import (
"npm/internal/entity/certificate"
"npm/internal/entity/host"
"npm/internal/entity/nginxtemplate"
2023-01-08 17:49:49 -05:00
"npm/internal/entity/upstream"
2023-02-24 02:19:07 -05:00
"github.com/rotisserie/eris"
)
// ValidateHost will check if associated objects exist and other checks
// will return a nil error if things are OK
func ValidateHost(h host.Model) error {
if h.CertificateID.Uint > 0 {
// Check certificate exists and is valid
// This will not determine if the certificate is Ready to use,
// as this validation only cares that the row exists.
if _, cErr := certificate.GetByID(h.CertificateID.Uint); cErr != nil {
return eris.Wrapf(cErr, "Certificate #%d does not exist", h.CertificateID.Uint)
}
}
if h.UpstreamID.Uint > 0 {
2023-01-08 17:49:49 -05:00
// Check upstream exists
if _, uErr := upstream.GetByID(h.UpstreamID.Uint); uErr != nil {
return eris.Wrapf(uErr, "Upstream #%d does not exist", h.UpstreamID.Uint)
2023-01-08 17:49:49 -05:00
}
}
// Ensure either UpstreamID is set or appropriate proxy host params are set
if h.UpstreamID.Uint > 0 && (h.ProxyHost != "" || h.ProxyPort > 0) {
2023-02-24 02:19:07 -05:00
return eris.Errorf("Proxy Host or Port cannot be set when using an Upstream")
2023-01-08 17:49:49 -05:00
}
if h.UpstreamID.Uint == 0 && (h.ProxyHost == "" || h.ProxyPort < 1) {
2023-02-24 02:19:07 -05:00
return eris.Errorf("Proxy Host and Port must be specified, unless using an Upstream")
2023-01-08 17:49:49 -05:00
}
// Check the nginx template exists and has the same type.
nginxTemplate, tErr := nginxtemplate.GetByID(h.NginxTemplateID)
if tErr != nil {
2023-02-24 02:19:07 -05:00
return eris.Wrapf(tErr, "Host Template #%d does not exist", h.NginxTemplateID)
}
if nginxTemplate.Type != h.Type {
2023-02-24 02:19:07 -05:00
return eris.Errorf("Host Template #%d is not valid for this host type", h.NginxTemplateID)
}
return nil
}