my monorepo for atproto based applications
at main 73 lines 2.0 kB view raw
1import http from "http"; 2import events from "events"; 3import express from "express"; 4import { DidResolver, MemoryCache } from "@atproto/identity"; 5import { createServer } from "@my/lexicon/server"; 6import feedGeneration from "@/methods/feed-generation"; 7import describeGenerator from "@/methods/describe-generator"; 8import { createDb, Database, migrateToLatest } from "@/db"; 9import { FirehoseSubscription } from "@/subscription"; 10import { AppContext, Config } from "@/config"; 11import wellKnown from "@/well-known"; 12 13export class FeedGenerator { 14 public app: express.Application; 15 public server?: http.Server; 16 public db: Database; 17 public firehose: FirehoseSubscription; 18 public cfg: Config; 19 20 constructor( 21 app: express.Application, 22 db: Database, 23 firehose: FirehoseSubscription, 24 cfg: Config, 25 ) { 26 this.app = app; 27 this.db = db; 28 this.firehose = firehose; 29 this.cfg = cfg; 30 } 31 32 static create(cfg: Config) { 33 const app = express(); 34 const db = createDb(cfg.sqliteLocation); 35 const firehose = new FirehoseSubscription(db, cfg.subscriptionEndpoint); 36 37 const didCache = new MemoryCache(); 38 const didResolver = new DidResolver({ 39 plcUrl: "https://plc.directory", 40 didCache, 41 }); 42 43 const server = createServer({ 44 validateResponse: true, 45 payload: { 46 jsonLimit: 100 * 1024, // 100kb 47 textLimit: 100 * 1024, // 100kb 48 blobLimit: 5 * 1024 * 1024, // 5mb 49 }, 50 }); 51 const ctx: AppContext = { 52 db, 53 didResolver, 54 cfg, 55 }; 56 feedGeneration(server, ctx); 57 describeGenerator(server, ctx); 58 app.use(server.xrpc.router); 59 app.use(wellKnown(ctx)); 60 61 return new FeedGenerator(app, db, firehose, cfg); 62 } 63 64 async start(): Promise<http.Server> { 65 await migrateToLatest(this.db); 66 this.firehose.run(this.cfg.subscriptionReconnectDelay); 67 this.server = this.app.listen(this.cfg.port, this.cfg.listenhost); 68 await events.once(this.server, "listening"); 69 return this.server; 70 } 71} 72 73export default FeedGenerator;