···11+# kita
22+33+## Installing the project
44+55+Run the following command to install the project:
66+77+```bash
88+npm install
99+```
1010+1111+## Running in development mode
1212+1313+Run the following command to start the server in development mode:
1414+1515+```bash
1616+npm run dev
1717+```
1818+1919+You can now open your browser and navigate to [`http://localhost:1227`](http://localhost:1227).
2020+2121+## Building for production
2222+2323+Run the following command to build the project:
2424+2525+```bash
2626+npm run build
2727+```
2828+2929+Run the following command to start the server in production mode:
3030+3131+```bash
3232+npm start
3333+```
3434+3535+You can now open your browser and navigate to [`http://localhost:1227`](http://localhost:1227).
3636+3737+## Running tests
3838+3939+You can run the tests using the following command:
4040+4141+```bash
4242+npm test
4343+```
4444+4545+## Environment variables
4646+4747+Environment variables are loaded from a [`.env`](./.env) file in the root of the project. These are the available
4848+variables:
4949+5050+```bash
5151+# Port and host for the server
5252+PORT=1227
5353+HOST=0.0.0.0
5454+```
5555+5656+## Linting and Formatting
5757+5858+You can run the following commands to lint and format your code:
5959+6060+```bash
6161+# Formats your code
6262+npm run format
6363+6464+# Lints your code
6565+npm run lint
6666+6767+# Lints and fixes your code
6868+npm run lint:fix
6969+7070+# Lints your code in CI mode
7171+npm run lint:ci
7272+```
···11+import { ajvFilePlugin } from '@fastify/multipart';
22+import fastify from 'fastify';
33+import { isMainThread } from 'node:worker_threads';
44+import backendPlugin from './plugin';
55+66+// Ensures this file is not executed in test context
77+if (process.env.NODE_TEST_CONTEXT) {
88+ throw new Error('This file should not be executed in test context');
99+}
1010+1111+// Ensures this file is not executed in worker context
1212+if (!isMainThread) {
1313+ throw new Error('This file should not be executed in worker context');
1414+}
1515+1616+// Ensures PORT are set
1717+if (!process.env.PORT) {
1818+ throw new Error('PORT must be set');
1919+}
2020+2121+fastify({
2222+ logger: { transport: { target: 'pino-pretty' } },
2323+ ajv: { plugins: [ajvFilePlugin] }
2424+})
2525+ // Registers our backend
2626+ .register(backendPlugin)
2727+ // Starts the server
2828+ .listen({
2929+ port: +process.env.PORT,
3030+ host: process.env.HOST || ''
3131+ });
+43
kita/src/plugin.ts
···11+import './prelude';
22+33+import fastifyHelmet from '@fastify/helmet';
44+import fastifyUnderPressure from '@fastify/under-pressure';
55+import { Kita } from '@kitajs/runtime';
66+import closeWithGrace from 'close-with-grace';
77+import fp from 'fastify-plugin';
88+99+export default fp(async (app) => {
1010+ // Registers the generated kita plugin
1111+ app.register(Kita);
1212+1313+ // Measures process load with automatic handling of "Service Unavailable"
1414+ app.register(fastifyUnderPressure, {
1515+ maxEventLoopDelay: 1000,
1616+ maxHeapUsedBytes: 1000000000,
1717+ maxRssBytes: 1000000000,
1818+ maxEventLoopUtilization: 0.98
1919+ });
2020+2121+ // Important security headers for Fastify
2222+ app.register(fastifyHelmet, {
2323+ global: true
2424+ });
2525+2626+ // Add your custom stuff here
2727+ // app.register(myPlugin)
2828+ // ...
2929+3030+ // Delay is the number of milliseconds for the graceful close to finish
3131+ const closeListeners = closeWithGrace({ delay: 500 }, async ({ err }) => {
3232+ if (err) {
3333+ app.log.error(err);
3434+ }
3535+3636+ await app.close();
3737+ });
3838+3939+ // Cancelling the close listeners
4040+ app.addHook('onClose', async () => {
4141+ closeListeners.uninstall();
4242+ });
4343+});
+2
kita/src/prelude.ts
···11+// This tells kita where to find the root of your project
22+globalThis.KITA_PROJECT_ROOT ??= __dirname;
+13
kita/src/routes/index.ts
···11+import type { Query } from '@kitajs/runtime';
22+33+/**
44+ * @tag Hello
55+ * @operationId getHello
66+ * @summary Get a hello message with date
77+ */
88+export function get(name: Query = 'World') {
99+ return {
1010+ name,
1111+ message: `Hello ${name}!`
1212+ };
1313+}