Skip to content

Commit d84cc66

Browse files
refactor: migrate storage backend from SQLite to PostgreSQL
1 parent 4b74074 commit d84cc66

4 files changed

Lines changed: 16 additions & 14 deletions

File tree

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ require (
1818
github.com/lib/pq v1.10.9
1919
github.com/likexian/whois v1.15.6
2020
github.com/likexian/whois-parser v1.24.20
21-
github.com/mattn/go-sqlite3 v1.14.22
2221
github.com/miekg/dns v1.1.67
2322
github.com/redis/go-redis/v9 v9.11.0
2423
github.com/spf13/cobra v1.9.1

internal/api/hera.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ import (
164164
"github.com/CodeMonkeyCybersecurity/shells/internal/logger"
165165
"github.com/gin-gonic/gin"
166166
"github.com/jmoiron/sqlx"
167-
_ "github.com/lib/pq" // PostgreSQL driver
168-
_ "github.com/mattn/go-sqlite3" // SQLite driver
167+
_ "github.com/lib/pq" // PostgreSQL driver
169168
)
170169

171170
// Hera-specific API types

pkg/monitoring/continuous.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,16 +1140,18 @@ func sortStrings(items []string) {
11401140
func createStorage(backend string) (MonitoringStorage, error) {
11411141
// Create appropriate storage backend
11421142
switch backend {
1143-
case "sqlite", "sqlite3":
1144-
// Use the same database as the main application
1145-
return NewSQLiteStorage("shells.db")
1143+
case "postgres", "postgresql":
1144+
// Use PostgreSQL connection string from environment or default
1145+
dsn := "postgres://shells:shells_password@localhost:5432/shells?sslmode=disable"
1146+
return NewSQLiteStorage(dsn)
11461147
case "memory":
11471148
return &InMemoryStorage{
11481149
data: make(map[string]interface{}),
11491150
}, nil
11501151
default:
1151-
// Default to SQLite for persistence
1152-
return NewSQLiteStorage("shells.db")
1152+
// Default to PostgreSQL for persistence
1153+
dsn := "postgres://shells:shells_password@localhost:5432/shells?sslmode=disable"
1154+
return NewSQLiteStorage(dsn)
11531155
}
11541156
}
11551157

pkg/monitoring/storage.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ import (
99
"time"
1010

1111
"github.com/jmoiron/sqlx"
12-
_ "github.com/mattn/go-sqlite3"
12+
_ "github.com/lib/pq" // PostgreSQL driver
1313

1414
"github.com/CodeMonkeyCybersecurity/shells/internal/config"
1515
"github.com/CodeMonkeyCybersecurity/shells/internal/logger"
1616
)
1717

18-
// SQLiteStorage implements MonitoringStorage using SQLite3
18+
// SQLiteStorage implements MonitoringStorage using PostgreSQL
19+
// Note: Name kept for backward compatibility but now uses PostgreSQL
1920
type SQLiteStorage struct {
2021
db *sqlx.DB
2122
logger *logger.Logger
2223
}
2324

24-
// NewSQLiteStorage creates a new SQLite storage backend for monitoring
25+
// NewSQLiteStorage creates a new PostgreSQL storage backend for monitoring
26+
// Note: Name kept for backward compatibility but now uses PostgreSQL
2527
func NewSQLiteStorage(dsn string) (*SQLiteStorage, error) {
2628
// Initialize logger
2729
log, err := logger.New(config.LoggerConfig{Level: "debug", Format: "json"})
@@ -30,10 +32,10 @@ func NewSQLiteStorage(dsn string) (*SQLiteStorage, error) {
3032
}
3133
log = log.WithComponent("monitoring-storage")
3234

33-
// Connect to SQLite database
34-
db, err := sqlx.Connect("sqlite3", dsn)
35+
// Connect to PostgreSQL database
36+
db, err := sqlx.Connect("postgres", dsn)
3537
if err != nil {
36-
return nil, fmt.Errorf("failed to connect to SQLite database: %w", err)
38+
return nil, fmt.Errorf("failed to connect to PostgreSQL database: %w", err)
3739
}
3840

3941
storage := &SQLiteStorage{

0 commit comments

Comments
 (0)