Files
rsspipe/cmd/server/main.go
T
haopengzhan ebcaa5d72a
CI Quality Checks & Tests / Gitleaks Secret Detection (push) Failing after 1m59s
CI Quality Checks & Tests / Go Lint & Unit Tests (push) Failing after 2m26s
CI Quality Checks & Tests / Frontend Typecheck & Build (push) Successful in 2m37s
refactor(core): overhaul state transition architecture, persistent job queue, and API layer
- Implement atomic state transitions: add EntryService.PatchEntry() with EntryDiff computation and EventEntryTransition event emission.
- Add persistent SQLite task queue (JobEngine): add jobs table with crash recovery, exponential backoff retries, concurrency control, and idempotency key deduplication.
- Decompose monolithic HTTP handlers: split handlers.go into domain handlers (auth, feeds, entries, jobs, settings, rules, sse).
- Implement real-time Server-Sent Events (/api/events/stream) for multi-tab/device state synchronization.
- Fix SQLite concurrency: apply busy_timeout(10000) and journal_mode(WAL) via DSN query parameters to resolve SQLITE_BUSY (5).
- Fix AI settings & prompt loading: unmarshal JSON from ai_settings, apply configured prompt templates, and enforce strict enabled/API key validations.
- Fix undo share state: remove omitempty from ShareUUID to serialize null for state resets.
- Fix middle list counter: compute true unread/total stats from DB and clean switcher button labels.
- Adapt mobile UI for iOS: add viewport-fit=cover in index.html and calibrate safe-area insets for the bottom navigation and article reader toolbar.
- Add architectural documentation and worklog in docs/009_worklog.md.
2026-09-01 00:41:05 -07:00

116 lines
3.5 KiB
Go

package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/actions"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/api"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/auth"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/config"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/crawler"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/db"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/events"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/logger"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/rules"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/services"
"git.pengzhan.dev/haopengzhan/rsspipe/internal/worker"
"go.uber.org/zap"
)
func main() {
log := logger.InitLogger(os.Getenv("LOG_LEVEL"))
log.Info("Starting RSSPipe Server")
cfg := config.LoadConfig()
// 1. Initialize SQLite Database
database, err := db.InitDB(cfg.DBPath)
if err != nil {
log.Fatal("Database initialization failed", zap.Error(err))
}
defer func() {
_ = database.Close()
}()
log.Info("Database connected", zap.String("path", cfg.DBPath))
// 2. Initialize Auth Service & Bootstrap Admin
authService := auth.NewAuthService(database, cfg)
if err := authService.BootstrapAdmin(); err != nil {
log.Fatal("Admin bootstrap failed", zap.Error(err))
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 3. Initialize EventBus
bus := events.NewEventBus()
// 4. Initialize Domain Services
entryService := services.NewEntryService(database, bus)
jobService := services.NewJobService(database, bus)
settingsService := services.NewSettingsService(database, bus)
// 5. Start Background Crawler
cr := crawler.NewCrawler(database, bus, cfg.CrawlerIntervalMinutes, cfg.CrawlerWorkers)
cr.Start(ctx)
feedService := services.NewFeedService(database, entryService, cr)
// 6. Initialize Persistent SQLite Task Queue Engine
jobEngine := worker.NewJobEngine(database, bus, entryService, 10)
actions.RegisterJobEngineHandlers(jobEngine, database, log)
jobEngine.Start(ctx)
// 7. Legacy WorkerPool (for backward compatibility if needed)
pool := worker.NewPool(database, bus, 5, 1000)
actions.RegisterDefaultHandlers(pool, database, log)
pool.Start(ctx)
// 8. Initialize Rules Engine (Transition-Aware Evaluator)
rulesEngine := rules.NewEngine(database, bus, pool)
rulesEngine.SetEnqueuer(jobService)
rulesEngine.Start(ctx)
// 9. Mount API Routes
handler := api.NewHandler(database, authService, bus, entryService, feedService, jobService, settingsService, jobEngine)
router := api.NewRouter(handler, authService, "./web/dist")
server := &http.Server{
Addr: ":" + cfg.Port,
Handler: router,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}
// 10. Graceful shutdown handler
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
log.Info("Shutting down RSSPipe gracefully...")
cancel()
jobEngine.Stop()
pool.Stop()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownCancel()
if err := server.Shutdown(shutdownCtx); err != nil {
log.Error("Error during server shutdown", zap.Error(err))
}
}()
log.Info("RSSPipe HTTP Server listening", zap.String("url", "http://localhost:"+cfg.Port))
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal("Server error", zap.Error(err))
}
log.Info("RSSPipe Server stopped")
}