nginx-proxy-manager-zh/backend/internal/validator/hosts.go
Jamie Curnow 5e5f0de0e2 - Added upstream objects
- Renamed host templates to nginx templates
- Generate upstream templates
- Better nginx error reporting when reloading
- Use tparse for golang test reporting
2023-01-04 15:53:52 +10:00

34 lines
999 B
Go

package validator
import (
"fmt"
"npm/internal/entity/certificate"
"npm/internal/entity/host"
"npm/internal/entity/nginxtemplate"
)
// 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 > 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); cErr != nil {
return fmt.Errorf("Certificate #%d does not exist", h.CertificateID)
}
}
// Check the nginx template exists and has the same type.
nginxTemplate, tErr := nginxtemplate.GetByID(h.NginxTemplateID)
if tErr != nil {
return fmt.Errorf("Host Template #%d does not exist", h.NginxTemplateID)
}
if nginxTemplate.Type != h.Type {
return fmt.Errorf("Host Template #%d is not valid for this host type", h.NginxTemplateID)
}
return nil
}