94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
defaultSeaweedFSEndpoint = "http://localhost:8333"
|
|
defaultSeaweedFSBucket = "lincodex"
|
|
defaultSeaweedFSRegion = "us-east-1"
|
|
defaultSeaweedFSTimeout = 10 * time.Second
|
|
minBucketNameLength = 3
|
|
maxBucketNameLength = 63
|
|
)
|
|
|
|
func (config Config) ValidateSeaweedFS() error {
|
|
if err := validateStorageEndpoint(config.SeaweedFSEndpoint); err != nil {
|
|
return err
|
|
}
|
|
if strings.TrimSpace(config.SeaweedFSAccessKey) == "" {
|
|
return errors.New("Config.ValidateSeaweedFS - SEAWEEDFS_ACCESS_KEY must not be empty")
|
|
}
|
|
if strings.TrimSpace(config.SeaweedFSSecretKey) == "" {
|
|
return errors.New("Config.ValidateSeaweedFS - SEAWEEDFS_SECRET_KEY must not be empty")
|
|
}
|
|
if !validBucket(config.SeaweedFSBucket) {
|
|
return errors.New("Config.ValidateSeaweedFS - SEAWEEDFS_BUCKET must be a valid S3 bucket name")
|
|
}
|
|
if strings.TrimSpace(config.SeaweedFSRegion) == "" {
|
|
return errors.New("Config.ValidateSeaweedFS - SEAWEEDFS_REGION must not be empty")
|
|
}
|
|
if config.SeaweedFSTimeout <= 0 {
|
|
return errors.New("Config.ValidateSeaweedFS - SEAWEEDFS_TIMEOUT must be positive")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateStorageEndpoint(raw string) error {
|
|
endpoint, err := url.Parse(raw)
|
|
if err != nil {
|
|
return errors.New("config.validateStorageEndpoint - SEAWEEDFS_ENDPOINT must be an absolute HTTP or HTTPS URL")
|
|
}
|
|
|
|
hasHost := endpoint.Hostname() != ""
|
|
hasHTTPScheme := endpoint.Scheme == "http" || endpoint.Scheme == "https"
|
|
if !hasHost || !hasHTTPScheme {
|
|
return errors.New("config.validateStorageEndpoint - SEAWEEDFS_ENDPOINT must be an absolute HTTP or HTTPS URL")
|
|
}
|
|
|
|
hasCredentials := endpoint.User != nil
|
|
hasQuery := endpoint.RawQuery != "" || endpoint.ForceQuery
|
|
hasFragment := endpoint.Fragment != ""
|
|
hasPath := endpoint.Path != "" && endpoint.Path != "/"
|
|
if hasCredentials || hasQuery || hasFragment || hasPath {
|
|
return errors.New("config.validateStorageEndpoint - SEAWEEDFS_ENDPOINT must not contain credentials, a path, query, or fragment")
|
|
}
|
|
if !validEndpointPort(endpoint) {
|
|
return errors.New("config.validateStorageEndpoint - SEAWEEDFS_ENDPOINT must have a valid nonzero port")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validEndpointPort(endpoint *url.URL) bool {
|
|
port := endpoint.Port()
|
|
if port == "" && !strings.HasSuffix(endpoint.Host, ":") {
|
|
return true
|
|
}
|
|
number, err := strconv.Atoi(port)
|
|
return err == nil && number >= 1 && number <= maxPortNumber
|
|
}
|
|
|
|
func validBucket(bucket string) bool {
|
|
validLength := len(bucket) >= minBucketNameLength && len(bucket) <= maxBucketNameLength
|
|
if !validLength || net.ParseIP(bucket) != nil {
|
|
return false
|
|
}
|
|
for _, label := range strings.Split(bucket, ".") {
|
|
if label == "" || label[0] == '-' || label[len(label)-1] == '-' {
|
|
return false
|
|
}
|
|
for _, char := range label {
|
|
if char != '-' && (char < 'a' || char > 'z') && (char < '0' || char > '9') {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|