Skip to content

Commit 0e6dff1

Browse files
jean-micheletclimba03003Fdawgs
authored
Create basic architecture (#1)
* chore: create basic architecture * fix: typo in comment * chore: add server.js to show how to not depend on cli * fix: should not try to parser string as json * fix: typo comment * chore: add helmet plugin * chore: add cors plugin * chore: add env plugin * refactor: reorganize plugin structure * chore: add swagger * fix: typo comment * fix: comment typo * fix: swagger tag Example in /example * refactor: simplify plugin structure folder * refactor: put swagger registration in /plugins * refactor: add pino-pretty to server.js * chore: add error handlers * refactor: unecessary await setNotFoundHandler * test: root not found handler * chore: reuse existing workflow * test on v4 * chore: use ci v4 * fix: run lint fix * fix: cant use import.meta.dirname for now * update readme getting-started * fix: node imports * chore: use glob to launch tests * fix: invalid workflow file * chore: use glob to run test suite * chore: use typescript * chore: use typescript-eslint with prettier * fix: add blank line to .prettierignore * chore: add under-pressure plugin * docs: add links to under-pressure plugin * refactor: custom plugins should have a name * chore: setup db * chore: remove phpmyadmin from docker composition * fix: typo * refactor: create src folder and run test folder with tap * chore: remove glob * test: error-handler still todo * fix: remove @types from tsconfig include * fix: compiler config * chore: get rid of NODE_ENV * fix: remove commented code * refactor: move under-pressure options configuration inside plugin definition * refactor: leverage import.meta.dirname * test: POST /example and error-handler * chore: pretty-logger only if the program is running in an interactive terminal * fix: fp must be used to override default error handler * chore: update CI * support only ubuntu * dont wait for mysql to be ready * lint * refactor: uninstall global listeners is not needed * refactor: use neostandard * refactor: add external plugins in their own folder * docs: highlights the concept of modular monolyth * chore: dont wait for MySQL to be ready in ci * chore: support only ubuntu-latest os * refactor: leverage autoConfig * fix: add blank line * chore: raise maxRssBytes to 1GB * chore: dhould not log during tests * refactor: launch server with await instead of callback * docs: add .env.example file * fix: remove body-limit test * refactor: leverage autoConfig callback feature * fix: add blank line * chore: remove build command before starting server related scripts * feat: add basic jwt authentication * refactor: return username in /api response * chore: use of postgrator for db migrations * chore: add JWT_SECRET generation step for CI env * feat: create repository plugin to simplify queries * fix: remove unused param con of doMigration * refactor: no need for seed database at the beggining of a test * test: ignore seed:db catch blocks * fix: typo * refactor: move type declarations outside plugin declaration * chore: generate dummy .env during CI for scripts using -env-file=.env flag * chore: can't --ignore-scripts if using bcrypt during CI * refactor: use IAuth interface tp type user on request * fix: add blank line to migration files * fix: remove useless comments * refactor: declare type decoration of fastify instance in plugin files * fix: add blank line * feat: create scrypt plugin * feat: create scrypt plugin * fix: add blank line * fix: revert timingSafeEqual in error branch * Update src/plugins/custom/repository.ts Co-authored-by: KaKa <[email protected]> Signed-off-by: Jean <[email protected]> * refactor: remove I prefix from ts interface * fix: identifier typo * fix: remove useless comment * fix: run migration before test * test: ensure access-control-allow-methods contains the expected methods * docs: add @link tag to urls * Update src/app.ts Co-authored-by: Frazer Smith <[email protected]> Signed-off-by: Jean <[email protected]> * fix: test name --------- Signed-off-by: Jean <[email protected]> Co-authored-by: KaKa <[email protected]> Co-authored-by: Frazer Smith <[email protected]>
1 parent e049e70 commit 0e6dff1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1488
-1
lines changed

.env.example

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Must always set to production
2+
# @see {@link https://www.youtube.com/watch?v=HMM7GJC5E2o}
3+
NODE_ENV=production
4+
5+
# Database
6+
MYSQL_HOST=localhost
7+
MYSQL_PORT=3306
8+
MYSQL_DATABASE=test_db
9+
MYSQL_USER=test_user
10+
MYSQL_PASSWORD=test_password
11+
12+
# Server
13+
FASTIFY_CLOSE_GRACE_DELAY=1000
14+
LOG_LEVEL=info
15+
16+
# Security
17+
JWT_SECRET=

.github/dependabot.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: "monthly"
7+
open-pull-requests-limit: 10
8+
9+
- package-ecosystem: "npm"
10+
directory: "/"
11+
schedule:
12+
interval: "weekly"
13+
open-pull-requests-limit: 10

.github/stale.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Number of days of inactivity before an issue becomes stale
2+
daysUntilStale: 15
3+
# Number of days of inactivity before a stale issue is closed
4+
daysUntilClose: 7
5+
# Issues with these labels will never be considered stale
6+
exemptLabels:
7+
- "discussion"
8+
- "feature request"
9+
- "bug"
10+
- "help wanted"
11+
- "plugin suggestion"
12+
- "good first issue"
13+
# Label to use when marking an issue as stale
14+
staleLabel: stale
15+
# Comment to post when marking an issue as stale. Set to `false` to disable
16+
markComment: >
17+
This issue has been automatically marked as stale because it has not had
18+
recent activity. It will be closed if no further activity occurs. Thank you
19+
for your contributions.
20+
# Comment to post when closing a stale issue. Set to `false` to disable
21+
closeComment: false

.github/workflows/ci.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- next
8+
- "v*"
9+
paths-ignore:
10+
- "docs/**"
11+
- "*.md"
12+
pull_request:
13+
paths-ignore:
14+
- "docs/**"
15+
- "*.md"
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
strategy:
21+
matrix:
22+
node-version: [22]
23+
24+
services:
25+
mysql:
26+
image: mysql:8.4
27+
ports:
28+
- 3306:3306
29+
env:
30+
MYSQL_ROOT_PASSWORD: root_password
31+
MYSQL_DATABASE: test_db
32+
MYSQL_USER: test_user
33+
MYSQL_PASSWORD: test_password
34+
options: >-
35+
--health-cmd="mysqladmin ping -u$MYSQL_USER -p$MYSQL_PASSWORD"
36+
--health-interval=10s
37+
--health-timeout=5s
38+
--health-retries=3
39+
40+
steps:
41+
- uses: actions/checkout@v4
42+
- name: Use Node.js ${{ matrix.node-version }}
43+
uses: actions/setup-node@v4
44+
with:
45+
node-version: ${{ matrix.node-version }}
46+
47+
- name: Install dependencies
48+
run: npm i
49+
50+
- name: Lint Code
51+
run: npm run lint
52+
53+
- name: Generate JWT Secret
54+
id: gen-jwt
55+
run: |
56+
JWT_SECRET=$(openssl rand -hex 32)
57+
echo "JWT_SECRET=$JWT_SECRET" >> $GITHUB_ENV
58+
59+
- name: Generate dummy .env for scripts using -env-file=.env flag
60+
run: touch .env
61+
62+
- name: Test
63+
env:
64+
MYSQL_HOST: localhost
65+
MYSQL_PORT: 3306
66+
MYSQL_DATABASE: test_db
67+
MYSQL_USER: test_user
68+
MYSQL_PASSWORD: test_password
69+
# JWT_SECRET is dynamically generated and loaded from the environment
70+
run: npm run db:migrate && npm run test

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ coverage
2626
# nyc test coverage
2727
.nyc_output
2828

29+
# tap test coverage
30+
.tap
31+
2932
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
3033
.grunt
3134

@@ -128,3 +131,9 @@ dist
128131
.yarn/build-state.yml
129132
.yarn/install-state.gz
130133
.pnp.*
134+
135+
# lock files
136+
bun.lockb
137+
package-lock.json
138+
pnpm-lock.yaml
139+
yarn.lock

@types/fastify/fastify.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Auth } from "../../src/schemas/auth.ts";
2+
3+
declare module "fastify" {
4+
export interface FastifyRequest {
5+
user: Auth
6+
}
7+
}

@types/node/environment.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
declare global {
2+
namespace NodeJS {
3+
interface ProcessEnv {
4+
PORT: number;
5+
LOG_LEVEL: string;
6+
FASTIFY_CLOSE_GRACE_DELAY: number;
7+
MYSQL_HOST: string
8+
MYSQL_PORT: number
9+
MYSQL_DATABASE: string
10+
MYSQL_USER: string
11+
MYSQL_PASSWORD: string
12+
}
13+
}
14+
}
15+
16+
export {};

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
1-
# demo
1+
# Fastify Official Demo
2+
3+
![CI](https://github.com/fastify/demo/workflows/CI/badge.svg)
4+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
5+
6+
> :warning: **Please note:** This repository is still under active development.
7+
8+
The aim of this repository is to provide a concrete example of a Fastify application using what are considered best practices by the Fastify community.
9+
10+
**Prerequisites:** You need to have Node.js version 22 or higher installed.
11+
12+
## Getting started
13+
14+
Install the dependencies:
15+
16+
```bash
17+
npm install
18+
```
19+
20+
## Available Scripts
21+
22+
In the project directory, you can run:
23+
24+
### `npm run dev`
25+
26+
To start the app in dev mode.\
27+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
28+
29+
### `npm start`
30+
31+
For production mode
32+
33+
### `npm run test`
34+
35+
Run the test cases.
36+
37+
## Learn More
38+
39+
To learn Fastify, check out the [Fastify documentation](https://fastify.dev/docs/latest/).

docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
db:
3+
image: mysql:8.4
4+
environment:
5+
MYSQL_DATABASE: ${MYSQL_DATABASE}
6+
MYSQL_USER: ${MYSQL_USER}
7+
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
8+
ports:
9+
- 3306:3306
10+
volumes:
11+
- db_data:/var/lib/mysql
12+
13+
volumes:
14+
db_data:

eslint.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
import neo from 'neostandard'
4+
5+
export default [
6+
...neo({
7+
ts: true
8+
}),
9+
{
10+
rules: {
11+
'@stylistic/comma-dangle': ['error', {
12+
arrays: 'never',
13+
objects: 'never',
14+
imports: 'never',
15+
exports: 'never',
16+
functions: 'never'
17+
}]
18+
}
19+
}
20+
]

0 commit comments

Comments
 (0)