This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added basic fastify boilerplate
- Loading branch information
1 parent
651e2d9
commit f1ca5d4
Showing
5 changed files
with
294 additions
and
5 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
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
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 |
---|---|---|
@@ -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; | ||
}; |
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
Oops, something went wrong.