-
Notifications
You must be signed in to change notification settings - Fork 554
/
index.ts
136 lines (123 loc) · 4.21 KB
/
index.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
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
// catch errors that get thrown in async route handlers
// this is a relatively non-invasive change to express
// they get handled in the error.handler middleware
// leave at top of file before importing Routes
import 'express-async-errors'
import express from 'express'
import cors from 'cors'
import http from 'http'
import events from 'events'
import { Options as XrpcServerOptions } from '@atproto/xrpc-server'
import { DAY, HOUR, MINUTE, SECOND } from '@atproto/common'
import API from './api'
import * as authRoutes from './auth-routes'
import * as basicRoutes from './basic-routes'
import * as wellKnown from './well-known'
import * as error from './error'
import { loggerMiddleware } from './logger'
import { ServerConfig, ServerSecrets } from './config'
import { createServer } from './lexicon'
import { createHttpTerminator, HttpTerminator } from 'http-terminator'
import AppContext, { AppContextOptions } from './context'
import compression from './util/compression'
import { proxyHandler } from './pipethrough'
export * from './config'
export { Database } from './db'
export { DiskBlobStore } from './disk-blobstore'
export { AppContext } from './context'
export { httpLogger } from './logger'
export { createSecretKeyObject } from './auth-verifier'
export { type Handler as SkeletonHandler } from './lexicon/types/app/bsky/feed/getFeedSkeleton'
export { createServer as createLexiconServer } from './lexicon'
export * as sequencer from './sequencer'
export { type PreparedWrite } from './repo'
export * as repoPrepare from './repo/prepare'
export { scripts } from './scripts'
export class PDS {
public ctx: AppContext
public app: express.Application
public server?: http.Server
private terminator?: HttpTerminator
private dbStatsInterval?: NodeJS.Timeout
private sequencerStatsInterval?: NodeJS.Timeout
constructor(opts: { ctx: AppContext; app: express.Application }) {
this.ctx = opts.ctx
this.app = opts.app
}
static async create(
cfg: ServerConfig,
secrets: ServerSecrets,
overrides?: Partial<AppContextOptions>,
): Promise<PDS> {
const ctx = await AppContext.fromConfig(cfg, secrets, overrides)
const xrpcOpts: XrpcServerOptions = {
validateResponse: false,
payload: {
jsonLimit: 150 * 1024, // 150kb
textLimit: 100 * 1024, // 100kb
blobLimit: cfg.service.blobUploadLimit,
},
catchall: proxyHandler(ctx),
rateLimits: ctx.ratelimitCreator
? {
creator: ctx.ratelimitCreator,
global: [
{
name: 'global-ip',
durationMs: 5 * MINUTE,
points: 3000,
},
],
shared: [
{
name: 'repo-write-hour',
durationMs: HOUR,
points: 5000, // creates=3, puts=2, deletes=1
},
{
name: 'repo-write-day',
durationMs: DAY,
points: 35000, // creates=3, puts=2, deletes=1
},
],
}
: undefined,
}
let server = createServer(xrpcOpts)
server = API(server, ctx)
const app = express()
app.set('trust proxy', true)
app.use(loggerMiddleware)
app.use(compression())
app.use(authRoutes.createRouter(ctx)) // Before CORS
app.use(cors({ maxAge: DAY / SECOND }))
app.use(basicRoutes.createRouter(ctx))
app.use(wellKnown.createRouter(ctx))
app.use(server.xrpc.router)
app.use(error.handler)
return new PDS({
ctx,
app,
})
}
async start(): Promise<http.Server> {
await this.ctx.sequencer.start()
const server = this.app.listen(this.ctx.cfg.service.port)
this.server = server
this.server.keepAliveTimeout = 90000
this.terminator = createHttpTerminator({ server })
await events.once(server, 'listening')
return server
}
async destroy(): Promise<void> {
await this.ctx.sequencer.destroy()
await this.terminator?.terminate()
await this.ctx.backgroundQueue.destroy()
await this.ctx.accountManager.close()
await this.ctx.redisScratch?.quit()
await this.ctx.proxyAgent.destroy()
clearInterval(this.dbStatsInterval)
clearInterval(this.sequencerStatsInterval)
}
}
export default PDS