organize project
took me a bit to figure out how to do this properly, but i believe it's improved my understanding of how go works
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
UploadDir string
|
||||
MaxPasteBytes int64
|
||||
MaxFileBytes int64
|
||||
Expiries []Expiry
|
||||
NormalizeMIMETypes bool
|
||||
BannedMIMETypes []string
|
||||
PoWDifficulty int
|
||||
ReportOptions []string
|
||||
|
||||
DatabasePath string
|
||||
// public host like "https://bin.zip", NOT the address minbin listens on
|
||||
Host string
|
||||
Addr string
|
||||
}
|
||||
|
||||
func LoadConfig() (Config, error) {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
powDifficultySetting := envOrDefault("MINIBIN_POW_DIFFICULTY", "18")
|
||||
powDifficulty, err := strconv.Atoi(powDifficultySetting)
|
||||
if err != nil {
|
||||
return Config{}, fmt.Errorf("failed to parse MINIBIN_POW_DIFFICULTY")
|
||||
}
|
||||
reportOptionsSetting := envOrDefault("MINIBIN_REPORT_OPTIONS", "Malware (including links),CSAM/CSEM,Copyright Infringement,Fraud")
|
||||
reportOptions := strings.Split(reportOptionsSetting, ",")
|
||||
if len(reportOptions) == 0 {
|
||||
return Config{}, fmt.Errorf("no valid report options set")
|
||||
}
|
||||
|
||||
log.Println(len(reportOptions))
|
||||
|
||||
return Config{
|
||||
UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"),
|
||||
MaxPasteBytes: maxPasteKiB * 1024,
|
||||
MaxFileBytes: maxFileKiB * 1024,
|
||||
Expiries: expiries,
|
||||
NormalizeMIMETypes: envOrDefault("MINIBIN_NORMALIZE_MIME_TYPES", "true") == "true",
|
||||
BannedMIMETypes: strings.Split(envOrDefault("MINIBIN_BANNED_MIME_TYPES", ""), ","),
|
||||
PoWDifficulty: powDifficulty,
|
||||
ReportOptions: reportOptions,
|
||||
|
||||
DatabasePath: envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite"),
|
||||
Host: envOrDefault("MINIBIN_HOST", "http://localhost:8080"),
|
||||
Addr: envOrDefault("MINIBIN_ADDR", "localhost:8080"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func envOrDefault(key, fallback string) string {
|
||||
value := strings.TrimSpace(os.Getenv(key))
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user