Remove mariadb and setup Firestore

main
felixmielcarek 7 months ago
parent 2357d8551b
commit 4099b4cf13

@ -1,101 +0,0 @@
kind: pipeline
type: docker
name: BB_PIPELINE
steps:
- name: deploy-container-db
image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
environment:
IMAGENAME: mariadb:10
CONTAINERNAME: bigbrotherdb
COMMAND: create
OVERWRITE: true
PRIVATE: true
CODEFIRST_CLIENTDRONE_ENV_MARIADB_ROOT_PASSWORD:
from_secret: db_root_password
CODEFIRST_CLIENTDRONE_ENV_MARIADB_DATABASE:
from_secret: db_database
CODEFIRST_CLIENTDRONE_ENV_MARIADB_USER:
from_secret: db_user
CODEFIRST_CLIENTDRONE_ENV_MARIADB_PASSWORD:
from_secret: db_password
- name: create-bigbrother-database
image: python:latest
commands:
- cd database/
- pip3 install mariadb
- python3 db-creation.py
environment:
MARIADB_USER:
from_secret: db_user
MARIADB_PASSWORD:
from_secret: db_password
MARIADB_DATABASE:
from_secret: db_database
depends_on: [ deploy-container-db ]
- name: build-image-server
image: plugins/docker
settings:
dockerfile: callback-server/Dockerfile
context: ./
registry: hub.codefirst.iut.uca.fr
repo: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother-server
username:
from_secret: SECRET_REGISTRY_USERNAME
password:
from_secret: SECRET_REGISTRY_PASSWORD
environment:
MARIADB_USER:
from_secret: db_user
MARIADB_PASSWORD:
from_secret: db_password
MARIADB_DATABASE:
from_secret: db_database
CLIENT_ID:
from_secret: spotify_client_id
CLIENT_SECRET:
from_secret: spotify_client_secret
depends_on: [ create-bigbrother-database ]
- name: deploy-container-server
image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
environment:
IMAGENAME: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother-server:latest
CONTAINERNAME: bigbrotherserver
COMMAND: create
OVERWRITE: true
depends_on: [ build-image-server ]
# - name: build-image-script
# image: plugins/docker
# settings:
# dockerfile: script/Dockerfile
# context: ./
# registry: hub.codefirst.iut.uca.fr
# repo: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother
# username:
# from_secret: SECRET_REGISTRY_USERNAME
# password:
# from_secret: SECRET_REGISTRY_PASSWORD
# #depends_on: [ create-bigbrother-database ]
# - name: deploy-container-script
# image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest
# environment:
# IMAGENAME: hub.codefirst.iut.uca.fr/felix.mielcarek/big-brother:latest
# CONTAINERNAME: bigbrotherscript
# COMMAND: create
# OVERWRITE: true
# MARIADB_USER:
# from_secret: db_user
# MARIADB_PASSWORD:
# from_secret: db_password
# MARIADB_DATABASE:
# from_secret: db_database
# CLIENT_ID:
# from_secret: spotify_client_id
# CLIENT_SECRET:
# from_secret: spotify_client_secret
# depends_on: [ build-image-script ]

2
.gitignore vendored

@ -130,5 +130,5 @@ dist
.pnp.*
# Specific to Gold Digger
spotify_access_token
firebaseServiceAccountKey.json
.vscode/

@ -1,16 +0,0 @@
FROM node:latest
# Set working directory inside the container
WORKDIR /usr/src/app
COPY callback-server/package*.json ./callback-server/
RUN npm install ./callback-server
COPY callback-server ./callback-server/
RUN env > .env
# Expose the port the app runs on
EXPOSE 80
# Command to run the application
CMD ["node", "--env-file=.env" , "callback-server/app.js"]

@ -1,19 +1,28 @@
//#region REQUIRE
const express = require('express')
const axios = require('axios');
const mariadb = require('mariadb');
const cors = require('cors');
//#region IMPORTS
import express from 'express';
import axios from 'axios';
import cors from 'cors';
import dotenv from 'dotenv';
import admin from 'firebase-admin';
//#endregion
//#region CONSTANTS
//#region CONFIGURATIONS
dotenv.config();
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
client_email: process.env.FIREBASE_CLIENT_EMAIL,
})
});
const db = admin.firestore();
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
const mariadbUser = process.env.MARIADB_USER;
const mariadbDatabase = process.env.MARIADB_DATABASE;
const mariadbPassword = process.env.MARIADB_PASSWORD;
const port = 80
const redirectUri = 'https://felixmielcarek.github.io/big-brother/callback/';
const allowedDomain = [ 'https://felixmielcarek.github.io' ];
const port = process.env.PORT;
const redirectUri = process.env.REDIRECT_URL;
const allowedDomain = [ process.env.ALLOWED_DOMAIN ];
//#endregion
//#region LOGS
@ -30,27 +39,14 @@ app.use(express.json());
app.listen(port, () => { console.log(`Big brother is listening on port ${port}`) })
//#endregion
const fs = require('fs');
const path = '/usr/src/app/.env';
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
console.log(data);
});
//#region ACCESS TOKEN
app.post('/', async (req, res) => {
console.log(clientId);
app.get('/callback', async (req, res) => {
stepBeggining("Activation");
let data;
let accessToken;
let refreshToken;
const code = req.body.code;
const code = req.query.code;
try {
const authOptions = {
@ -86,92 +82,17 @@ app.post('/', async (req, res) => {
res.status(500).send('Error');
}
const sqlQuery = `
INSERT INTO users (spotifyid, accesstoken, refreshtoken)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE
accesstoken = VALUES(accesstoken),
refreshtoken = VALUES(refreshtoken);
`;
try {
const pool = mariadb.createPool({
host: 'felixmielcarek-bigbrotherdb',
user: mariadbUser,
database: mariadbDatabase,
password: mariadbPassword,
connectionLimit: 5
});
const conn = await pool.getConnection();
conn.query(sqlQuery, [data.SpotifyId, data.AccessToken, data.RefreshToken], (err, res) => {
if (err) {
console.error('Error executing query', err);
return;
}
console.log('Data inserted/updated successfully');
res.status(200).send('Ok');
conn.end();
});
} catch (error) {
console.log(`Error accessing database: ${error}`);
res.status(500).send('Error');
}
});
app.get('/settings/deactivate', async (req,res) => {
stepBeggining("Setting: deactivation");
const code = req.query.code;
const authOptions = {
url: 'https://accounts.spotify.com/api/token', method: 'post', json: true,
data: { code: code, redirect_uri: redirectUri, grant_type: 'authorization_code' },
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
}
};
let userId;
try {
const response1 = await axios(authOptions);
const accessToken = response1.data.access_token;
const response2 = await axios.get(`https://api.spotify.com/v1/me`, { headers: { 'Authorization': 'Bearer ' + accessToken, } });
userId = response2.data.SpotifyId;
} catch (error) {
console.log(`Error getting user Spotify id: ${error}`);
res.status(500).send('Error');
}
const sqlQuery = `
DELETE FROM users
WHERE spotifyid = ?;
`;
try {
const pool = mariadb.createPool({
host: 'felixmielcarek-bigbrotherdb',
user: mariadbUser,
database: mariadbDatabase,
password: mariadbPassword,
connectionLimit: 5
await db.collection("users").doc(data.SpotifyId).set({
accessToken: data.AccessToken,
refreshToken: data.RefreshToken,
});
const conn = await pool.getConnection();
conn.query(sqlQuery, [userId], (err, res) => {
if (err) {
console.error('Error executing query', err);
return;
}
console.log('Data deleted successfully');
res.status(200).send('Ok');
conn.end();
});
console.log('Data inserted/updated successfully');
res.status(200).send('Ok');
} catch (error) {
console.log(`Error accessing database: ${error}`);
res.status(500).send('Error');
}
});
//#endregion
//#endregion

File diff suppressed because it is too large Load Diff

@ -1,19 +1,20 @@
{
"name": "bigbrother-callback-server",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "app.js",
"keywords": [
"big-brother",
"spotify"
],
"scripts": {
"start": "node app.js"
},
"author": "Félix Mielcarek",
"license": "MIT",
"dependencies": {
"axios": "^1.6.7",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.3",
"fs": "^0.0.1-security",
"mariadb": "^3.3.0"
"firebase": "^11.0.1",
"firebase-admin": "^12.7.0",
"fs": "^0.0.1-security"
}
}

@ -1,12 +0,0 @@
FROM postgres:latest
ENV POSTGRES_DB=big-brother
ENV POSTGRES_PASSWORD=postgres
COPY init_db.py /docker-entrypoint-initdb.d/
RUN apt-get update && \
apt-get install -y python3 && \
apt install python3-psycopg2
EXPOSE 5432

@ -1,18 +0,0 @@
import psycopg2
conn = psycopg2.connect(
database="big-brother", user='postgres', password='postgres', host='127.0.0.1', port= '5432'
)
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS USERS")
sql ='''CREATE TABLE USERS(
SPOTIFY_ID VARCHAR(255) PRIMARY KEY,
ACCESS_TOKEN VARCHAR(255),
REFRESH_TOKEN VARCHAR(255)
)'''
cursor.execute(sql)
print("Table created successfully........")
conn.commit()
conn.close()

@ -1,23 +0,0 @@
FROM node:latest
# Install Python and pip
RUN apt-get update && \
apt-get install -y python3 python3-pip && \
rm -rf /var/lib/apt/lists/*
# Set working directory inside the container
WORKDIR /usr/src/app
COPY script/package*.json ./script/
RUN npm install ./script
COPY script ./script/
COPY common ./common/
RUN touch common/.env
# Expose the port the app runs on
EXPOSE 80
# Command to run the application
CMD ["python3", "common/set-env-var.py"]
CMD ["node", "--env-file=common/.env" , "script/app.js"]

@ -1,9 +1,21 @@
//#region REQUIRE
const axios = require('axios');
const mariadb = require('mariadb');
//#region IMPORTS
import axios from 'axios';
import dotenv from 'dotenv';
import admin from 'firebase-admin';
//#endregion
//#region CONSTANTS
//#region CONFIGURATIONS
dotenv.config();
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
client_email: process.env.FIREBASE_CLIENT_EMAIL,
})
});
const db = admin.firestore();
const spotifyRequestsLimit = 50;
const thresholdLove = 0.6;
const clientId = process.env.CLIENT_ID;
@ -170,54 +182,38 @@ async function mainAlgorithm(accessToken) {
}
async function main() {
const pool = mariadb.createPool({
host: 'felixmielcarek-bigbrotherdb',
user: process.env.MARIADB_USER,
database: process.env.MARIADB_DATABASE,
password: process.env.MARIADB_PASSWORD,
connectionLimit: 5
});
let conn;
try {
conn = await pool.getConnection();
const selectQuery = 'SELECT * FROM users';
const selectResult = await conn.query(selectQuery);
for(let row of selectResult.rows) {
const spotifyId = row.spotifyid;
const refreshToken = row.refreshtoken;
const querySnapshot = await db.collection("users").get();
for (const doc of querySnapshot.docs) {
const spotifyId = doc.id;
const refreshToken = doc.data().refreshToken;
let authOptions = {
url: 'https://accounts.spotify.com/api/token',
method: 'post',
data: {
data: new URLSearchParams({
refresh_token: refreshToken,
grant_type: 'refresh_token',
},
client_id: clientId
}),
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
},
json: true
'Authorization': 'Basic ' + Buffer.from(clientId + ':' + clientSecret).toString('base64'),
}
};
const response = await axios(authOptions);
const newAccessToken = response.data.access_token;
const newRefreshToken = response.data.refresh_token;
const updateQuery = `
UPDATE users
SET accesstoken = ?, refreshtoken = ?
WHERE spotifyid = ?;
`;
await conn.query(updateQuery, [spotifyId, newAccessToken, newRefreshToken])
await db.collection("users").doc(spotifyId).set({
accessToken: newAccessToken,
refreshToken: newRefreshToken !== undefined ? newRefreshToken : refreshToken,
});
await mainAlgorithm(newAccessToken);
}
} catch (error) { console.error('Error executing select query:', error) } finally { await conn.end() }
} catch (error) { console.error('Error getting Firestore data: ', error) }
}
//#endregion

File diff suppressed because it is too large Load Diff

@ -1,16 +1,17 @@
{
"name": "bigbrother-script",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "app.js",
"keywords": [
"big-brother",
"spotify"
],
"scripts": {
"start": "node app.js"
},
"author": "Félix Mielcarek",
"license": "MIT",
"dependencies": {
"axios": "^1.7.2",
"mariadb": "^3.3.0"
"dotenv": "^16.4.5",
"firebase": "^11.0.1",
"firebase-admin": "^12.7.0"
}
}

Loading…
Cancel
Save