Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): add .env loading and pino-pretty in development #12

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ node_modules

# Local env files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
.env.*
!.env.sample

# Testing
coverage
Expand Down
1 change: 1 addition & 0 deletions apps/backend/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NODE_ENV=development
10 changes: 6 additions & 4 deletions apps/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ npm run test

## Features

- ESLint, Typescript and test setup, see the [./package.json](./package.json) scripts.
- ESLint, Typescript and test setup with [node:test](https://nodejs.org/api/test.html) and
[node:assert/strict](https://nodejs.org/api/assert.html#strict-assertion-mode).
- `.env` & `.env.local` support via [dotenv](https://npm.im/dotenv).
- Pretty logs in development with [pino-pretty](https://npm.im/pino-pretty).
- Graceful shutdown on exit signals via
[close-with-grace](https://npm.im/close-with-grace) in [./server.ts](./server.ts).
[close-with-grace](https://npm.im/close-with-grace).
- Basic multipart form handling via
[@fastify/multipart](https://npm.im/@fastify/multipart) in
[./plugins/base.ts](./plugins/base.ts).
[@fastify/multipart](https://npm.im/@fastify/multipart).
14 changes: 13 additions & 1 deletion apps/backend/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ import fastify, { FastifyRequest } from "fastify";
import { basePlugin } from "./plugins/base.js";

export async function buildApp() {
const loggerConfig =
process.env.NODE_ENV === "development" ?
{
transport: {
target: "pino-pretty",
options: {
ignore: "pid,hostname",
},
},
}
: true;

const app = fastify({
logger: true,
logger: loggerConfig,
});

app.register(basePlugin);
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
"build": "tsc -p ./tsconfig.json",
"lint": "eslint . --fix --cache --cache-strategy content --cache-location .cache/eslint/ --color",
"lint:ci": "eslint .",
"pretest": "npm run build",
"test": "node --test"
},
"dependencies": {
"close-with-grace": "^1.2.0",
"dotenv": "^16.4.4",
"fastify": "^4.26.0",
"fastify-plugin": "^4.5.1",
"@fastify/multipart": "^8.1.0"
Expand All @@ -19,6 +21,7 @@
"@types/node": "^20.11.17",
"@lightbase/tsconfig": "0.1.0",
"@lightbase/eslint-config": "0.1.0",
"pino-pretty": "^10.3.1",
"typescript": "5.3.3"
},
"engines": {
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import closeWithGrace from "close-with-grace";
import { buildApp } from "./app.js";
import dotenv from "dotenv";

dotenv.config({ path: [".env.local", ".env"] });

const app = await buildApp();

Expand Down
Loading