2023-05-10 14:56:30 +00:00
|
|
|
import http from 'http'
|
|
|
|
import events from 'events'
|
|
|
|
import express from 'express'
|
2023-05-11 04:20:44 +00:00
|
|
|
import { DidResolver, MemoryCache } from '@atproto/did-resolver'
|
2023-05-10 14:56:30 +00:00
|
|
|
import { createServer } from './lexicon'
|
2023-05-19 15:31:28 +00:00
|
|
|
import feedGeneration from './methods/feed-generation'
|
|
|
|
import describeGenerator from './methods/describe-generator'
|
2023-05-11 04:00:44 +00:00
|
|
|
import { createDb, Database, migrateToLatest } from './db'
|
2023-05-10 14:56:30 +00:00
|
|
|
import { FirehoseSubscription } from './subscription'
|
2023-05-11 04:20:44 +00:00
|
|
|
import { AppContext, Config } from './config'
|
2023-05-11 23:43:47 +00:00
|
|
|
import wellKnown from './well-known'
|
2023-05-10 14:56:30 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-19 15:31:39 +00:00
|
|
|
static create(cfg: Config) {
|
2023-05-10 14:56:30 +00:00
|
|
|
const app = express()
|
|
|
|
const db = createDb(cfg.sqliteLocation)
|
|
|
|
const firehose = new FirehoseSubscription(db, cfg.subscriptionEndpoint)
|
|
|
|
|
2023-05-11 04:20:44 +00:00
|
|
|
const didCache = new MemoryCache()
|
|
|
|
const didResolver = new DidResolver(
|
|
|
|
{ plcUrl: 'https://plc.directory' },
|
|
|
|
didCache,
|
|
|
|
)
|
|
|
|
|
2023-05-10 14:56:30 +00:00
|
|
|
const server = createServer({
|
|
|
|
validateResponse: true,
|
|
|
|
payload: {
|
|
|
|
jsonLimit: 100 * 1024, // 100kb
|
|
|
|
textLimit: 100 * 1024, // 100kb
|
|
|
|
blobLimit: 5 * 1024 * 1024, // 5mb
|
|
|
|
},
|
|
|
|
})
|
2023-05-11 04:20:44 +00:00
|
|
|
const ctx: AppContext = {
|
|
|
|
db,
|
|
|
|
didResolver,
|
|
|
|
cfg,
|
|
|
|
}
|
|
|
|
feedGeneration(server, ctx)
|
2023-05-19 15:31:28 +00:00
|
|
|
describeGenerator(server, ctx)
|
2023-05-10 14:56:30 +00:00
|
|
|
app.use(server.xrpc.router)
|
2023-05-19 15:31:39 +00:00
|
|
|
app.use(wellKnown(ctx))
|
2023-05-10 14:56:30 +00:00
|
|
|
|
|
|
|
return new FeedGenerator(app, db, firehose, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
async start(): Promise<http.Server> {
|
2023-05-11 04:00:44 +00:00
|
|
|
await migrateToLatest(this.db)
|
|
|
|
this.firehose.run()
|
2023-05-24 22:53:37 +00:00
|
|
|
this.server = this.app.listen(this.cfg.port, this.cfg.listenhost)
|
2023-05-11 04:00:44 +00:00
|
|
|
await events.once(this.server, 'listening')
|
|
|
|
return this.server
|
2023-05-10 14:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FeedGenerator
|