slug: Prohibit slugs ending in '_' for future use

This commit is contained in:
Tom Wiesing 2023-04-12 11:26:25 +02:00
parent 7ff2ecf7fe
commit eacd59bb1b
No known key found for this signature in database
2 changed files with 40 additions and 11 deletions

View file

@ -15,7 +15,7 @@ type HTTPConfig struct {
PrimaryDomain string `yaml:"domain" default:"localhost.kwarc.info" validate:"domain"`
// By default, only the 'self' domain above is caught.
// To catch additional domains, add them here (comma seperated)
// To catch additional domains, add them here (comma separated)
ExtraDomains []string `yaml:"domains" validate:"domains"`
// The system can support setting up certificate(s) automatically.
@ -51,6 +51,41 @@ func (hcfg HTTPConfig) HTTPSEnabled() bool {
return hcfg.CertbotEmail != ""
}
type SpecialDomain string
var (
TriplestoreDomain SpecialDomain = "ts"
)
func (hcfg HTTPConfig) SpecialDomain(domain SpecialDomain) string {
return fmt.Sprintf("%s.%s", string(domain)+"_", hcfg.PrimaryDomain)
}
// Domains adds the given subdomain to the primary and alias domains.
// If sub is empty, returns only the domains.
//
// sub is not otherwise validated, and should be normalized by the caller.
func (hcfg HTTPConfig) Domains(sub string) []string {
domains := append([]string{hcfg.PrimaryDomain}, hcfg.ExtraDomains...)
if sub == "" {
return domains
}
for i, d := range domains {
domains[i] = sub + "." + d
}
return domains
}
// HostRule returns a HostRule for the provided subdomain.
// See Domains() for usage of sub.
func (hcfg HTTPConfig) HostRule(sub string) string {
quoted := collection.MapSlice(hcfg.Domains(sub), func(name string) string {
return "`" + name + "`"
})
return fmt.Sprintf("Host(%s)", strings.Join(quoted, ","))
}
// HTTPSEnabledEnv returns "true" if https is enabled, and "false" otherwise.
func (hcfg HTTPConfig) HTTPSEnabledEnv() string {
if hcfg.HTTPSEnabled() {
@ -101,17 +136,8 @@ func TrimSuffixFold(s string, suffix string) string {
return s
}
// HostRule returns a traefik rule for the given names
// TODO: Move this over!
func HostRule(names ...string) string {
quoted := collection.MapSlice(names, func(name string) string {
return "`" + name + "`"
})
return fmt.Sprintf("Host(%s)", strings.Join(quoted, ","))
}
// DefaultHostRule returns the default traefik hostname rule for this distillery.
// This consists of the [DefaultDomain] as well as [ExtraDomains].
func (cfg HTTPConfig) DefaultHostRule() string {
return HostRule(append([]string{cfg.PrimaryDomain}, cfg.ExtraDomains...)...)
return cfg.HostRule("")
}

View file

@ -20,5 +20,8 @@ func ValidateSlug(s *string, dflt string) error {
if !regexpSlug.MatchString(*s) {
return ErrInvalidSlug
}
if strings.HasSuffix(*s, "_") {
return ErrInvalidSlug
}
return nil
}