Skip to content

Commit ce4aab2

Browse files
authored
Merge pull request #8 from La-404-Devinci/features/databases
Features/databases
2 parents 3665a26 + 7519977 commit ce4aab2

File tree

7 files changed

+260
-8
lines changed

7 files changed

+260
-8
lines changed

backend/.env.example

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
PORT=3000
1+
PORT=3000
2+
3+
JWT_SECRET="s3cr3t"
4+
5+
MYSQL_USER="pixelwar"
6+
MYSQL_PASSWORD="pixelwar"
7+
MYSQL_HOST="localhost"
8+
MYSQL_PORT="3336"
9+
MYSQL_DATABASE="pixelwar"
10+
11+
REDIS_USER=""
12+
REDIS_PASSWORD=""
13+
REDIS_HOST="localhost"
14+
REDIS_PORT="3337"
15+
REDIS_DATABASE="0"

backend/.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
node_modules
2-
dist
3-
out
4-
build
2+
dist/
3+
out/
4+
build/
55
.env
66
.*.local.env
7-
index.js
7+
index.js
8+
data/

backend/database/mysql.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as mysql from "mysql2/promise";
2+
3+
const access: mysql.PoolOptions = {
4+
user: process.env.MYSQL_USER,
5+
password: process.env.MYSQL_PASSWORD,
6+
host: process.env.MYSQL_HOST,
7+
port: parseInt(process.env.MYSQL_PORT ?? "3306"),
8+
database: process.env.MYSQL_DATABASE,
9+
connectionLimit: 15,
10+
};
11+
12+
export default mysql.createPool(access);

backend/database/redis.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createClient } from "redis";
2+
3+
const user = process.env.REDIS_USER;
4+
const password = process.env.REDIS_PASSWORD;
5+
const credentials = user && password ? `${user}:${password}@` : "";
6+
7+
const databaseNumber = process.env.REDIS_DATABASE;
8+
const database = databaseNumber ? `/${databaseNumber}` : "";
9+
10+
const redisUrl = `redis://${credentials}${process.env.REDIS_HOST}:${process.env.REDIS_PORT}${database}`;
11+
12+
const getRedisClient = createClient({
13+
url: redisUrl,
14+
})
15+
.on("error", (err) => console.log("Redis Client Error", err))
16+
.connect();
17+
18+
export default getRedisClient;

backend/docker-compose.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: "3"
2+
3+
services:
4+
mysql:
5+
image: mysql:8.3.0
6+
container_name: mysql-pixelwar
7+
restart: always
8+
environment:
9+
MYSQL_ROOT_PASSWORD: root
10+
MYSQL_DATABASE: pixelwar
11+
MYSQL_USER: pixelwar
12+
MYSQL_PASSWORD: pixelwar
13+
ports:
14+
- "0.0.0.0:3336:3306"
15+
volumes:
16+
- ./data/mysql:/var/lib/mysql
17+
redis:
18+
image: redis:7.2.4
19+
container_name: redis-pixelwar
20+
restart: always
21+
ports:
22+
- "0.0.0.0:3337:6379"
23+
volumes:
24+
- ./data/redis:/data

backend/package-lock.json

Lines changed: 183 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"license": "ISC",
1515
"devDependencies": {
1616
"@types/express": "^4.17.21",
17+
"@types/node": "^20.11.17",
1718
"@types/jsonwebtoken": "^9.0.5",
1819
"@types/node": "^20.11.16",
1920
"@typescript-eslint/eslint-plugin": "^6.21.0",
@@ -27,6 +28,8 @@
2728
"dotenv": "^16.4.1",
2829
"express": "^4.18.2",
2930
"jsonwebtoken": "^9.0.2",
31+
"mysql2": "^3.9.1",
32+
"redis": "^4.6.13",
3033
"socket.io": "^4.7.4"
3134
}
3235
}

0 commit comments

Comments
 (0)