feat: migrate store layer to GORM with PostgreSQL support

- Migrate all store packages from raw database/sql to GORM ORM
- Add PostgreSQL support alongside SQLite
- Move EncryptedString type to crypto package for cleaner architecture
- Add automatic encryption/decryption for sensitive fields (API keys, secrets)
- Fix PostgreSQL AutoMigrate conflicts by skipping existing tables
- Fix duplicate /klines route registration
- Update tests to use GORM database connections
- Add database configuration support in config package
This commit is contained in:
tinkle-community
2026-01-01 19:32:49 +08:00
parent d547863ebb
commit 2d272bb7b8
32 changed files with 2573 additions and 3771 deletions

View File

@@ -20,6 +20,16 @@ type Config struct {
RegistrationEnabled bool
MaxUsers int // Maximum number of users allowed (0 = unlimited, default = 10)
// Database configuration
DBType string // sqlite or postgres
DBPath string // SQLite database file path
DBHost string // PostgreSQL host
DBPort int // PostgreSQL port
DBUser string // PostgreSQL user
DBPassword string // PostgreSQL password
DBName string // PostgreSQL database name
DBSSLMode string // PostgreSQL SSL mode
// Security configuration
// TransportEncryption enables browser-side encryption for API keys
// Requires HTTPS or localhost. Set to false for HTTP access via IP.
@@ -43,6 +53,14 @@ func Init() {
RegistrationEnabled: true,
MaxUsers: 10, // Default: 10 users allowed
ExperienceImprovement: true, // Default: enabled to help improve the product
// Database defaults
DBType: "sqlite",
DBPath: "data/data.db",
DBHost: "localhost",
DBPort: 5432,
DBUser: "postgres",
DBName: "nofx",
DBSSLMode: "disable",
}
// Load from environment variables
@@ -86,6 +104,34 @@ func Init() {
cfg.AlpacaSecretKey = os.Getenv("ALPACA_SECRET_KEY")
cfg.TwelveDataKey = os.Getenv("TWELVEDATA_API_KEY")
// Database configuration
if v := os.Getenv("DB_TYPE"); v != "" {
cfg.DBType = strings.ToLower(v)
}
if v := os.Getenv("DB_PATH"); v != "" {
cfg.DBPath = v
}
if v := os.Getenv("DB_HOST"); v != "" {
cfg.DBHost = v
}
if v := os.Getenv("DB_PORT"); v != "" {
if port, err := strconv.Atoi(v); err == nil && port > 0 {
cfg.DBPort = port
}
}
if v := os.Getenv("DB_USER"); v != "" {
cfg.DBUser = v
}
if v := os.Getenv("DB_PASSWORD"); v != "" {
cfg.DBPassword = v
}
if v := os.Getenv("DB_NAME"); v != "" {
cfg.DBName = v
}
if v := os.Getenv("DB_SSLMODE"); v != "" {
cfg.DBSSLMode = v
}
global = cfg
// Initialize experience improvement (installation ID will be set after database init)