Skip to content

Commit be6d73f

Browse files
committed
feat: 🔧 Add CORS constant/env variable, add healthz route
Add `HLAMBDA_CORS_DOMAIN`. By default, all CORS requests to the Hlambda server are allowed. To run with more restrictive CORS settings, use this env variable. Example: `https://*.foo.bar.com:8080, http://*.localhost, http://localhost:3000, http://example.com`
1 parent a8673d3 commit be6d73f

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ PRIVATE_KEY_CONFIGURATION="__INSERT_YOUR_PRIVATE_KEY_CONFIGURATION__"
3030
# Server max allowed body size from client that express app will support. (Main usecase is Apple Subscription Notifications)
3131
SERVER_BODY_SIZE="2mb"
3232

33+
# Constant reference in code: ENV_HLAMBDA_CORS_DOMAIN | Default value: *
34+
# By default, all CORS requests to the Hlambda server are allowed. To run with more restrictive CORS settings, use this env variable. Example: `https://*.foo.bar.com:8080, http://*.localhost, http://localhost:3000, http://example.com`
35+
HLAMBDA_CORS_DOMAIN="*"
36+
3337
# Constant reference in code: ENV_SERVER_HEALTH | Default value: Healthy
3438
# Server health that can change based on different events "Healthy", "Degraded", "Unhealthy", "Advisory"
3539
SERVER_HEALTH="Healthy"

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Todo
1+
# (Future) Release 0.0.9
22

3-
- Default GET /healthz if does not exist.
3+
- undefined :)
44

55
# Release 0.0.8
66

@@ -9,3 +9,5 @@
99
- Add example for the static serving of the content because `__dirname` is not available, `fileURLToPath(import.meta.url);` should be used.
1010
- Add version build number route. (GET /build-number)
1111
- Add timestamp to docker image on build `./image-build-timestamp.txt`
12+
- Add GET /healthz route
13+
- Add CORS env variable `HLAMBDA_CORS_DOMAIN`, by default Hlambda server continues to allow '\*'

src/constants/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ export const constants = {
6969
'Server max allowed body size from client that express app will support. (Main usecase is Apple Subscription Notifications)',
7070
},
7171

72+
ENV_HLAMBDA_CORS_DOMAIN: {
73+
name: 'HLAMBDA_CORS_DOMAIN',
74+
default: '*',
75+
description:
76+
'By default, all CORS requests to the Hlambda server are allowed. To run with more restrictive CORS settings, use this env variable. Example: `https://*.foo.bar.com:8080, http://*.localhost, http://localhost:3000, http://example.com`',
77+
},
78+
7279
ENV_SERVER_HEALTH: {
7380
name: 'SERVER_HEALTH',
7481
default: 'Healthy',

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import middlewareProtector from './routes/protector.js';
2525
// import routeConsole from './routes/console.js';
2626
import route404 from './routes/404.js';
2727
import hasuraErrorHandler from './routes/hasuraErrorHandler.js';
28+
import healthzRouter from './routes/health/public-router.healthz.js';
2829
// import requestTimeLogger from './utils/requestTimer.js';
2930

3031
import generateDotEnvFileFromConsts from './utils/generateDotEnvFileFromConsts.js';
@@ -153,6 +154,7 @@ const spinServer = async () => {
153154
// const SERVER_VERSION = getEnvValue(constants.ENV_SERVER_VERSION);
154155
const HLAMBDA_DISABLE_CONSOLE = isEnvTrue(constants.ENV_HLAMBDA_DISABLE_CONSOLE);
155156
const HLAMBDA_DISABLE_INITIAL_ROUTE_REDIRECT = isEnvTrue(constants.ENV_HLAMBDA_DISABLE_INITIAL_ROUTE_REDIRECT);
157+
const HLAMBDA_CORS_DOMAIN = getEnvValue(constants.ENV_HLAMBDA_CORS_DOMAIN);
156158
// --------------------------------------------------------------------------------
157159
const app = express();
158160
// --------------------------------------------------------------------------------
@@ -163,7 +165,7 @@ const spinServer = async () => {
163165
app.use(express.json());
164166

165167
// Allow cors everywhere, it make sense for this usecase, unsafe otherwise!
166-
app.use(cors());
168+
app.use(cors({ origin: HLAMBDA_CORS_DOMAIN }));
167169

168170
if (!HLAMBDA_DISABLE_CONSOLE) {
169171
// Serve static
@@ -279,6 +281,8 @@ const spinServer = async () => {
279281
);
280282
}
281283
// --------------------------------------------------------------------------------
284+
// Add healthz route.
285+
app.use(healthzRouter);
282286
// Handle 404 routes.
283287
app.use(route404);
284288
// !!! Important !!! Error handler.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express from 'express';
2+
import asyncHandler from 'express-async-handler';
3+
4+
// Create express router
5+
const router = express.Router();
6+
7+
router.get(
8+
'/healthz',
9+
asyncHandler(async (req, res) => {
10+
res.status(200).send('OK');
11+
})
12+
);
13+
14+
export default router;

0 commit comments

Comments
 (0)