This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (47 loc) · 1.67 KB
/
index.js
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
import express from "express";
import expressPromiseRouter from "express-promise-router";
import config from "exp-config";
import assert from "assert";
import "express-async-errors";
import { logger } from "lu-logger";
import { validate } from "./lib/recipe-repo.js";
import validateConfig from "./lib/config-validator.js";
import cloudTasksRouter from "./lib/router.js";
export { default as buildContext } from "./lib/context.js";
export function route(key, fn, { queue } = {}) {
if (fn) fn.queue = queue; // Ugly hack to pass the queue along to the cloud tasks router, remove when removing pubsub support
return { [key]: fn };
}
export function start({ recipes, triggers, startServer = true }) {
assert(config.appName, "appName must be set in config");
validateConfig(config);
const router = expressPromiseRouter();
const app = express();
// use PubSubs message size limit
app.use(express.json({ limit: "32mb" }));
validate(recipes, triggers);
router.use((req, _, next) => {
// middleware to handle requests via a proxy
if (config.appName?.length && req.url.startsWith(`/${config.appName}`)) {
req.url = req.url.replace(`/${config.appName}`, "");
}
next();
});
router.get("/", (req, res) => {
res.send("Im alive - som fan!");
});
router.get("/_status", (req, res) => {
res.send({ status: "ok" });
});
app.use(router);
app.use("/v2", cloudTasksRouter(recipes, triggers));
/* c8 ignore start */
if (startServer) {
const port = process.env.PORT || 8080;
app.listen(port, () => {
logger.info(`${config.appName}: listening on port ${port}, env ${config.envName}`);
});
}
/* c8 ignore stop */
return app;
}