forked from planetarium/Corvette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
155 lines (145 loc) · 3.92 KB
/
app.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { parse } from "std/flags/mod.ts";
import * as path from "std/path/mod.ts";
import { ConsoleHandler } from "std/log/handlers.ts";
import { getLogger, setup as setupLog } from "std/log/mod.ts";
import { parseOptions } from "amqp/src/amqp_connect_options.ts";
import { broker } from "https://deno.land/x/[email protected]/mod.ts";
import { api } from "./api.ts";
import { emitter } from "./emitter.ts";
import { AmqpBrokerUrlEnvKey, combinedEnv } from "./envUtils.ts";
import {
ApiLoggerName,
defaultLogFormatter,
DevLoggerName,
EmitterLoggerName,
getInternalLoggers,
ObserverLoggerName,
TestWebhookReceiverLoggerName,
WebLoggerName,
} from "./logUtils.ts";
import { observer } from "./observer.ts";
import {
block,
runWithAmqp,
runWithChainDefinition,
runWithPrisma,
} from "./runHelpers.ts";
import { testWebhookReceiver } from "./testWebhookReceiver.ts";
async function prepareAndMain() {
const status = await new Deno.Command("deno", {
args: ["task", "prisma", "db", "push"],
stdout: "inherit",
stderr: "inherit",
}).spawn().status;
if (!status.success) Deno.exit(status.code);
new Deno.Command("deno", {
args: [
"run",
"--allow-read",
"--allow-write",
"--allow-env",
"--allow-run",
"--allow-net",
"--allow-ffi",
path.fromFileUrl(import.meta.url),
"--main",
],
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
cwd: Deno.cwd(),
uid: Deno.uid() !== null ? Deno.uid()! : undefined,
gid: Deno.gid() !== null ? Deno.gid()! : undefined,
}).spawn().ref();
}
async function main() {
setupLog({
handlers: {
console: new ConsoleHandler("DEBUG", {
formatter: defaultLogFormatter,
}),
},
loggers: {
...getInternalLoggers({ level: "DEBUG", handlers: ["console"] }),
[DevLoggerName]: {
level: "DEBUG",
handlers: ["console"],
},
[ObserverLoggerName]: {
level: "DEBUG",
handlers: ["console"],
},
[EmitterLoggerName]: {
level: "DEBUG",
handlers: ["console"],
},
[ApiLoggerName]: {
level: "DEBUG",
handlers: ["console"],
},
[WebLoggerName]: {
level: "DEBUG",
handlers: ["console"],
},
[TestWebhookReceiverLoggerName]: {
level: "INFO",
handlers: ["console"],
},
lop: {
level: "INFO",
handlers: ["console"],
},
},
});
const logger = getLogger(DevLoggerName);
logger.debug(
`Serving AMQP Broker, URL: ${combinedEnv[AmqpBrokerUrlEnvKey]}.`,
);
const amqpOptions = parseOptions(combinedEnv[AmqpBrokerUrlEnvKey]);
const abortBroker = broker({
hostname: amqpOptions.hostname,
port: amqpOptions.port,
});
try {
await runWithChainDefinition((chain) => ({
runningPromise: runWithPrisma(async (prisma) => {
const runningPromise = runWithAmqp(async (amqpConnection) => {
const { cleanup: cleanupObserver } = await observer(
chain,
prisma,
amqpConnection,
);
const { cleanup: cleanupEmitter } = await emitter(
chain,
prisma,
amqpConnection,
);
const { cleanup: cleanupApi } = await api(prisma);
return {
runningPromise: block(),
cleanup: async () => {
await cleanupApi();
await cleanupEmitter();
await cleanupObserver();
},
};
});
const { cleanup: cleanupTestWebhookReceiver } =
await testWebhookReceiver();
return {
runningPromise,
cleanup: async () => {
await cleanupTestWebhookReceiver();
},
};
}),
}));
} finally {
abortBroker();
}
}
if (import.meta.main) {
const params = parse(Deno.args, { boolean: ["main"] });
if (params.main) await main();
else prepareAndMain();
}