chore: consolidate DBURL into DATA_DIR, codebase audit for 1.0.0
All checks were successful
check / check (push) Successful in 56s

DBURL → DATA_DIR consolidation:
- Remove DBURL env var entirely; main DB now lives at {DATA_DIR}/webhooker.db
- database.go constructs DB path from config.DataDir, ensures dir exists
- Update DATA_DIR prod default from /data/events to /data
- Update all tests to use DataDir instead of DBURL
- Update Dockerfile: /data (not /data/events) for all SQLite databases
- Update README configuration table, Docker examples, architecture docs

Dead code removal:
- Remove unused IndexResponse struct (handlers/index.go)
- Remove unused TemplateData struct (handlers/handlers.go)

Stale comment cleanup:
- Remove TODO in server.go (DB cleanup handled by fx lifecycle)
- Fix nolint:golint → nolint:revive on ServerParams for consistency
- Clean up verbose middleware/routing comments in routes.go
- Fix TODO fan-out description (worker pool, not goroutine-per-target)

.gitignore fixes:
- Add data/ directory to gitignore
- Remove stale config.yaml entry (env-only config since rework)
This commit is contained in:
clawbot
2026-03-01 23:33:20 -08:00
parent 536e5682d6
commit 4dd4dfa5eb
13 changed files with 51 additions and 105 deletions

View File

@@ -8,6 +8,8 @@ import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"go.uber.org/fx"
"gorm.io/driver/sqlite"
@@ -49,13 +51,17 @@ func New(lc fx.Lifecycle, params DatabaseParams) (*Database, error) {
}
func (d *Database) connect() error {
dbURL := d.params.Config.DBURL
if dbURL == "" {
// Default to SQLite for development
dbURL = "file:webhooker.db?cache=shared&mode=rwc"
// Ensure the data directory exists before opening the database.
dataDir := d.params.Config.DataDir
if err := os.MkdirAll(dataDir, 0750); err != nil {
return fmt.Errorf("creating data directory %s: %w", dataDir, err)
}
// First, open the database with the pure Go driver
// Construct the main application database path inside DATA_DIR.
dbPath := filepath.Join(dataDir, "webhooker.db")
dbURL := fmt.Sprintf("file:%s?cache=shared&mode=rwc", dbPath)
// Open the database with the pure Go SQLite driver
sqlDB, err := sql.Open("sqlite", dbURL)
if err != nil {
d.log.Error("failed to open database", "error", err)
@@ -72,7 +78,7 @@ func (d *Database) connect() error {
}
d.db = db
d.log.Info("connected to database", "database", dbURL)
d.log.Info("connected to database", "path", dbPath)
// Run migrations
return d.migrate()

View File

@@ -2,7 +2,6 @@ package database
import (
"context"
"os"
"testing"
"go.uber.org/fx/fxtest"
@@ -12,10 +11,6 @@ import (
)
func TestDatabaseConnection(t *testing.T) {
// Set DBURL env var for config loading
os.Setenv("DBURL", "file::memory:?cache=shared")
defer os.Unsetenv("DBURL")
// Set up test dependencies
lc := fxtest.NewLifecycle(t)
@@ -35,18 +30,12 @@ func TestDatabaseConnection(t *testing.T) {
t.Fatalf("Failed to create logger: %v", err)
}
// Create config
c, err := config.New(lc, config.ConfigParams{
Globals: g,
Logger: l,
})
if err != nil {
t.Fatalf("Failed to create config: %v", err)
// Create config with DataDir pointing to a temp directory
c := &config.Config{
DataDir: t.TempDir(),
Environment: "dev",
}
// Override DBURL to use a temp file-based SQLite (in-memory doesn't persist across connections)
c.DBURL = "file:" + t.TempDir() + "/test.db?cache=shared&mode=rwc"
// Create database
db, err := New(lc, DatabaseParams{
Config: c,

View File

@@ -18,10 +18,6 @@ import (
func setupTestWebhookDBManager(t *testing.T) (*WebhookDBManager, *fxtest.Lifecycle) {
t.Helper()
// Set DBURL env var for config loading
os.Setenv("DBURL", "file::memory:?cache=shared")
t.Cleanup(func() { os.Unsetenv("DBURL") })
lc := fxtest.NewLifecycle(t)
globals.Appname = "webhooker-test"
@@ -37,7 +33,6 @@ func setupTestWebhookDBManager(t *testing.T) (*WebhookDBManager, *fxtest.Lifecyc
dataDir := filepath.Join(t.TempDir(), "events")
cfg := &config.Config{
DBURL: "file::memory:?cache=shared",
DataDir: dataDir,
}