Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Commit

Permalink
feat: added basic fastify boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Jul 25, 2021
1 parent 651e2d9 commit f1ca5d4
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
coverage
packages/express-krabs/dev
packages/express-fastify/dev
packages/fastify-krabs/dev

# Created by https://www.toptal.com/developers/gitignore/api/node,osx,visualstudiocode,intellij,windows,linux
# Edit at https://www.toptal.com/developers/gitignore?templates=node,osx,visualstudiocode,intellij,windows,linux
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"prepare": "npm run test && npm run build",
"build": "yarn build:express && yarn build:fastify",
"build:express": "tsc packages/express-krabs/index.ts --outDir packages/express-krabs/dist --declarationDir packages/express-krabs/dist --declaration true",
"build:fastify": "tsc packages/express-fastify/index.ts --outDir packages/express-fastify/dist --declarationDir packages/express-fastify/dist --declaration true"
"build:fastify": "tsc packages/fastify-krabs/index.ts --outDir packages/fastify-krabs/dist --declarationDir packages/fastify-krabs/dist --declaration true"
},
"dependencies": {
"chalk": "^4.1.1",
Expand Down
58 changes: 58 additions & 0 deletions packages/fastify-krabs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as chalk from 'chalk';
import type { FastifyRequest, FastifyReply } from 'fastify';
import { currentEnv, safeEnv } from '../utils/env';
import { getTenantConfig } from '../utils/config';
import { Config } from '../utils/config/config';
import findTenant from '../utils/tenants/findTenant';
import resolveRoutes from '../utils/routes/resolve';

type Merge<A, B> = { [K in keyof (A | B)]: K extends keyof B ? B[K] : A[K] };

if (!currentEnv) {
const warningMessage = `
\u{26A0}\u{FE0F} ${chalk.bold(' Warning ')}
The ${chalk.bold('NODE_ENV')} environment variable is ${chalk.bold('undefined')}.
Krabs will run in ${chalk.bold(safeEnv)} mode, meaning it will only serve
tenants domains set as ${chalk.bold(safeEnv)} domains.
`
.split('\n')
.map((line) => line.trimLeft())
.join('\n');

console.warn(chalk.yellow(warningMessage));
}

export default async function krabs(
request: FastifyRequest,
reply: FastifyReply,
app: any,
handle: any,
config?: Config): Promise<void> {

const { tenants } = config ?? (await getTenantConfig());

const rawHostname = request.hostname;
const pathName = request.url;
const query = request.query;

const hostname = rawHostname.replace(/:\d+$/, '');
const tenant = findTenant(tenants, hostname);

if (!tenant) {
reply.status(500).send({
error: 'Invalid tenant',
});
}

const route = resolveRoutes(tenant?.name!, pathName);

if (route) {
// @ts-ignore
request.tenant = tenant;
app.render(request.raw, reply.raw, route, query);
return;
}

handle(request, reply);
return;
};
4 changes: 3 additions & 1 deletion packages/fastify-krabs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"private": false,
"dependencies": {
"chalk": "^4.1.1",
"express": "^4.17.1"
"express": "^4.17.1",
"fastify": "^3.19.2",
"fastify-plugin": "^3.0.0"
}
}

0 comments on commit f1ca5d4

Please sign in to comment.