74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package drop
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
Addr string
|
|
TempDir string
|
|
MaxFileSize int64
|
|
FileTTL time.Duration
|
|
AllowedCIDRs []string
|
|
}
|
|
|
|
func DefaultConfig() Config {
|
|
return Config{
|
|
Addr: envString("INTERNAL_DROP_ADDR", ":10051"),
|
|
TempDir: envString("INTERNAL_DROP_TEMP_DIR", "./tmp/internal-drop"),
|
|
MaxFileSize: envInt64("INTERNAL_DROP_MAX_FILE_SIZE", 1<<30),
|
|
FileTTL: envDuration("INTERNAL_DROP_FILE_TTL", 2*time.Hour),
|
|
AllowedCIDRs: envList("INTERNAL_DROP_ALLOWED_CIDRS"),
|
|
}
|
|
}
|
|
|
|
func envString(key, fallback string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func envInt64(key string, fallback int64) int64 {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
parsed, err := strconv.ParseInt(value, 10, 64)
|
|
if err != nil || parsed <= 0 {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func envDuration(key string, fallback time.Duration) time.Duration {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
parsed, err := time.ParseDuration(value)
|
|
if err != nil || parsed <= 0 {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func envList(key string) []string {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return nil
|
|
}
|
|
parts := strings.Split(value, ",")
|
|
out := make([]string, 0, len(parts))
|
|
for _, part := range parts {
|
|
part = strings.TrimSpace(part)
|
|
if part != "" {
|
|
out = append(out, part)
|
|
}
|
|
}
|
|
return out
|
|
}
|