oh my fucking GOD bro, i've seen expiry SO SO SO much it idek if i've been spelling it right at this point, please let me be done with this
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
UploadDir string
|
|
MaxPasteBytes int64
|
|
MaxFileBytes int64
|
|
Expiries []Expiry
|
|
|
|
DatabasePath string
|
|
}
|
|
|
|
func loadConfig() (Config, error) {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
log.Fatal("Error loading .env file")
|
|
}
|
|
|
|
uploadDir := envOrDefault("MINIBIN_UPLOAD_DIR", "uploads")
|
|
pasteSizeSetting := envOrDefault("MINIBIN_MAX_PASTE_KIB", "1024")
|
|
maxPasteKiB, err := strconv.ParseInt(pasteSizeSetting, 10, 64)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("failed to parse MINIBIN_MAX_PASTE_KIB: %w", err)
|
|
}
|
|
if maxPasteKiB <= 0 {
|
|
return Config{}, fmt.Errorf("MINIBIN_MAX_PASTE_KIB must be greater than 0")
|
|
}
|
|
|
|
fileSizeSetting := envOrDefault("MINIBIN_MAX_FILE_KIB", "1024")
|
|
maxFileKiB, err := strconv.ParseInt(fileSizeSetting, 10, 64)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("failed to parse MINIBIN_MAX_FILE_KIB: %w", err)
|
|
}
|
|
if maxFileKiB <= 0 {
|
|
return Config{}, fmt.Errorf("MINIBIN_MAX_FILE_KIB must be greater than 0")
|
|
}
|
|
|
|
databasePath := envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite")
|
|
|
|
expiriesSetting := envOrDefault("MINIBIN_EXPIRIES", "1h,12h,1d,7d,2w,1mo,6mo,1y,5y, 0")
|
|
var expiries []Expiry
|
|
for entry := range strings.SplitSeq(expiriesSetting, ",") {
|
|
expiry, err := parseExpiry(entry)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("MINIBIN_EXPIRIES: %w", err)
|
|
}
|
|
expiries = append(expiries, expiry)
|
|
}
|
|
|
|
return Config{
|
|
UploadDir: uploadDir,
|
|
MaxPasteBytes: maxPasteKiB * 1024,
|
|
MaxFileBytes: maxFileKiB * 1024,
|
|
Expiries: expiries,
|
|
|
|
DatabasePath: databasePath,
|
|
}, nil
|
|
}
|
|
|
|
func envOrDefault(key, fallback string) string {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
return value
|
|
}
|