Shyam-Chen / Fastify-Starter

:leopard: A boilerplate for server applications with Fastify and Mongo using TypeScript on Vite.

Home Page:https://fastify-starter-12ih.onrender.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fastify Starter

πŸ† A boilerplate for server applications with Fastify and Mongo using TypeScript on Vite.

🌈 View Demo: Live | Windows | macOS | Android | iOS

:octocat: Source Code: Web-side | Native-side | Server-side | Cloud-side

Table of Contents

Getting Started

Prerequisites:

  • Node.js v20
  • PNPM v9
  • Docker v4 (Optional)

Get started with Fastify Starter.

# (optional)
# If you already have the MONGODB_URL and REDIS_URL connections,
# you can directly replace them.

# mongo server
$ docker compose up -d local-mongo

# redis server
$ docker compose up -d local-redis
# install dependencies
$ pnpm install

# dev server (in one terminal)
$ pnpm dev

# (negligible)
# The current demo doesn't make any calls to third-party or internal APIs.
# mock server (in another terminal)
$ pnpm mock

Or use barebones scaffolding for your new Fastify app

$ pnpm dlx degit Shyam-Chen/Barebones-Templates/fastify my-fastify-app

Project Setup

Follow steps to execute this boilerplate.

Install dependencies

$ pnpm install

Compiles and hot-reloads for development

$ pnpm dev

Mock third-party/private APIs during development

$ pnpm mock

Compiles and minifies for production

$ pnpm build

Locally preview the production build

# Before running the `preview` command, make sure to run the following commands.
$ pnpm build

$ pnpm preview

Lints and fixes files

Files: **/*.{js,ts}

$ pnpm lint

Check types

Files: app/src/**/*.ts

$ pnpm check

Runs unit tests

Files: app/src/**/*.test.ts

$ pnpm test

Runs end-to-end tests

Files: e2e/src/**/*.test.ts

# Before running the `e2e` command, make sure to run the following commands.
$ pnpm build
$ pnpm preview

$ pnpm e2e

Key Features

This seed repository provides the following features:

  • ---------- Essentials ----------
  • Fastify - Web Application Framework
  • Routes - File-based Routing
  • MongoDB - Document Database
  • JWT - Authentication
  • PBKDF2 - Hash Passwords
  • OTP - Authenticator
  • Cloudinary - Asset Management
  • I18n - Internationalization and Localization
  • Redis - In-memory Data Structure Store
  • WebSocket - Two-way Interactive Communication Session
  • EventSource - Server-sent Events
  • Mailer - Email Sending
  • MJML - Email Builder
  • Nunjucks - Email Rendering
  • BullMQ - Message Queue
  • ---------- Tools ----------
  • Vite - Bundler
  • TypeScript - JavaScript with Syntax for Types
  • Biome - Formatter and Linter
  • Vitest - Test Runner
  • Playwright - Test Automation
  • ---------- Environments ----------
  • Node.js - JavaScript Runtime Environment
  • PNPM - Package Manager
  • Caddy - Web Server
  • Docker - Containerized Application Development
  • GitHub Actions - Continuous Integration and Delivery
  • Render - Cloud Application Hosting

Tiny examples

Configuration

Control the environment.

Default environments

Set your local environment variables.

// app/vite.config.ts
  define: envify({
    NODE_ENV: process.env.NODE_ENV || 'development',

    HOST: process.env.HOST || '127.0.0.1',
    PORT: process.env.PORT || 3000,

    SITE_URL: process.env.SITE_URL || 'http://127.0.0.1:5173',

    MONGODB_URL: process.env.MONGODB_URL || 'mongodb://root:rootpasswd@127.0.0.1:27017/mydb',
    REDIS_URL: process.env.REDIS_URL || 'redis://127.0.0.1:6379',
    CLOUDINARY_URL: process.env.CLOUDINARY_URL || 'cloudinary://apikey:apisecret@cloudname',
    SMTP_URL: process.env.SMTP_URL || `smtp://${user}:${pass}@smtp.ethereal.email:587`,

    SECRET_KEY: process.env.SECRET_KEY || 'jbmpHPLoaV8N0nEpuLxlpT95FYakMPiu',

    MONGOMS_VERSION: process.env.MONGOMS_VERSION || '7.0.11',
  }),

Continuous integration environments

Add environment secrets to the GitHub Actions workflow.

DEPLOY_HOOK=xxx

Continuous delivery environments

Add environment variables to the Render build.

SITE_URL=xxx
MONGODB_URL=xxx
REDIS_URL=xxx
CLOUDINARY_URL=xxx
SMTP_URL=xxx

Directory Structure

The structure follows the LIFT Guidelines.

.
β”œβ”€β”€ .github/workflows/ci.yaml
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ public
β”‚   β”œβ”€β”€ src
β”‚   β”‚   β”œβ”€β”€ assets
β”‚   β”‚   β”œβ”€β”€ components
β”‚   β”‚   β”œβ”€β”€ composables
β”‚   β”‚   β”œβ”€β”€ jobs
β”‚   β”‚   β”œβ”€β”€ locales
β”‚   β”‚   β”œβ”€β”€ middleware
β”‚   β”‚   β”œβ”€β”€ plugins
β”‚   β”‚   β”œβ”€β”€ routes
β”‚   β”‚   β”œβ”€β”€ templates
β”‚   β”‚   β”œβ”€β”€ utilities
β”‚   β”‚   β”œβ”€β”€ app.ts
β”‚   β”‚   β”œβ”€β”€ main.ts
β”‚   β”‚   β”œβ”€β”€ shims.d.ts
β”‚   β”‚   └── worker.ts
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ tsconfig.json
β”‚   └── vite.config.ts
β”œβ”€β”€ db -> Set up local data for initializing and testing the database
β”‚   β”œβ”€β”€ src
β”‚   β”œβ”€β”€ mongo-init.js
β”‚   └── package.json
β”œβ”€β”€ docs -> Write API documentation with VitePress
β”‚   β”œβ”€β”€ .vitepress
β”‚   β”‚   └── config.ts
β”‚   β”œβ”€β”€ index.md
β”‚   └── package.json
β”œβ”€β”€ e2e -> API End-to-end testing
β”‚   β”œβ”€β”€ src
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ playwright.config.ts
β”‚   └── tsconfig.json
β”œβ”€β”€ mock -> Mocking third-party or private API calls
β”‚   β”œβ”€β”€ src
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ tsconfig.json
β”‚   └── vite.config.ts
β”œβ”€β”€ .dockerignore
β”œβ”€β”€ .editorconfig
β”œβ”€β”€ .gitignore
β”œβ”€β”€ biome.json
β”œβ”€β”€ Caddyfile
β”œβ”€β”€ compose.yaml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ package.json
β”œβ”€β”€ pnpm-lock.yaml
β”œβ”€β”€ pnpm-workspace.yaml
β”œβ”€β”€ README.md
└── render.yaml

About

:leopard: A boilerplate for server applications with Fastify and Mongo using TypeScript on Vite.

https://fastify-starter-12ih.onrender.com


Languages

Language:TypeScript 88.4%Language:HTML 6.7%Language:JavaScript 2.7%Language:Dockerfile 2.2%