forked from bluesky-social/feed-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
73 lines (65 loc) · 1.95 KB
/
server.ts
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
import http from 'http'
import events from 'events'
import express from 'express'
import { DidResolver, MemoryCache } from '@atproto/identity'
import { createServer } from './lexicon'
import feedGeneration from './methods/feed-generation'
import describeGenerator from './methods/describe-generator'
import { createDb, Database, migrateToLatest } from './db'
import { FirehoseSubscription } from './subscription'
import { AppContext, Config } from './config'
import wellKnown from './well-known'
export class FeedGenerator {
public app: express.Application
public server?: http.Server
public db: Database
public firehose: FirehoseSubscription
public cfg: Config
constructor(
app: express.Application,
db: Database,
firehose: FirehoseSubscription,
cfg: Config,
) {
this.app = app
this.db = db
this.firehose = firehose
this.cfg = cfg
}
static create(cfg: Config) {
const app = express()
const db = createDb(cfg.sqliteLocation)
const firehose = new FirehoseSubscription(db, cfg.subscriptionEndpoint)
const didCache = new MemoryCache()
const didResolver = new DidResolver({
plcUrl: 'https://plc.directory',
didCache,
})
const server = createServer({
validateResponse: true,
payload: {
jsonLimit: 100 * 1024, // 100kb
textLimit: 100 * 1024, // 100kb
blobLimit: 5 * 1024 * 1024, // 5mb
},
})
const ctx: AppContext = {
db,
didResolver,
cfg,
}
feedGeneration(server, ctx)
describeGenerator(server, ctx)
app.use(server.xrpc.router)
app.use(wellKnown(ctx))
return new FeedGenerator(app, db, firehose, cfg)
}
async start(): Promise<http.Server> {
await migrateToLatest(this.db)
this.firehose.run(this.cfg.subscriptionReconnectDelay)
this.server = this.app.listen(this.cfg.port, this.cfg.listenhost)
await events.once(this.server, 'listening')
return this.server
}
}
export default FeedGenerator