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
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Expiry struct {
|
||||
Label string
|
||||
Amount int
|
||||
Unit string
|
||||
}
|
||||
|
||||
// this will parse something like "1d" into 2 groups: the amount, and the unit
|
||||
var expiryPattern = regexp.MustCompile(`^([1-9][0-9]*)(h|d|w|mo|y)$`)
|
||||
|
||||
func ParseExpiry(expiry string) (Expiry, error) {
|
||||
expiry = strings.TrimSpace(expiry)
|
||||
if expiry == "0" {
|
||||
return Expiry{
|
||||
Label: "indefinite",
|
||||
Unit: "indefinite",
|
||||
}, nil
|
||||
}
|
||||
matches := expiryPattern.FindStringSubmatch(expiry)
|
||||
if matches == nil {
|
||||
return Expiry{}, fmt.Errorf("invalid expiry %q", expiry)
|
||||
}
|
||||
|
||||
// parses the string amount to an int
|
||||
// the amount is 2nd (1) in the index because the first (0) is the entire string
|
||||
amount, err := strconv.Atoi(matches[1])
|
||||
if err != nil {
|
||||
return Expiry{}, fmt.Errorf("invalid expiry %q: %w", expiry, err)
|
||||
}
|
||||
|
||||
return Expiry{
|
||||
Label: expiry,
|
||||
Amount: amount,
|
||||
Unit: matches[2],
|
||||
}, nil
|
||||
}
|
||||
|
||||
func FindExpiry(expiries []Expiry, label string) (Expiry, bool) {
|
||||
index := slices.IndexFunc(expiries, func(expiry Expiry) bool {
|
||||
return expiry.Label == label
|
||||
})
|
||||
if index == -1 {
|
||||
return Expiry{}, false
|
||||
}
|
||||
return expiries[index], true
|
||||
}
|
||||
|
||||
func (expiry Expiry) ExpiresFrom(now time.Time) (*time.Time, error) {
|
||||
var expires time.Time
|
||||
switch expiry.Unit {
|
||||
case "h":
|
||||
expires = now.Add(time.Duration(expiry.Amount) * time.Hour)
|
||||
case "d":
|
||||
expires = now.AddDate(0, 0, expiry.Amount)
|
||||
case "w":
|
||||
expires = now.AddDate(0, 0, expiry.Amount*7)
|
||||
case "mo":
|
||||
expires = now.AddDate(0, expiry.Amount, 0)
|
||||
case "y":
|
||||
expires = now.AddDate(expiry.Amount, 0, 0)
|
||||
case "indefinite":
|
||||
return nil, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported expiry unit %q", expiry.Unit)
|
||||
}
|
||||
|
||||
return &expires, nil
|
||||
}
|
||||
Reference in New Issue
Block a user