Skip to content

Commit

Permalink
refactor(server, cors): simplified cors configuration (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinTh authored Aug 31, 2024
1 parent da85a10 commit 57330fc
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ You can configure the application using environment variables. Here are the avai
| Environment Variable | Description | Default Value |
| -------------------- | ----------- | ------------- |
| `PORT` | The port to listen on when using node server | `8787` |
| `CORS_ORIGIN` | The CORS origin the server should allow | `*` |
| `SERVER_CORS_ORIGINS` | The CORS origin for the api server | _No default value_ |
| `NOTES_MAX_ENCRYPTED_CONTENT_LENGTH` | The maximum length of the encrypted content of a note allowed by the api | `5242880` |
| `TASK_DELETE_EXPIRED_NOTES_ENABLED` | Whether to enable a periodic task to delete expired notes (not available for cloudflare) | `true` |
| `TASK_DELETE_EXPIRED_NOTES_CRON` | The frequency with which to run the task to delete expired notes (cron syntax) | `0 * * * *` |
Expand Down
5 changes: 2 additions & 3 deletions packages/app-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
"dev:cloudflare": "wrangler dev --test-scheduled --port=8787 src/index.cloudflare.ts",
"build:cloudflare": "esbuild --bundle src/index.cloudflare.ts --format=esm --outfile=dist-cloudflare/_worker.js --minify",
"build:node": "esbuild --bundle src/index.node.ts --platform=node --format=cjs --outfile=dist-node/index.cjs --minify",
"preview": "wrangler pages dev",
"deploy": "$npm_execpath run build && wrangler pages deploy",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test": "vitest run",
"test:watch": "vitest watch",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"script:generate-config-table": "tsx -r dotenv/config src/scripts/generate-config-table.script.ts"
},
"dependencies": {
"@hono/node-server": "^1.12.1",
Expand Down
8 changes: 4 additions & 4 deletions packages/app-server/src/modules/app/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export const configDefinition = {
default: 8787,
env: 'PORT',
},
corsOrigin: {
doc: 'The CORS origin the server should allow',
corsOrigins: {
doc: 'The CORS origin for the api server',
schema: z.union([
z.string(),
z.array(z.string()),
]).transform(value => (typeof value === 'string' ? value.split(',') : value)),
default: '*',
env: 'CORS_ORIGIN',
default: [],
env: 'SERVER_CORS_ORIGINS',
},
},
notes: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { cors } from 'hono/cors';
import { createMiddleware } from 'hono/factory';
import type { Context } from '../server.types';

export const corsMiddleware = cors({
origin: (origin, context: Context) => {
const allowedOrigins = context.get('config').server.corsOrigin;
export const corsMiddleware = createMiddleware(async (context: Context, next) => {
const { server: { corsOrigins } } = context.get('config');

if (allowedOrigins.length === 1 && allowedOrigins[0] === '*') {
return origin;
}
const corsHandler = cors({ origin: corsOrigins });

return allowedOrigins.find(allowedOrigin => allowedOrigin === origin);
},
credentials: true,
return corsHandler(context, next);
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import _ from 'lodash-es';
import { isArray, isEmpty, isNil } from 'lodash-es';
import type { ConfigDefinition, ConfigDefinitionElement } from 'figue';
import { fs } from 'zx';
import { configDefinition } from '../modules/app/config/config';
Expand All @@ -25,7 +25,8 @@ const configDetails = walk(configDefinition);
const rows = configDetails
.filter(({ path }) => path[0] !== 'env')
.map(({ doc, default: defaultValue, env }) => {
const defaultValueString = _.isNil(defaultValue) ? 'No default value' : `\`${defaultValue}\``;
const isEmptyDefaultValue = isNil(defaultValue) || (isArray(defaultValue) && isEmpty(defaultValue));
const defaultValueString = isEmptyDefaultValue ? '_No default value_' : `\`${defaultValue}\``;

return `| \`${env}\` | ${doc} | ${defaultValueString} |`;
});
Expand Down

0 comments on commit 57330fc

Please sign in to comment.