-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (214 loc) · 5.63 KB
/
main.go
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"flag"
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
"strconv"
"time"
"github.com/floj/serializer-go/assets"
"github.com/floj/serializer-go/config"
"github.com/floj/serializer-go/job"
"github.com/floj/serializer-go/model"
"github.com/floj/serializer-go/scraper"
"github.com/floj/serializer-go/scraper/hackernews"
"github.com/floj/serializer-go/views"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
)
func envOrDefault(name, def string) string {
v := os.Getenv(name)
if v != "" {
return v
}
return def
}
func main() {
//dbFile := flag.String("database-file", envOrDefault("DATABASE_FILE", "serializer.db"), "path to the database file")
dbURL := flag.String("db-uri", envOrDefault("DB_URI", ""), "connection uri for the DB")
scrapeInterval := flag.String("scrape-interval", envOrDefault("SCRAPE_INTERVAL", "1m"), "how often to scrape, set to 0 to disable scrape job")
scrapeTimeout := flag.String("scrape-timeout", envOrDefault("SCRAPE_TIMEOUT", "1m"), "max time one scrape job is allowed to run set to 0 to for no limit")
cookieInsecure := flag.Bool("cookie-insecure", false, "set secure flag on cookie")
logLevel := flag.String("log-level", "info", "log level (debug, info, warn, error)")
flag.Parse()
logDest := os.Stdout
tintOpts := &tint.Options{
TimeFormat: time.RFC3339,
NoColor: !isatty.IsTerminal(logDest.Fd()),
}
switch *logLevel {
case "debug":
tintOpts.Level = slog.LevelDebug
case "info":
tintOpts.Level = slog.LevelInfo
case "warn":
tintOpts.Level = slog.LevelWarn
case "error":
tintOpts.Level = slog.LevelError
default:
panic("invalid log level: " + *logLevel)
}
conf, err := config.Create(*dbURL, *scrapeInterval, *scrapeTimeout, !*cookieInsecure)
if err != nil {
panic(err)
}
slog.SetDefault(slog.New(
tint.NewHandler(logDest, tintOpts),
))
if err := run(conf); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func unreadCount(stories []model.Story, last int64) int {
i := 0
for _, s := range stories {
if s.ID <= last {
continue
}
i++
}
return i
}
func run(conf config.Config) error {
if conf.DBURI == "" {
return fmt.Errorf("db-uri is mandatory but was not set")
}
db, err := model.InitDB(conf)
if err != nil {
return fmt.Errorf("could not initialize DB connection: %w", err)
}
defer db.Close()
scrapers, err := loadScrapers(conf)
if err != nil {
return err
}
trigger, stopJob := job.Start(db, conf, scrapers...)
defer stopJob()
if conf.ScrapeEnabled() {
go trigger(func(r job.Result, err error) error {
if err != nil {
slog.Error("failed to scrape", "err", err)
return nil
}
slog.Info("scrape success", "new", r.New, "updated", r.Updated)
return nil
})
} else {
slog.Info("scraping disabled")
}
app := echo.New()
app.Use(
middleware.Recover(),
middleware.Logger(),
middleware.Secure(),
)
app.StaticFS("/assets", assets.StaticAssets())
app.GET("/", func(c echo.Context) error {
last := getLastIdFromCookie(c.Cookie("serializer-go"))
stories, err := getStories(c.Request().Context(), db, last)
if err != nil {
return err
}
unread := unreadCount(stories, last)
return views.Index(stories, last, unread).Render(c.Request().Context(), c.Response())
})
app.GET("/clear", func(c echo.Context) error {
writeCookie(c, 0, conf.CookieSecure)
return c.Redirect(http.StatusSeeOther, "/")
})
app.GET("/healthz", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]any{"status": "UP"})
})
app.POST("/", func(c echo.Context) error {
last := getLastIdFromPOST(c)
slog.Info("updating last", "last", last)
writeCookie(c, last, conf.CookieSecure)
return c.Redirect(http.StatusSeeOther, "/")
})
app.GET("/scrape", func(c echo.Context) error {
return trigger(func(r job.Result, err error) error {
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]any{
"err": err,
})
}
return c.JSON(http.StatusOK, r)
})
})
return app.Start(":3000")
}
func getLastIdFromPOST(c echo.Context) int64 {
p := c.FormValue("last")
if p == "" {
return 0
}
v, err := strconv.ParseInt(p, 10, 64)
if err != nil {
slog.Error("could not parse int", "err", err, "p", p)
return 0
}
return v
}
type CookieVal struct {
Last int64 `json:"last"`
}
func getLastIdFromCookie(c *http.Cookie, err error) int64 {
if c == nil {
return 0
}
if err != nil {
slog.Error("could not parse cookie", "err", err, "cookie", c)
return 0
}
v := CookieVal{}
cv, err := url.QueryUnescape(c.Value)
if err != nil {
slog.Error("could not url decode cookie", "err", err, "cookie", c)
return 0
}
err = json.Unmarshal([]byte(cv), &v)
if err != nil {
slog.Error("could not decode cookie value", "err", err, "cookie", c)
return 0
}
return v.Last
}
func writeCookie(c echo.Context, last int64, secure bool) error {
b := bytes.Buffer{}
err := json.NewEncoder(&b).Encode(CookieVal{Last: last})
if err != nil {
return err
}
c.SetCookie(&http.Cookie{
Name: "serializer-go",
Value: url.QueryEscape(b.String()),
Expires: time.Now().Add(time.Hour * 24 * 365),
HttpOnly: true,
Secure: secure,
})
return nil
}
func getStories(ctx context.Context, db *sql.DB, id int64) ([]model.Story, error) {
queries := model.New(db)
stories, err := queries.ListStoriesBeginningAt(ctx, id-9)
return stories, err
}
func loadScrapers(conf config.Config) ([]scraper.Scraper, error) {
httpc := &http.Client{}
hnScraper, err := hackernews.NewScraper(httpc)
if err != nil {
return nil, err
}
return []scraper.Scraper{
hnScraper,
}, nil
}