-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defer app import to keep app.js simpler
- Loading branch information
Showing
3 changed files
with
31 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
import request from "supertest"; | ||
|
||
import createApp from "../app.js"; | ||
|
||
/** | ||
* @returns {Promise<import("supertest").Agent>} | ||
*/ | ||
export async function createRequest() { | ||
const app = await createApp(); | ||
const { default: app } = await import("../app.js"); | ||
return request(app); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,42 @@ | ||
import express from "express"; | ||
|
||
import apiRouter from "./api.js"; | ||
import { testConnection } from "./db.js"; | ||
import config from "./utils/config.cjs"; | ||
import { | ||
asyncHandler, | ||
clientRouter, | ||
configuredHelmet, | ||
configuredMorgan, | ||
httpsOnly, | ||
logErrors, | ||
} from "./utils/middleware.js"; | ||
|
||
const API_ROOT = "/api"; | ||
|
||
/** @type {import("express").Application} */ | ||
let app; | ||
const app = express(); | ||
|
||
export default async function createApp() { | ||
if (app) { | ||
return app; | ||
} | ||
app.use(express.json()); | ||
app.use(configuredHelmet()); | ||
app.use(configuredMorgan()); | ||
|
||
const middleware = await import("./utils/middleware.js"); | ||
|
||
app = express(); | ||
|
||
app.use(express.json()); | ||
app.use(middleware.configuredHelmet()); | ||
app.use(middleware.configuredMorgan()); | ||
|
||
if (config.production) { | ||
app.enable("trust proxy"); | ||
app.use(middleware.httpsOnly()); | ||
} | ||
if (config.production) { | ||
app.enable("trust proxy"); | ||
app.use(httpsOnly()); | ||
} | ||
|
||
const { testConnection } = await import("./db.js"); | ||
app.get( | ||
"/healthz", | ||
middleware.asyncHandler(async (_, res) => { | ||
await testConnection(); | ||
res.sendStatus(200); | ||
}), | ||
); | ||
app.get( | ||
"/healthz", | ||
asyncHandler(async (_, res) => { | ||
await testConnection(); | ||
res.sendStatus(200); | ||
}), | ||
); | ||
|
||
const { default: apiRouter } = await import("./api.js"); | ||
app.use(API_ROOT, apiRouter); | ||
app.use(API_ROOT, apiRouter); | ||
|
||
app.use(middleware.clientRouter(API_ROOT)); | ||
app.use(clientRouter(API_ROOT)); | ||
|
||
app.use(middleware.logErrors()); | ||
app.use(logErrors()); | ||
|
||
return app; | ||
} | ||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters