-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
107 lines (88 loc) · 2.24 KB
/
main.go
File metadata and controls
107 lines (88 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
//go:generate go run install_tools.go
import (
"context"
_ "embed"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/ppowo/feedlet/internal/config"
"github.com/ppowo/feedlet/internal/fetcher"
"github.com/ppowo/feedlet/internal/logging"
"github.com/ppowo/feedlet/internal/server"
"github.com/ppowo/feedlet/web"
)
var (
shutdownOnce sync.Once
)
func main() {
if err := logging.Setup(); err != nil {
log.Fatal(err)
}
// Load embedded configuration
cfg := config.GetConfig()
// Create fetcher with configuration
f := fetcher.NewFromConfigs(cfg.Sources, cfg.MinFetchInterval, cfg.MaxSubscribers)
// Start fetcher in background
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Give fetcher a moment to initialize
go f.Start(ctx)
time.Sleep(100 * time.Millisecond)
// Create and start server
port := cfg.Port
if port == 0 {
port = 8080
}
// Build source limits and days maps
sourceLimits := make(map[string]int)
sourceDays := make(map[string]int)
for _, srcCfg := range cfg.Sources {
if srcCfg.Limit > 0 {
sourceLimits[srcCfg.Name] = srcCfg.Limit
} else if cfg.DefaultSourceLimit > 0 {
sourceLimits[srcCfg.Name] = cfg.DefaultSourceLimit
}
if srcCfg.Days > 0 {
sourceDays[srcCfg.Name] = srcCfg.Days
} else {
sourceDays[srcCfg.Name] = 2 // Default 2 days
}
}
srv, err := server.New(f, web.IndexTemplate, port, cfg.Sources, sourceLimits, sourceDays)
if err != nil {
log.Fatal(err)
}
// Handle graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
shutdownOnce.Do(func() {
log.Println("Shutting down...")
cancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("Server shutdown error: %v", err)
}
done := make(chan struct{})
go func() {
defer close(done)
f.Shutdown()
}()
select {
case <-done:
log.Println("Fetcher shutdown complete")
case <-time.After(10 * time.Second):
log.Println("Fetcher shutdown timeout - proceeding anyway")
}
})
}()
if err := srv.Start(); err != nil {
log.Fatal(err)
}
}