-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.ts
59 lines (47 loc) · 1.65 KB
/
worker.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
import schema from "db:schema";
import { EmailAccountRecoveryCodeJob } from "app:jobs/email-account-recovery-code.js";
import { SyncUserWithGravatarJob } from "app:jobs/sync-user-with-gravatar.js";
import { CleanupSessionsTask } from "app:tasks/cleanup-sessions.js";
import type { Request, Response } from "@cloudflare/workers-types";
import { IPAddress, UserAgent } from "edgekitjs";
import { bootstrap } from "edgekitjs/worker";
import { createRequestHandler } from "react-router";
export default bootstrap({
orm: { schema },
rateLimit: { limit: 1000, period: 60 },
jobs() {
return [new SyncUserWithGravatarJob(), new EmailAccountRecoveryCodeJob()];
},
tasks() {
return [new CleanupSessionsTask().hourly()];
},
async onRequest(request) {
let handler = createRequestHandler(
// @ts-expect-error The file may not exists in dev, or the type will be different
() => import("./build/server/index.js"),
"production",
);
let context = await getLoadContext(request);
// @ts-expect-error The RR handler expects a Request with a different type
return (await handler(request, context)) as Response;
},
});
async function getLoadContext(request: Request) {
let ua = UserAgent.fromRequest(request);
let ip = IPAddress.fromRequest(request);
return { ua, ip };
}
declare module "edgekitjs" {
export interface Environment {
// 👇 Env variables
GRAVATAR_API_TOKEN: string;
SESSION_SECRET: string;
APP_ENV: "development" | "production";
}
type Schema = typeof schema;
export interface DatabaseSchema extends Schema {}
}
declare module "react-router" {
export interface AppLoadContext
extends Awaited<ReturnType<typeof getLoadContext>> {}
}