this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

support both postgres & sqlite

dholms d4fe3b9a 16ac57aa

+67 -14
+5 -5
cmd/nexus/main.go
··· 46 46 EnvVars: []string{"OTEL_EXPORTER_OTLP_ENDPOINT"}, 47 47 }, 48 48 &cli.StringFlag{ 49 - Name: "db-path", 50 - Usage: "path to SQLite database file", 51 - Value: "./nexus.db", 52 - EnvVars: []string{"NEXUS_DB_PATH"}, 49 + Name: "database-url", 50 + Usage: "database connection string (sqlite://path or postgres://...)", 51 + Value: "sqlite://./nexus.db", 52 + EnvVars: []string{"NEXUS_DATABASE_URL"}, 53 53 }, 54 54 &cli.StringFlag{ 55 55 Name: "relay-url", ··· 132 132 signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) 133 133 134 134 config := NexusConfig{ 135 - DBPath: cctx.String("db-path"), 135 + DatabaseURL: cctx.String("database-url"), 136 136 RelayUrl: cctx.String("relay-url"), 137 137 FirehoseParallelism: cctx.Int("firehose-parallelism"), 138 138 ResyncParallelism: cctx.Int("resync-parallelism"),
+62 -8
cmd/nexus/nexus.go
··· 2 2 3 3 import ( 4 4 "context" 5 + "fmt" 5 6 "log/slog" 7 + "os" 8 + "path/filepath" 9 + "strings" 6 10 "sync" 7 11 "time" 8 12 ··· 10 14 "github.com/bluesky-social/indigo/atproto/identity" 11 15 "github.com/bluesky-social/indigo/cmd/nexus/models" 12 16 "github.com/bluesky-social/indigo/events" 17 + "gorm.io/driver/postgres" 13 18 "gorm.io/driver/sqlite" 14 19 "gorm.io/gorm" 15 20 "gorm.io/gorm/logger" ··· 40 45 } 41 46 42 47 type NexusConfig struct { 43 - DBPath string 48 + DatabaseURL string 44 49 RelayUrl string 45 50 FirehoseParallelism int 46 51 ResyncParallelism int ··· 53 58 } 54 59 55 60 func NewNexus(config NexusConfig) (*Nexus, error) { 56 - db, err := gorm.Open(sqlite.Open(config.DBPath+"?_journal_mode=WAL&_busy_timeout=10000&_synchronous=NORMAL&_temp_store=MEMORY&_cache_size=8000&_wal_autocheckpoint=3000"), &gorm.Config{ 57 - Logger: logger.Default.LogMode(logger.Silent), 58 - }) 61 + db, err := SetupDatabase(config.DatabaseURL) 59 62 if err != nil { 60 - return nil, err 61 - } 62 - 63 - if err := db.AutoMigrate(&models.Repo{}, &models.RepoRecord{}, &models.OutboxBuffer{}, &models.ResyncBuffer{}, &models.FirehoseCursor{}, &models.ListReposCursor{}, &models.CollectionCursor{}); err != nil { 64 63 return nil, err 65 64 } 66 65 ··· 193 192 194 193 return nil 195 194 } 195 + 196 + func SetupDatabase(dbUrl string) (*gorm.DB, error) { 197 + // Setup database connection (supports both SQLite and Postgres) 198 + var dialector gorm.Dialector 199 + isSqlite := false 200 + maxOpenConns := 80 201 + 202 + if strings.HasPrefix(dbUrl, "sqlite://") { 203 + sqlitePath := dbUrl[len("sqlite://"):] 204 + // Create directory if it doesn't exist 205 + if err := os.MkdirAll(filepath.Dir(sqlitePath), os.ModePerm); err != nil { 206 + return nil, err 207 + } 208 + // SQLite with pragmas in connection string 209 + dialector = sqlite.Open(sqlitePath + "?_journal_mode=WAL&_busy_timeout=10000&_synchronous=NORMAL&_temp_store=MEMORY&_cache_size=8000&_wal_autocheckpoint=3000") 210 + isSqlite = true 211 + maxOpenConns = 1 212 + } else if strings.HasPrefix(dbUrl, "postgresql://") || strings.HasPrefix(dbUrl, "postgres://") { 213 + dialector = postgres.Open(dbUrl) 214 + } else { 215 + return nil, fmt.Errorf("unsupported database URL scheme: must start with sqlite://, postgres://, or postgresql://") 216 + } 217 + 218 + db, err := gorm.Open(dialector, &gorm.Config{ 219 + Logger: logger.Default.LogMode(logger.Silent), 220 + }) 221 + if err != nil { 222 + return nil, err 223 + } 224 + 225 + if isSqlite { 226 + db.Exec("PRAGMA journal_mode=WAL;") 227 + db.Exec("PRAGMA synchronous=NORMAL;") 228 + db.Exec("PRAGMA busy_timeout=10000;") 229 + db.Exec("PRAGMA temp_store=MEMORY;") 230 + db.Exec("PRAGMA cache_size=8000;") 231 + db.Exec("PRAGMA wal_autocheckpoint=3000;") 232 + } else { 233 + // Configure connection pool 234 + sqlDB, err := db.DB() 235 + if err != nil { 236 + return nil, err 237 + } 238 + sqlDB.SetMaxOpenConns(maxOpenConns) 239 + sqlDB.SetMaxIdleConns(maxOpenConns) 240 + sqlDB.SetConnMaxIdleTime(time.Hour) 241 + 242 + } 243 + 244 + if err := db.AutoMigrate(&models.Repo{}, &models.RepoRecord{}, &models.OutboxBuffer{}, &models.ResyncBuffer{}, &models.FirehoseCursor{}, &models.ListReposCursor{}, &models.CollectionCursor{}); err != nil { 245 + return nil, err 246 + } 247 + 248 + return db, nil 249 + }
-1
cmd/nexus/resync.go
··· 55 55 WHERE did = ( 56 56 SELECT did FROM repos 57 57 WHERE state IN (?, ?) 58 - ORDER BY RANDOM() 59 58 LIMIT 1 60 59 ) 61 60 RETURNING did