-
Notifications
You must be signed in to change notification settings - Fork 3
/
module.go
207 lines (178 loc) · 5.83 KB
/
module.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
package xrss
import (
"context"
"database/sql"
"fmt"
"io/fs"
"log/slog"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/infogulch/watch"
"github.com/infogulch/xtemplate"
"github.com/infogulch/xtemplate/providers"
natsxt "github.com/infogulch/xtemplate/providers/nats"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"go.uber.org/zap/exp/zapslog"
_ "github.com/mattn/go-sqlite3"
)
func init() {
caddy.RegisterModule(XRssModule{})
httpcaddyfile.RegisterHandlerDirective("xrss", func(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
return &XRssModule{}, nil
})
}
// CaddyModule returns the Caddy module information.
func (XRssModule) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.xrss",
New: func() caddy.Module { return new(XRssModule) },
}
}
func (m *XRssModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
m.Handler.ServeHTTP(w, r)
return nil
}
var _ caddyhttp.MiddlewareHandler = (*XRssModule)(nil)
type XRssModule struct {
http.Handler
cancel func()
}
// Provision provisions t. Implements caddy.Provisioner.
func (m *XRssModule) Provision(ctx caddy.Context) error {
srv, err := server.NewServer(&server.Options{DontListen: true, JetStream: true})
if err != nil {
return fmt.Errorf("failed to create nats server: %w", err)
}
srv.Start()
conn, err := nats.Connect("", nats.InProcessServer(srv))
if err != nil {
return fmt.Errorf("failed to create nats conn: %w", err)
}
js, err := jetstream.New(conn)
if err != nil {
return fmt.Errorf("failed to create jetstream: %w", err)
}
kv, err := js.CreateKeyValue(context.Background(), jetstream.KeyValueConfig{Bucket: "rss"})
if err != nil {
return fmt.Errorf("failed to create key value: %w", err)
}
connstr := "file:rss.sqlite?_journal=WAL&_synchronous=NORMAL&cache=shared&_cache_size=1000000000&_busy_timeout=5000"
wdb, err := sql.Open("sqlite3", connstr+"&mode=rwc&_txlock=immediate&_foreign_keys=true&_vacuum=full")
if err != nil {
return fmt.Errorf("failed to create writable db: %w", err)
}
wdb.SetMaxOpenConns(1)
rdb, err := sql.Open("sqlite3", connstr+"&mode=ro&_txlock=deferred")
if err != nil {
return fmt.Errorf("failed to created readonly db: %w", err)
}
rdb.SetMaxOpenConns(runtime.NumCPU())
// Wrap zap logger into a slog logger for xtemplate
log := slog.New(zapslog.NewHandler(ctx.Logger().Core(), nil)).WithGroup("xrss")
err = migrate(wdb, log)
if err != nil {
return fmt.Errorf("failed to apply migrations: %w", err)
}
for i := range 5 {
wlog := log.With(slog.Int("fetch-worker", i))
go fetchWorker(wdb, rdb, kv, conn, wlog)
}
config, err := xtemplate.New().Defaults().Options(
providers.WithKV("Config", make(map[string]string)),
providers.WithDB("DB", rdb, &sql.TxOptions{ReadOnly: true}),
natsxt.WithKV("KV", kv),
natsxt.WithConn("Nats", conn),
xtemplate.WithLogger(log),
)
if err != nil {
return fmt.Errorf("failed to configure xtemplate: %w", err)
}
server, err := config.Server()
if err != nil {
return fmt.Errorf("failed to start xtemplate server: %w", err)
}
halt, err := watch.Watch([]string{"templates"}, 200*time.Millisecond, log.WithGroup("fswatch"), func() bool {
server.Reload()
return true
})
if err != nil {
return fmt.Errorf("failed to start fswatch: %w", err)
}
m.Handler = server.Handler()
m.cancel = func() {
server.Stop()
srv.Shutdown()
conn.Close()
wdb.Close()
rdb.Close()
halt <- struct{}{}
m.Handler = nil
m.cancel = nil
}
return nil
}
// Cleanup discards resources held by t. Implements caddy.CleanerUpper.
func (m *XRssModule) Cleanup() error {
if m.cancel != nil {
m.cancel()
}
return nil
}
func migrate(db *sql.DB, log *slog.Logger) error {
var start_user_version, curent_user_version int64
err := db.QueryRow(`PRAGMA user_version;`).Scan(&start_user_version)
if err != nil {
return fmt.Errorf("failed to get current user_version: %w", err)
}
count := 0
re := regexp.MustCompile(`schema.(\d+).sql`)
if err = filepath.WalkDir("./migrations", func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
m := re.FindStringSubmatch(filepath.Base(path))
if m == nil {
return fmt.Errorf("failed to match schema migration file '%s'", path)
}
i, _ := strconv.ParseInt(m[1], 10, 0)
if i <= start_user_version {
log.Debug("skipped file", slog.String("path", path), slog.Int64("path_version", i), slog.Int64("start_user_version", start_user_version))
return err
}
log.Debug("applying migration", slog.String("path", path), slog.Int64("path_version", i), slog.Int64("start_user_version", start_user_version))
migration, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read migration file '%s': %w", path, err)
}
_, err = db.Exec(string(migration))
if err != nil {
return fmt.Errorf("failed to exec migration file '%s': %w", path, err)
}
err = db.QueryRow(`PRAGMA user_version;`).Scan(&curent_user_version)
if err != nil {
return fmt.Errorf("failed to get current user_version: %w", err)
}
log.Debug("applied migration", slog.String("file", path), slog.Int64("curent_user_version", curent_user_version))
count++
return nil
}); err != nil {
return fmt.Errorf("failed to apply migration file: %w", err)
}
var end_user_version int64
err = db.QueryRow(`PRAGMA user_version;`).Scan(&end_user_version)
if err != nil {
return fmt.Errorf("failed to get current user_version: %w", err)
}
log.Info("applied migrations", slog.Int("count", count), slog.Int64("start_user_version", start_user_version), slog.Int64("curent_user_version", curent_user_version))
return nil
}