-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
183 lines (151 loc) · 4.79 KB
/
Copy pathmain.go
File metadata and controls
183 lines (151 loc) · 4.79 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/aidenappl/SentimentScraperAPI/background"
"github.com/aidenappl/SentimentScraperAPI/db"
"github.com/aidenappl/SentimentScraperAPI/env"
"github.com/aidenappl/SentimentScraperAPI/logging"
"github.com/aidenappl/SentimentScraperAPI/middleware"
"github.com/aidenappl/SentimentScraperAPI/routers"
"github.com/aidenappl/SentimentScraperAPI/scraper"
"github.com/aidenappl/SentimentScraperAPI/sentiment"
"github.com/aidenappl/SentimentScraperAPI/state"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
func main() {
logging.Init(env.LogLevel, env.LogSummaryInterval)
if err := env.Validate(); err != nil {
logging.Fatal("invalid configuration", "err", err)
}
if env.CrawlBlockedDomains == "none" {
scraper.SetBlockedDomains(nil)
} else if env.CrawlBlockedDomains != "" {
scraper.SetBlockedDomains(strings.Split(env.CrawlBlockedDomains, ","))
}
// Ping DB
if err := db.PingDB(); err != nil {
logging.Fatal("failed to connect to the database", "err", err)
}
slog.Info("connected to the database")
// SIGTERM is what Docker sends on stop, and it arrives roughly ten seconds
// before SIGKILL — everything below the cancel must finish inside that.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
var wg sync.WaitGroup
go sentiment.StartSentimentWorker(ctx)
// Emit one crawl summary per interval, plus a final line on shutdown.
wg.Add(1)
go func() {
defer wg.Done()
logging.Crawl.Run(ctx, env.LogSummaryInterval)
}()
// Hydrate News Cache
if err := state.HydrateNewsCache(); err != nil {
logging.Fatal("failed to hydrate news cache", "err", err)
}
slog.Info("news cache hydrated")
r := mux.NewRouter()
// Request logger
r.Use(middleware.LoggingMiddleware)
// Base API Endpoint
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Welcome to the SentimentScraper API!"))
}).Methods(http.MethodGet)
// Health Check Endpoint
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}).Methods(http.MethodGet)
// Core V1 API Endpoint
core := r.PathPrefix("/core/v1/").Subrouter()
// Get All News
core.HandleFunc("/trending", routers.GetTrendingNews).Methods(http.MethodGet)
core.HandleFunc("/hydrateTickers", routers.HydrateTickers).Methods(http.MethodPost)
core.HandleFunc("/news", routers.ListNews).Methods(http.MethodGet)
core.HandleFunc("/news/{id}", routers.GetNews).Methods(http.MethodGet)
// Background Handlers
wg.Add(1)
go func() {
defer wg.Done()
runCrawlLoop(ctx)
}()
// CORS Middleware
corsMiddleware := cors.New(cors.Options{
AllowedOrigins: []string{
"http://localhost:3000",
"http://localhost:8001",
"https://sentimentscraper.com",
},
AllowCredentials: true,
AllowedHeaders: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
})
// Start Healthcheck Polling
go background.StartHealthCheckPolling(ctx)
server := &http.Server{
Addr: ":" + env.Port,
Handler: corsMiddleware.Handler(r),
ReadTimeout: 15 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
}
go func() {
slog.Info("SentimentScraper API listening",
"port", env.Port,
"log_level", env.LogLevel,
"summary_interval", env.LogSummaryInterval.String(),
)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logging.Fatal("http server failed", "err", err)
}
}()
<-ctx.Done()
stop()
slog.Info("shutdown signal received, draining")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
slog.Error("http server shutdown failed", "err", err)
}
// Wait for the crawl loop and the summary emitter: without this the
// process exits before the final summary line is written, losing exactly
// the interval that explains why the service stopped.
wg.Wait()
slog.Info("shutdown complete")
}
// runCrawlLoop polls the feed and crawls outstanding articles until ctx is
// cancelled. It uses a ticker rather than sleeping at the end of the body, so
// a slow cycle does not push every later cycle further out of step.
func runCrawlLoop(ctx context.Context) {
cycle := func() {
slog.Debug("fetching feeds")
if err := state.HydrateNewsCache(); err != nil {
slog.Error("failed to hydrate news cache", "reason", "query", "err", err)
return
}
background.NewsFilter()
background.CheckCrawlers()
}
cycle()
t := time.NewTicker(env.CrawlInterval)
defer t.Stop()
for {
select {
case <-t.C:
cycle()
case <-ctx.Done():
return
}
}
}