Skip to content

Commit

Permalink
fix: fixed github script and updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrilDesch committed Feb 3, 2024
1 parent e40ae6d commit 75b9a63
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@commitlint/config-conventional"]
}
25 changes: 10 additions & 15 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy
name: Linter and Deploy

on:
push:
Expand All @@ -7,10 +7,10 @@ on:
- production

env:
DOCKER_PROJECT_NAME_DEV: dev-REPLACE-PROJECT-NAME
PROJECT_PATH_DEV: ${{ secrets.DEVELOPMENT_DOCKER_IMAGES_PATH }}/dev-REPLACE-PROJECT-NAME
DOCKER_PROJECT_NAME_PROD: REPLACE-PROJECT-NAME
PROJECT_PATH_PROD: ${{ secrets.PRODUCTION_DOCKER_IMAGES_PATH }}/REPLACE-PROJECT-NAME
DOCKER_PROJECT_NAME_DEV: dev-fastify
PROJECT_PATH_DEV: ${{ secrets.DEVELOPMENT_DOCKER_IMAGES_PATH }}/dev-fastify
DOCKER_PROJECT_NAME_PROD: fastify
PROJECT_PATH_PROD: ${{ secrets.PRODUCTION_DOCKER_IMAGES_PATH }}/fastify

jobs:
# Test with eslint and prettier
Expand Down Expand Up @@ -67,12 +67,6 @@ jobs:
echo "APP_BACKEND_PUBLIC_URL=${{ secrets.PRODUCTION_APP_BACKEND_PUBLIC_URL }}" >> .env
echo "APP_FRONTEND_PUBLIC_URL=${{ secrets.PRODUCTION_APP_FRONTEND_PUBLIC_URL }}" >> .env
- name: Use Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: "16.x"
cache: "yarn"

# DEVELOPMENT
- name: Copy repository contents via scp DEVELOPMENT
if: ${{ steps.extract_branch.outputs.branch == 'DEVELOPMENT' }}
Expand Down Expand Up @@ -109,7 +103,7 @@ jobs:
PORT: ${{ secrets.PRODUCTION_PORT }}
KEY: ${{ secrets.PRODUCTION_SSHKEY }}
source: "."
target: "/home/debian/toptenpro-back-prod"
target: ${{ env.PROJECT_PATH_PROD }}

- name: Build and start docker container PRODUCTION
if: ${{ steps.extract_branch.outputs.branch == 'PRODUCTION' }}
Expand All @@ -119,7 +113,8 @@ jobs:
USERNAME: ${{ secrets.PRODUCTION_USERNAME }}
PORT: ${{ secrets.PRODUCTION_PORT }}
KEY: ${{ secrets.PRODUCTION_SSHKEY }}
envs: PROJECT_PATH_PROD, DOCKER_PROJECT_NAME_PROD
script: |
docker ps -q --filter "name=toptenpro-back-prod" | grep -q . && docker stop toptenpro-back-prod && docker container rm toptenpro-back-prod
docker build -t toptenpro-back-prod /home/debian/toptenpro-back-prod/
docker run -d --network reverse-proxy --name toptenpro-back-prod --ip 172.18.0.8 -t toptenpro-back-prod
docker ps -q --filter "name=$DOCKER_PROJECT_NAME_PROD" | grep -q . && docker stop $DOCKER_PROJECT_NAME_PROD && docker container rm $DOCKER_PROJECT_NAME_PROD
docker build -t $DOCKER_PROJECT_NAME_PROD $PROJECT_PATH_PROD
docker run -d --network reverse-proxy --name $DOCKER_PROJECT_NAME_PROD --ip ${{ secrets.PRODUCTION_DOCKER_LOCAL_IP }} -t $DOCKER_PROJECT_NAME_PROD
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
node_modules
*.log
public/*
storage/*
storage/logs
*.xml
www
.env
Expand Down
5 changes: 0 additions & 5 deletions .prettierrc.cjs

This file was deleted.

5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "all",
"tabWidth": 2,
"semi": true
}
113 changes: 85 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
# fastify-typescript-prisma-boilerplate

![Typescript](https://img.shields.io/badge/TypeScript-5.3-blue)
![NodeJS](https://img.shields.io/badge/NodeJS>=-16-darkgreen)
![License](https://img.shields.io/badge/License-APLv2-magenta)
![](https://github.com/Cyril-Deschamps/fastify-typescript-prisma-boilerplate/actions/workflows/deploy.yml/badge.svg)

👩🏻‍💻 Developer Ready: A comprehensive template. Followed strict and REST guidelines

💼 Ready for production: built-in [GitHub Actions][gh-actions] script to check linter and deploy. Dockerfile already configurated [(see server-documentation.md)](./server-documentation.md)

🎁 Free : available under the APLv2 license.
<div align="center">
<h1>fastify-typescript-prisma-boilerplate</h1>
<img alt="Typescript" src="https://img.shields.io/badge/TypeScript-5.3-blue" />
<img alt="NodeJS" src="https://img.shields.io/badge/NodeJS%3E%3D-16-darkgreen" />
<img alt="License" src="https://img.shields.io/badge/License-APLv2-magenta" />
<a href="https://github.com/CyrilDesch/fastify-typescript-prisma-boilerplate/actions/workflows/deploy.yml">
<img alt="CI/CD status" src="https://github.com/CyrilDesch/fastify-typescript-prisma-boilerplate/actions/workflows/deploy.yml/badge.svg" />
</a>
</div>

<br />

<div align="center">
<strong><sup>Project Highlights</sup></strong>
<br />
<span>• Request validation with schema <a href="https://github.com/sinclairzx81/typebox">Typebox</a></span>
<br />
<span>• <a href="https://www.prisma.io">Prisma</a> as ORM</span>
<br />
<span>• Efficient error handling and logger</span>
<br />
<span>• Routes guard with middleware equivalent</span>
</div>

<br />
<hr />
<br />

<div align="center">
<sub>👩🏻‍💻 <strong>Developer Ready:</strong> </sub>
<p>A comprehensive template. Followed <strong>strict</strong> and <strong>REST guidelines</strong></p>
<sub>💼 <strong>Ready for production:</strong></sub>
<p>Built-in <a href="https://github.com/features/actions">GitHub Actions</a> script to check <strong>linter</strong> and <strong>deploy</strong>. Dockerfile already configurated. See below how to use secrets.</p>
<sub>🎁 <strong>Opensource:</strong></sub>
<p>Available under the APLv2 license.</p>
</div>

<br />
<hr />
<br />

## Features

Expand All @@ -22,7 +50,7 @@
- **Format :** [Prettier](https://prettier.io/) to enforce consistent code style
- **Linter :** [ESLint](https://eslint.org/) with good practices of clean code
- **GIT Helper :** Husky + Commitlint + Commitizen
- **CI/CD :** [GitHub Actions](https://github.com/features/actions)
- **CI/CD :** [GitHub Actions](https://github.com/features/actions) + Docker

## Getting started

Expand Down Expand Up @@ -53,21 +81,42 @@ yarn install

Secrets are stored in github secrets. For a local environment, you can use the `.env` file to store the secret (see .env.example).

### General secret :

- APP_SECRET : The secret used for the cookies (32 chars recommended)
- APP_CORS_ORIGIN : List of the CORS in the shape `http://localhost,http://cyrildeschamps.fr`

### For each environment (DEVELOPMENT, PRODUCTION):

- (DEVELOPMENT | STAGING | PRODUCTION)\_APP_BACKEND_PUBLIC_URL : HTTP API address (example: https://cyrildeschamps.fr)
- (DEVELOPMENT | STAGING | PRODUCTION)\_APP_FRONTEND_PUBLIC_URL : Web application address (example: https://toptenpro.fr)
- (DEVELOPMENT | STAGING | PRODUCTION)\_APP_DATABASE_URL : Database URL (e.g. `mysql://user:password@hostname:ip/dbname?schema=public`)
- (DEVELOPMENT | STAGING | PRODUCTION)\_HOST : Deployment server IP
- (DEVELOPMENT | STAGING | PRODUCTION)\_PORT : Deployment server SSH port (example: 22)
- (DEVELOPMENT | STAGING | PRODUCTION)\_SSHKEY : SSH key for connecting to the server (see server's .ssh/id\_... file)
- (DEVELOPMENT | STAGING | PRODUCTION)\_USERNAME : Username for connecting to the server (example: debian)
- (DEVELOPMENT | STAGING | PRODUCTION)\_DOCKER_IMAGES_PATH : Name of the folder where the project is stored on the server (example: /home/debian/docker-container-images)
### General secret

| Variable | Description | Exemple |
| ----------------- | ------------------------------------------------------ | -------------------------------------------- |
| `APP_SECRET` | The secret used for the cookies (32 chars recommended) | `U£,rGtD~Fm;1TY9!Zmpz&R7q&0Sdz@2H` |
| `APP_CORS_ORIGIN` | List of the CORS in the shape | `http://localhost,https://cyrildeschamps.fr` |

### For each environment

#### Development

| Variable | Description | Example |
| ------------------------------------- | ------------------------------------------------------------ | -------------------------------------------------------- |
| `DEVELOPMENT_APP_BACKEND_PUBLIC_URL` | HTTP API address | `https://fastify.cyrildeschamps.fr` |
| `DEVELOPMENT_APP_FRONTEND_PUBLIC_URL` | Web application address | `https://fastify.cyrildeschamps.fr` |
| `DEVELOPMENT_APP_DATABASE_URL` | Database URL | `mysql://user:password@hostname:ip/dbname?schema=public` |
| `DEVELOPMENT_HOST` | Deployment server IP | `66.254.114.41` |
| `DEVELOPMENT_PORT` | Deployment server SSH port | `22` |
| `DEVELOPMENT_SSHKEY` | SSH key for connecting to the server | See server's `.ssh/id_...` file |
| `DEVELOPMENT_USERNAME` | Username for connecting to the server | `debian` |
| `DEVELOPMENT_DOCKER_IMAGES_PATH` | Name of the folder where the project is stored on the server | `/home/debian/docker-container-images` |
| `DEVELOPMENT_DOCKER_LOCAL_IP` | Local IP (nginx) of the container | `172.0.0.18` |

#### Production

| Variable | Description | Example |
| ------------------------------------ | ------------------------------------------------------------ | -------------------------------------------------------- |
| `PRODUCTION_APP_BACKEND_PUBLIC_URL` | HTTP API address | `https://cyrildeschamps.fr` |
| `PRODUCTION_APP_FRONTEND_PUBLIC_URL` | Web application address | `https://cyrildeschamps.fr` |
| `PRODUCTION_APP_DATABASE_URL` | Database URL | `mysql://user:password@hostname:ip/dbname?schema=public` |
| `PRODUCTION_HOST` | Deployment server IP | `66.254.114.41` |
| `PRODUCTION_PORT` | Deployment server SSH port | `22` |
| `PRODUCTION_SSHKEY` | SSH key for connecting to the server | See server's `.ssh/id_...` file |
| `PRODUCTION_USERNAME` | Username for connecting to the server | `debian` |
| `PRODUCTION_DOCKER_IMAGES_PATH` | Name of the folder where the project is stored on the server | `/home/debian/docker-container-images` |
| `PRODUCTION_DOCKER_LOCAL_IP` | Local IP (nginx) of the container | `172.0.0.19` |

## Support

Expand All @@ -76,3 +125,11 @@ You can support this project by contributing to it, by posting issues and propos
## License

Licensed under the APLv2. See the [LICENSE](https://github.com/jsynowiec/node-typescript-boilerplate/blob/main/LICENSE) file for details.

## Improvements

- Use github environment to store secrets and maybe reduce the number of secrets
- Add testing with Jest or other
- Auto-tagging with semantic-release
- Manage migration file between tags or environment)
- And, all others improvements are welcome...
4 changes: 0 additions & 4 deletions commitlint.config.cjs

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "REPLACE-PROJECT-NAME",
"name": "fastify",
"version": "0.0.1",
"description": "REPLACE-PROJECT-NAME Node TS BackEnd - Rest API",
"description": "fastify Node TS BackEnd - Rest API",
"private": true,
"dependencies": {
"@fastify/compress": "^7.0.0",
Expand Down
8 changes: 4 additions & 4 deletions server-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- Création d'un network docker "reverse-proxy"
- Création d'un docker Nginx et MariaDB
- Création d'une configuration nginx pour le reverse-proxy et le lien avec la BDD
- Création d'une configuration nginx pour le projet toptenpro (HTTP et HTTPS)
- Création d'une configuration nginx pour le projet (HTTP et HTTPS)
- Auto génération des certificats pour SSL avec Letsencrypt et Certbot
- Création d'un script github action pour le déploiement continu sur le repo github du projet
- Configuration des secrets en fonction du serveur de déploiement [(voir README.md)](./README.md)
Expand All @@ -31,7 +31,7 @@ Les secrets Github sont utilisés pour la connexion SSH au serveur, et peuvent d
- Importer le projet

```bash
git clone https://github.com/Cyril-Deschamps/toptenpro-front.git
git clone https://github.com/CyrilDesch/fastify-typescript-prisma-boilerplate.git
git checkout development

# La seconde commande permet de se placer
Expand All @@ -46,8 +46,8 @@ Les secrets Github sont utilisés pour la connexion SSH au serveur, et peuvent d
```
- Déployer les modifications
- Se rendre à l’adresse :
- FRONT : https://github.com/Cyril-Deschamps/toptenpro-front/compare/production...development?expand=1
- BACK : https://github.com/Cyril-Deschamps/toptenpro-back/compare/production...development?expand=1
- FRONT : https://github.com/CyrilDesch/fastify-typescript-prisma-boilerplate.git/compare/production...development?expand=1
- BACK : https://github.com/CyrilDesch/fastify-typescript-prisma-boilerplate.git/compare/production...development?expand=1
- Créer la pull request
- A ce stade là, vous pouvez vérifier vos modifications; que vous n’envoyer pas d’erreur qui pourrait causer un crash.
- Si tout est bon, merger la pull request.
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { config as dotenvConfig } from "dotenv";
import { join } from "path";
dotenvConfig();

export const project_name = "REPLACE-PROJECT-NAME";
export const project_name = "fastify";

//secret used to encrypt pwd
export const secret = process.env.APP_SECRET!;
Expand Down
Empty file added storage/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"strict": true,
"outDir": "./dist"
},
"include": ["src/**/*.ts", "src/types/fastify.d.ts", ".prettierrc.js"],
"include": ["src/**/*.ts", "src/types/fastify.d.ts"],
"exclude": ["node_modules"],
"ts-node": {
"files": true
Expand Down

0 comments on commit 75b9a63

Please sign in to comment.