From 10e0c22108c53e14f19c7f91ec0dc173a4f0c696 Mon Sep 17 00:00:00 2001 From: Keiran Date: Thu, 7 Aug 2025 19:16:12 +0100 Subject: [PATCH] add user-facing cli to makefile, and config master address through .env --- .env.example | 4 +++ Makefile | 14 ++++++++- internal/config/config.go | 60 +++++++++++++++++++++------------------ internal/db/account.go | 14 ++++++--- 4 files changed, 59 insertions(+), 33 deletions(-) diff --git a/.env.example b/.env.example index 10cddb9..733f711 100644 --- a/.env.example +++ b/.env.example @@ -21,3 +21,7 @@ DB_HEALTH_CHECK_PERIOD=5s # User Defaults and Billing DEFAULT_STORAGE_LIMIT_GB=1 PRICE_PER_GB_USD=0.50 + +# Bitcoin Payment Configuration +BITCOIN_MASTER_ADDRESS=bc1qyour_bitcoin_address_here +BITCOIN_NETWORK=mainnet diff --git a/Makefile b/Makefile index da37ed3..641a18a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ BINARY = termcloud ADMIN_BINARY = termcloud-admin +CLI_BINARY = tcman include .env export @@ -7,6 +8,7 @@ export build: clean go build -o build/${BINARY} cmd/${BINARY}/main.go go build -o build/${ADMIN_BINARY} cmd/admin/main.go + go build -o build/${CLI_BINARY} cmd/${CLI_BINARY}/main.go run: build ./build/${BINARY} @@ -14,9 +16,15 @@ run: build admin: build ./build/${ADMIN_BINARY} $(ARGS) +cli: build + ./build/${CLI_BINARY} $(ARGS) + dev: go run cmd/${BINARY}/main.go +cli-dev: + go run cmd/${CLI_BINARY}/main.go $(ARGS) + clean: go clean ./... rm -rf build @@ -24,4 +32,8 @@ clean: test: go test ./... -.PHONY: build run admin dev clean test +install: build + cp build/${CLI_BINARY} /usr/local/bin/tcman + chmod +x /usr/local/bin/tcman + +.PHONY: build run admin cli dev cli-dev clean test install diff --git a/internal/config/config.go b/internal/config/config.go index d7c98df..0acf0a4 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -7,38 +7,42 @@ import ( ) type Config struct { - DatabaseURL string - Port string - StorageDir string - MaxFileSize int64 - RateLimit float64 - CORSOrigins []string - GzipLevel int - MaxConnections int32 - MinConnections int32 - ConnLifetime time.Duration - ConnIdleTime time.Duration - HealthCheckPeriod time.Duration - DefaultStorageLimit int64 - PricePerGBUSD float64 + DatabaseURL string + Port string + StorageDir string + MaxFileSize int64 + RateLimit float64 + CORSOrigins []string + GzipLevel int + MaxConnections int32 + MinConnections int32 + ConnLifetime time.Duration + ConnIdleTime time.Duration + HealthCheckPeriod time.Duration + DefaultStorageLimit int64 + PricePerGBUSD float64 + BitcoinMasterAddress string + BitcoinNetwork string } func Load() *Config { return &Config{ - DatabaseURL: getEnv("DATABASE_URL", ""), - Port: getEnv("PORT", "8080"), - StorageDir: getEnv("STORAGE_DIR", "storage"), - MaxFileSize: getEnvInt64("MAX_FILE_SIZE_MB", 100) * 1024 * 1024, - RateLimit: getEnvFloat64("RATE_LIMIT", 20.0), - CORSOrigins: getEnvSlice("CORS_ORIGINS", []string{"*"}), - GzipLevel: getEnvInt("GZIP_LEVEL", 5), - MaxConnections: getEnvInt32("DB_MAX_CONNECTIONS", 100), - MinConnections: getEnvInt32("DB_MIN_CONNECTIONS", 10), - ConnLifetime: getEnvDuration("DB_CONN_LIFETIME", time.Hour), - ConnIdleTime: getEnvDuration("DB_CONN_IDLE_TIME", time.Minute), - HealthCheckPeriod: getEnvDuration("DB_HEALTH_CHECK_PERIOD", 5*time.Second), - DefaultStorageLimit: getEnvInt64("DEFAULT_STORAGE_LIMIT_GB", 1) * 1024 * 1024 * 1024, - PricePerGBUSD: getEnvFloat64("PRICE_PER_GB_USD", 0.50), + DatabaseURL: getEnv("DATABASE_URL", ""), + Port: getEnv("PORT", "8080"), + StorageDir: getEnv("STORAGE_DIR", "storage"), + MaxFileSize: getEnvInt64("MAX_FILE_SIZE_MB", 100) * 1024 * 1024, + RateLimit: getEnvFloat64("RATE_LIMIT", 20.0), + CORSOrigins: getEnvSlice("CORS_ORIGINS", []string{"*"}), + GzipLevel: getEnvInt("GZIP_LEVEL", 5), + MaxConnections: getEnvInt32("DB_MAX_CONNECTIONS", 100), + MinConnections: getEnvInt32("DB_MIN_CONNECTIONS", 10), + ConnLifetime: getEnvDuration("DB_CONN_LIFETIME", time.Hour), + ConnIdleTime: getEnvDuration("DB_CONN_IDLE_TIME", time.Minute), + HealthCheckPeriod: getEnvDuration("DB_HEALTH_CHECK_PERIOD", 5*time.Second), + DefaultStorageLimit: getEnvInt64("DEFAULT_STORAGE_LIMIT_GB", 1) * 1024 * 1024 * 1024, + PricePerGBUSD: getEnvFloat64("PRICE_PER_GB_USD", 0.50), + BitcoinMasterAddress: getEnv("BITCOIN_MASTER_ADDRESS", ""), + BitcoinNetwork: getEnv("BITCOIN_NETWORK", "mainnet"), } } diff --git a/internal/db/account.go b/internal/db/account.go index 21e9711..6964f89 100644 --- a/internal/db/account.go +++ b/internal/db/account.go @@ -55,8 +55,9 @@ type UsageRecord struct { } type AccountService struct { - pool *pgxpool.Pool - pricePerGB float64 + pool *pgxpool.Pool + pricePerGB float64 + bitcoinMasterAddr string } func NewAccountService(pool *pgxpool.Pool, cfg *config.Config) *AccountService { @@ -66,8 +67,9 @@ func NewAccountService(pool *pgxpool.Pool, cfg *config.Config) *AccountService { } return &AccountService{ - pool: pool, - pricePerGB: pricePerGB, + pool: pool, + pricePerGB: pricePerGB, + bitcoinMasterAddr: cfg.BitcoinMasterAddress, } } @@ -422,6 +424,10 @@ func (s *AccountService) CheckResourceLimits(ctx context.Context, accountID int6 } func (s *AccountService) generateBTCAddress() string { + if s.bitcoinMasterAddr != "" { + return s.bitcoinMasterAddr + } + bytes := make([]byte, 20) rand.Read(bytes) return fmt.Sprintf("bc1q%x", bytes)