refactor: improve env loading
chore: add Dockerfile
This commit is contained in:
parent
162e256424
commit
a19e1415f1
9 changed files with 148 additions and 4 deletions
19
Dockerfile
19
Dockerfile
|
@ -1 +1,18 @@
|
||||||
# todo: Write Dockerfile
|
FROM node:21
|
||||||
|
|
||||||
|
# Install netcat
|
||||||
|
RUN apt-get update && apt-get install -y netcat
|
||||||
|
|
||||||
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
|
# Copy package.json and package-lock.json to the working directory
|
||||||
|
COPY package*.json ./
|
||||||
|
|
||||||
|
RUN npm install pnpm
|
||||||
|
RUN pnpm install
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
EXPOSE 4300
|
||||||
|
|
||||||
|
ENTRYPOINT ["./entrypoint.sh"]
|
||||||
|
|
11
app.js
11
app.js
|
@ -14,10 +14,14 @@
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const bodyParser = require("body-parser");
|
const bodyParser = require("body-parser");
|
||||||
|
const cookieParser = require("cookie-parser");
|
||||||
const mysql = require("mysql2");
|
const mysql = require("mysql2");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const dotenv = require("dotenv");
|
|
||||||
dotenv.config();
|
if (!process.env.DOCKER) {
|
||||||
|
const dotenv = require("dotenv");
|
||||||
|
dotenv.config();
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------>
|
//------------------------------------------------>
|
||||||
// Express initialization & configuration
|
// Express initialization & configuration
|
||||||
|
@ -34,8 +38,9 @@ app.set("view engine", "ejs");
|
||||||
// Middlewares
|
// Middlewares
|
||||||
//------------------------------------------------>
|
//------------------------------------------------>
|
||||||
|
|
||||||
// Use body-parser to get req.body
|
// Use body-parser and cookie-parser
|
||||||
app.use(bodyParser.urlencoded({ extended: false }));
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
|
app.use(cookieParser());
|
||||||
|
|
||||||
//------------------------------------------------>
|
//------------------------------------------------>
|
||||||
// MySQL connection
|
// MySQL connection
|
||||||
|
|
9
config/example.env
Normal file
9
config/example.env
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# Database
|
||||||
|
DB_HOST="exampleHost"
|
||||||
|
DB_PORT=3306
|
||||||
|
DB_NAME="exampleName"
|
||||||
|
DB_USER="exampleUser"
|
||||||
|
DB_PASSWORD="examplePassword"
|
||||||
|
|
||||||
|
# Express
|
||||||
|
PORT=4300
|
37
docker-compose.yml
Normal file
37
docker-compose.yml
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
# MySQL service
|
||||||
|
db:
|
||||||
|
image: mysql:8
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
MYSQL_DATABASE: jet
|
||||||
|
MYSQL_USER: jet
|
||||||
|
MYSQL_PASSWORD: ghiuwe§825429h§$%ff
|
||||||
|
MYSQL_ROOT_PASSWORD: hgruwhgvuiwrghiuwe§825429h§$%ff
|
||||||
|
ports:
|
||||||
|
- "3306:3306"
|
||||||
|
volumes:
|
||||||
|
- mysql:/var/lib/mysql
|
||||||
|
|
||||||
|
# Express app service
|
||||||
|
app:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
ports:
|
||||||
|
- "4300:4300"
|
||||||
|
environment:
|
||||||
|
DB_HOST: db
|
||||||
|
DB_PORT: 3306
|
||||||
|
DB_USER: jet
|
||||||
|
DB_PASSWORD: ghiuwe§825429h§$%ff
|
||||||
|
DB_NAME: jet
|
||||||
|
DOCKER: true
|
||||||
|
PORT: 4300
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
mysql:
|
17
entrypoint.sh
Executable file
17
entrypoint.sh
Executable file
|
@ -0,0 +1,17 @@
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Function to check if the database is healthy
|
||||||
|
check_database() {
|
||||||
|
until nc -z -w 1 "$DB_HOST" "$DB_PORT"; do
|
||||||
|
>&2 echo "[JET] Waiting for MySQL to be ready..."
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
>&2 echo "[JET] MySQL is ready"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if the database is healthy
|
||||||
|
check_database
|
||||||
|
|
||||||
|
# Start the Express app
|
||||||
|
exec npm run start
|
37
example-compose.yml
Normal file
37
example-compose.yml
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
# MySQL service
|
||||||
|
db:
|
||||||
|
image: mysql:8
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
MYSQL_DATABASE: exampleDatabaseName
|
||||||
|
MYSQL_USER: exampleUser
|
||||||
|
MYSQL_PASSWORD: examplePassword
|
||||||
|
MYSQL_ROOT_PASSWORD: exampleRootPassword
|
||||||
|
ports:
|
||||||
|
- "3306:3306"
|
||||||
|
volumes:
|
||||||
|
- mysql:/var/lib/mysql
|
||||||
|
|
||||||
|
# Express app service
|
||||||
|
app:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
ports:
|
||||||
|
- "4300:4300"
|
||||||
|
environment:
|
||||||
|
DB_HOST: db
|
||||||
|
DB_PORT: 3306
|
||||||
|
DB_USER: exampleUser
|
||||||
|
DB_PASSWORD: examplePassword
|
||||||
|
DB_NAME: exampleDatabaseName
|
||||||
|
DOCKER: true
|
||||||
|
PORT: 4300
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
mysql:
|
21
package-lock.json
generated
21
package-lock.json
generated
|
@ -10,6 +10,7 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
|
"cookie-parser": "^1.4.6",
|
||||||
"dotenv": "^16.3.1",
|
"dotenv": "^16.3.1",
|
||||||
"ejs": "^3.1.9",
|
"ejs": "^3.1.9",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
|
@ -510,6 +511,26 @@
|
||||||
"node": ">= 0.6"
|
"node": ">= 0.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/cookie-parser": {
|
||||||
|
"version": "1.4.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz",
|
||||||
|
"integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==",
|
||||||
|
"dependencies": {
|
||||||
|
"cookie": "0.4.1",
|
||||||
|
"cookie-signature": "1.0.6"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/cookie-parser/node_modules/cookie": {
|
||||||
|
"version": "0.4.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz",
|
||||||
|
"integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/cookie-signature": {
|
"node_modules/cookie-signature": {
|
||||||
"version": "1.0.6",
|
"version": "1.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
|
"cookie-parser": "^1.4.6",
|
||||||
"dotenv": "^16.3.1",
|
"dotenv": "^16.3.1",
|
||||||
"ejs": "^3.1.9",
|
"ejs": "^3.1.9",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
|
|
Loading…
Reference in a new issue