Skip to content

Commit

Permalink
Merge pull request #92 from WildCodeSchool/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rocambille authored Oct 16, 2023
2 parents 76fb115 + f70455f commit f54c43b
Show file tree
Hide file tree
Showing 43 changed files with 18,357 additions and 5,732 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
steps:
- name: Check out Git repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Node.js
uses: actions/setup-node@v3
Expand All @@ -24,8 +26,11 @@ jobs:
- name: Install pnpm
run: npm -g install pnpm

- name: Install Node.js dependencies
run: cd ./frontend && pnpm install && cd ../backend && pnpm install
- name: Set package.json config.cli
run: sed -i 's/.*"cli":.*/"cli":"pnpm"/' package.json

- name: Install package dependencies
run: pnpm install

- name: Run linters
run: cd ./frontend && pnpm run lint && cd ../backend && pnpm run lint
run: npx lint-staged --diff="origin/${GITHUB_BASE_REF}...origin/${GITHUB_HEAD_REF}"
48 changes: 48 additions & 0 deletions .github/workflows/deploy-traefik.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: CD-traefik

on:
push:
branches:
- main
pull_request:
branches:
- main
types: [closed]

jobs:
lint:
uses: ./.github/workflows/_lint.yml
permissions:
checks: write
contents: write

check-secrets:
needs: lint
if: (github.event.pull_request.merged || github.event_name == 'push')
runs-on: ubuntu-latest
outputs:
caprover-server: ${{ steps.caprover-server.outputs.defined }}
steps:
- name: Check for secrets availability
id: defined
# perform secret check & put boolean result as an output
shell: bash
run: |
if [ "${{ secrets.SSH_USER }}" != '' && "${{ secrets.SSH_HOST }}" != '' && "${{ secrets.SSH_PASSWORD }}" != '' ]; then
echo "defined=true" >> $GITHUB_OUTPUT;
else
echo "defined=false" >> $GITHUB_OUTPUT;
fi
deploy:
needs: check-secrets
if: needs.check-secrets.outputs.defined == 'true'
runs-on: ubuntu-latest
steps:
- name: Deploy to VPS
uses: appleboy/ssh-action@master
with:
username: ${{ secrets.SSH_USER }}
host: ${{ secrets.SSH_HOST }}
password: ${{ secrets.SSH_PASSWORD }}
script: cd && cd traefik/deploy && bash ./js-project.sh ${{ github.actor }} ${{ github.event.repository.name }} ${{ vars.PROJECT_NAME }} '${{ toJSON(vars) }}'
4 changes: 0 additions & 4 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: dev

on:
push:
branches:
- develop
- dev
pull_request:
branches:
- develop
Expand Down
65 changes: 0 additions & 65 deletions .github/workflows/main.yml

This file was deleted.

16 changes: 16 additions & 0 deletions .github/workflows/remove.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Remove-from-Traefik

on:
workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Remove from VPS
uses: appleboy/ssh-action@master
with:
username: ${{ secrets.SSH_USER }}
host: ${{ secrets.SSH_HOST }}
password: ${{ secrets.SSH_PASSWORD }}
script: cd && cd traefik/deploy && bash ./remove-project.sh ${{ github.event.repository.name }}
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
node_modules
.yarn
node_modules
.DS_Store
.idea
.vscode
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ then

echo "$fileList\n"

echo "use 'git restore ...' to fix the problem"
echo "use 'git restore --staged ...' to fix the problem"
echo "(and ask your instructor why you should not change these files)\n"

exit 1
Expand Down
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing right now. [Open an issue](https://github.com/WildCodeSchool/js-template-fullstack/issues) if you find something.

## [4.1.0] - 2023-10-16

### Fixed

- Fixed [issue #11](https://github.com/WildCodeSchool/js-template-fullstack/issues/11): installed and configured `lint-staged`.

### Added

- Installed `supertest` in backend, and added smoke testing samples in a `backend/tests/items/routes.spec.js` file.

- Added unit testing samples in a `backend/tests/items/manager.spec.js` file.

- Added a section in `backend/src/app.js` for error handling. Reminder: [an error-handling middleware _must_ have 4 parameters](https://stackoverflow.com/a/51826777/6612932)

- Added support for network-wide testing (ie: mobile testing) using `--host` option of Vite. Thanks to [Loïc Brassart](https://github.com/LoicBrassart).

### Changed

- Isolated `database` client from `backend/src/models/AbstractManager.js` into a separate file `backend/database/client.js`, so it is accessible to test suite in a consistent way.

- **Breaking change:** Refactored deployment using Traefik. Thanks to [Jean-François Morin](https://github.com/jfm-wcs) and [Anthony Gorski](https://github.com/GorskiAnthony).

## [4.0.1] - 2023-08-09

### Fixed
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dockerfile backend
FROM node:16.14

WORKDIR /usr/src/app

COPY ./ .

RUN npm install
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ git config --global core.autocrlf false
- `dev` : Starts both servers (frontend + backend) in one terminal
- `dev-front` : Starts the React frontend server
- `dev-back` : Starts the Express backend server
- `lint` : Runs validation tools, and refuses unclean code (will be executed on every _commit_)
- `fix` : Fixes linter errors (run it if `lint` growls on your code !)
- `lint` : Runs validation tools (will be executed on every _commit_, and refuse unclean code)

## FAQ

Expand All @@ -46,13 +45,30 @@ git config --global core.autocrlf false
- _ESLint_ : "Quality of code" tool, ensures chosen rules will be enforced
- _Prettier_ : "Quality of code" tool as well, focuses on the styleguide
- _ Airbnb Standard_ : One of the most known "standards", even though it's not officially linked to ES/JS
- _Nodemon_ : Allows to restart the server everytime a .js file is udated

### Deployment
### Deployment with Caprover

For deployment, you have to go to `secrets` → app `actions` on the github repo to insert via `New repository secret` :

- CAPROVER_BACK_APPNAME : name app on caprover
- CAPROVER_FRONT_APPNAME : name app on caprover
- CAPROVER_PASSWORD : password caprover
- CAPROVER_SERVER : link of domain

### Deployment with Traefik

>⚠️ Prerequisites : You must have installed and configured Traefik on your VPS beforehand.
> https://github.com/WildCodeSchool/vps-traefik-starter-kit/
For deployment, you have to go to `secrets` → app `actions` on the github repo to insert via `New repository secret` :
- SSH_HOST : IP address of your VPS
- SSH_USER : SSH login to your VPS
- SSH_PASSWORD : SSH connection password to your VPS

And a public variable from the tab `/settings/variables/actions`
- PROJECT_NAME : the name of the project used to create the subdomain for frontend.
The backend subdomain will be automatically created with the suffix -backend.
The global variable VITE_BACKEND_URL will be automatically created and pre-filled on the basis of this information.

Use this same tab to add the other environment variables required for the project if any.

3 changes: 2 additions & 1 deletion backend/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"env": {
"browser": true,
"es2021": true
"es2021": true,
"jest": true
},
"extends": ["airbnb-base", "plugin:prettier/recommended"],
"parserOptions": {
Expand Down
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
coverage
.env
19 changes: 0 additions & 19 deletions backend/Dockerfile

This file was deleted.

36 changes: 36 additions & 0 deletions backend/database/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Get variables from .env file for database connection
const { DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME } = process.env;

// Create a connection pool to the database
const mysql = require("mysql2/promise");

const client = mysql.createPool({
host: DB_HOST,
port: DB_PORT,
user: DB_USER,
password: DB_PASSWORD,
database: DB_NAME,
});

// Try to get a connection to the database
client
.getConnection()
.then((connection) => {
console.info(`Using database ${DB_NAME}`);

connection.release();
})
.catch((error) => {
console.warn(
"Warning:",
"Failed to establish a database connection.",
"Please check your database credentials in the .env file if you need a database access."
);
console.error("Error message:", error.message);
});

// Store database name into client for further uses
client.databaseName = DB_NAME;

// Ready to export
module.exports = client;
Loading

0 comments on commit f54c43b

Please sign in to comment.