Merge branch 'add-pg-client'
This commit is contained in:
commit
37259bc5d7
19
README.md
Normal file
19
README.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Briques
|
||||||
|
|
||||||
|
On aime les briques
|
||||||
|
|
||||||
|
## Dev
|
||||||
|
|
||||||
|
To run postgresql server + adminer in dev environnement (assuming you're running the backend manually with node), run :
|
||||||
|
|
||||||
|
```
|
||||||
|
docker compose -f docker-compose.dev.yml up
|
||||||
|
```
|
||||||
|
|
||||||
|
## Production
|
||||||
|
|
||||||
|
Use the file `docker-compose.prod.yml` to start the app in production mode, it can be done using the following command :
|
||||||
|
|
||||||
|
```
|
||||||
|
docker compose -f docker-compose.dev.yml up
|
||||||
|
```
|
5
back-end/.dockerignore
Normal file
5
back-end/.dockerignore
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
.env
|
||||||
|
.env.example
|
||||||
|
nodemon.json
|
@ -1,7 +1,10 @@
|
|||||||
NODE_ENV="dev"
|
# This .env file is meant for developpement usage, use
|
||||||
|
# .env.production for production
|
||||||
|
NODE_PORT=3000
|
||||||
|
NODE_ENV=dev
|
||||||
|
|
||||||
DB_HOST="postgres"
|
# those values must be the same as in ../docker-compose.dev.yml
|
||||||
DB_NAME="briques"
|
DB_HOST=localhost
|
||||||
DB_USER="briques_db"
|
DB_NAME=briques_db
|
||||||
DB_PASSWORD="briques_password"
|
DB_USER=briques_llm
|
||||||
DB_PORT=5432
|
DB_PASSWORD=briques_password_2025
|
||||||
|
6
back-end/.env.production
Normal file
6
back-end/.env.production
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
NODE_PORT=3000
|
||||||
|
NODE_ENV=production
|
||||||
|
DB_HOST=briques_postegres
|
||||||
|
DB_NAME=briques_db
|
||||||
|
DB_USER=briques_llm
|
||||||
|
DB_PASSWORD=briques_password_2025
|
@ -4,30 +4,14 @@ FROM node:22-alpine
|
|||||||
# Workdir definition
|
# Workdir definition
|
||||||
WORKDIR /usr/src/back
|
WORKDIR /usr/src/back
|
||||||
|
|
||||||
# Environment declaration variables
|
|
||||||
ENV NODE_ENV="dev"
|
|
||||||
ENV DB_HOST="briques_postgres"
|
|
||||||
ENV DB_NAME="briques_db"
|
|
||||||
ENV DB_USER="briques_llm"
|
|
||||||
ENV DB_PASSWORD="briques_password_2025"
|
|
||||||
ENV DB_PORT=5432
|
|
||||||
|
|
||||||
COPY package.json ./
|
COPY package.json ./
|
||||||
|
|
||||||
RUN npm install
|
RUN npm install
|
||||||
|
|
||||||
RUN npm install -g pm2
|
RUN npm install -g pm2
|
||||||
|
|
||||||
# Copy sources
|
# Copy sources
|
||||||
COPY . .
|
COPY . .
|
||||||
|
COPY .env.production .env
|
||||||
# Setting up environment
|
|
||||||
RUN echo "NODE_ENV=${NODE_ENV}" > .env \
|
|
||||||
&& echo "DB_HOST=${DB_HOST}" >> .env \
|
|
||||||
&& echo "DB_NAME=${DB_NAME}" >> .env \
|
|
||||||
&& echo "DB_USER=${DB_USER}" >> .env \
|
|
||||||
&& echo "DB_PORT=${DB_PORT}" >> .env \
|
|
||||||
&& echo "DB_PASSWORD=${DB_PASSWORD}" >> .env
|
|
||||||
|
|
||||||
# Compilation des fichiers TypeScript
|
# Compilation des fichiers TypeScript
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
@ -40,5 +24,4 @@ RUN chmod -R u=rwx,g=,o= ./
|
|||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
# Start app
|
# Start app
|
||||||
CMD ["npm", "run", "startonly"]
|
CMD ["pm2", "start", "dist/app.js", "--no-daemon"]
|
||||||
#CMD ["pm2", "start", "dist/app.js", "--no-daemon"]
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"startonly": "node dist/app.js",
|
"startonly": "node dist/app.js",
|
||||||
"start": "tsc && node dist/app.js",
|
"start": "tsc && node dist/app.js",
|
||||||
"build": "tsc",
|
"build": "tsc && cp -r src/templates dist",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
@ -13,13 +13,18 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"description": "",
|
"description": "",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/ejs": "^3.1.5",
|
||||||
"@types/express": "^5.0.0",
|
"@types/express": "^5.0.0",
|
||||||
|
"@types/morgan": "^1.9.9",
|
||||||
"@types/pg": "^8.11.10",
|
"@types/pg": "^8.11.10",
|
||||||
"globals": "^15.11.0",
|
"globals": "^15.11.0",
|
||||||
"typescript": "^5.6.3"
|
"typescript": "^5.6.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"dotenv": "^16.4.5",
|
||||||
|
"ejs": "^3.1.10",
|
||||||
"express": "^4.21.1",
|
"express": "^4.21.1",
|
||||||
|
"morgan": "^1.10.0",
|
||||||
"pg": "^8.13.1"
|
"pg": "^8.13.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,31 @@
|
|||||||
|
// Load dotenv before any process.env is accessed
|
||||||
|
import dotenv from 'dotenv';
|
||||||
|
dotenv.config()
|
||||||
|
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import db from './db';
|
import morgan from 'morgan';
|
||||||
|
import path from 'path';
|
||||||
|
import routes from './routes';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
const port = process.env.PORT ?? 3000;
|
const port = process.env.PORT ?? 3000;
|
||||||
|
|
||||||
app.get('/', async (_req, res) => {
|
const log_format = (process.env.NODE_ENV === "dev") ? "dev": "combined";
|
||||||
const message = await db.example_request();
|
console.log("=== LOG CONFIG ===");
|
||||||
res.send(message);
|
console.log(`log format: ${log_format}`);
|
||||||
});
|
console.log("=== END LOG CONFIG ===");
|
||||||
|
|
||||||
|
// Need the path.join for node to resolve correctly the templates directory
|
||||||
|
app.set('views', path.join(__dirname, 'templates'));
|
||||||
|
app.set('view engine', 'ejs');
|
||||||
|
|
||||||
|
// Add logs
|
||||||
|
app.use(morgan(log_format));
|
||||||
|
|
||||||
|
// Defines endpoints
|
||||||
|
app.use(routes);
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
return console.log(`Express is listening at http://localhost:${port}`);
|
return console.log(`Briques is listening at http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
@ -7,17 +7,19 @@ const db_host = process.env.DB_HOST ?? "localhost";
|
|||||||
const db_port = parseInt(process.env.DB_PORT ?? "5432");
|
const db_port = parseInt(process.env.DB_PORT ?? "5432");
|
||||||
|
|
||||||
console.log("=== DB CONFIG ===");
|
console.log("=== DB CONFIG ===");
|
||||||
console.log(`DB_USER:\t\t${db_user}`);
|
console.log(`DB_USER:\t${db_user}`);
|
||||||
console.log(`DB_PASSWORD:\t${db_user}`);
|
console.log(`DB_PASSWORD:\t${db_user}`);
|
||||||
console.log(`DB_NAME:\t\t${db_name}`);
|
console.log(`DB_NAME:\t${db_name}`);
|
||||||
console.log(`DB_HOST:\t\t${db_host}`);
|
console.log(`DB_HOST:\t${db_host}`);
|
||||||
console.log(`DB_PORT:\t\t${db_port}`);
|
console.log(`DB_PORT:\t${db_port}`);
|
||||||
|
console.log("=== END DB CONFIG ===");
|
||||||
|
|
||||||
|
export function new_client() {
|
||||||
export const new_client = () => new pg.Client({
|
return new pg.Client({
|
||||||
user: db_user,
|
user: db_user,
|
||||||
password: db_password,
|
password: db_password,
|
||||||
database: db_name,
|
database: db_name,
|
||||||
host: db_host,
|
host: db_host,
|
||||||
port: db_port,
|
port: db_port,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
16
back-end/src/routes/index.ts
Normal file
16
back-end/src/routes/index.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
import db from '../db';
|
||||||
|
|
||||||
|
const routes = Router();
|
||||||
|
|
||||||
|
routes.get('/', async (_req, res) => {
|
||||||
|
const message = await db.example_request();
|
||||||
|
res.send(message);
|
||||||
|
});
|
||||||
|
|
||||||
|
routes.get("/message", async (_req, res) => {
|
||||||
|
const message = await db.example_request();
|
||||||
|
res.render('index.ejs', { message });
|
||||||
|
});
|
||||||
|
|
||||||
|
export default routes;
|
13
back-end/src/templates/index.ejs
Normal file
13
back-end/src/templates/index.ejs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Leohl !dworl</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Naisu</h1>
|
||||||
|
<h2><%= message %></h2>
|
||||||
|
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sapiente quod dicta molestiae harum veniam iste, nostrum nemo earum commodi fugit modi, ratione obcaecati, beatae ullam. Debitis ducimus dignissimos rem at.</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
20
docker-compose.dev.yml
Normal file
20
docker-compose.dev.yml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
services:
|
||||||
|
database:
|
||||||
|
hostname: database
|
||||||
|
container_name: briques_postgres
|
||||||
|
image: postgres
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
- POSTGRES_DB=${DB_NAME}
|
||||||
|
- POSTGRES_USER=${DB_USER}
|
||||||
|
- POSTGRES_PASSWORD=${DB_PASSWORD}
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
volumes:
|
||||||
|
- ./database:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
adminer:
|
||||||
|
image: adminer
|
||||||
|
restart: always
|
||||||
|
ports:
|
||||||
|
- 8080:8080
|
@ -11,9 +11,6 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- ./database:/var/lib/postgresql/data
|
- ./database:/var/lib/postgresql/data
|
||||||
|
|
||||||
networks:
|
|
||||||
- briques-db
|
|
||||||
|
|
||||||
|
|
||||||
back:
|
back:
|
||||||
hostname: back
|
hostname: back
|
||||||
@ -32,21 +29,3 @@ services:
|
|||||||
- '${NODE_PORT}:3000'
|
- '${NODE_PORT}:3000'
|
||||||
depends_on:
|
depends_on:
|
||||||
- database
|
- database
|
||||||
|
|
||||||
networks:
|
|
||||||
- briques-db
|
|
||||||
|
|
||||||
adminer:
|
|
||||||
image: adminer
|
|
||||||
restart: always
|
|
||||||
ports:
|
|
||||||
- 8080:8080
|
|
||||||
|
|
||||||
networks:
|
|
||||||
- briques-db
|
|
||||||
|
|
||||||
|
|
||||||
networks:
|
|
||||||
briques-db:
|
|
||||||
driver: bridge
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user