Skip to content
Snippets Groups Projects
Commit 062b312a authored by benjamin.sitbon's avatar benjamin.sitbon
Browse files

3 instances et les dockerfiles

parent 9b9d342c
Branches
No related tags found
No related merge requests found
Showing
with 3253 additions and 291 deletions
FROM postgres
ENV POSTGRES_PASSWORD postgres
ENV POSTGRES_DB testdb
COPY dump.sql /docker-entrypoint-initdb.d/
EXPOSE 3306/tcp
FROM node:16
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
const userDB = []; const userDB = [];
const pokemons = []; module.exports = {userDB};
module.exports = {userDB,pokemons};
This diff is collapsed.
...@@ -13,10 +13,11 @@ ...@@ -13,10 +13,11 @@
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"mongoose": "^5.12.13", "mongoose": "^5.12.13",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"mysql": "^2.18.1",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"nodemon": "^2.0.7" "nodemon": "^2.0.7",
"pg": "^8.7.1"
}, },
"devDependencies": {},
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js" "start": "nodemon server.js"
......
...@@ -10,15 +10,35 @@ const bcrypt = require('bcrypt'); ...@@ -10,15 +10,35 @@ const bcrypt = require('bcrypt');
const jwt = require("jsonwebtoken"); const jwt = require("jsonwebtoken");
const express = require('express'); const express = require('express');
const app = express(); const app = express();
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false}));
const users = require('./data').userDB; const users = require('./data').userDB;
const pokemons = require('./data').pokemons;
const cart = require('./data').cart; const cart = require('./data').cart;
const { Client } = require('pg')
const client = new Client({
user: 'postgres',
host: 'localhost',
database: 'testdb',
password: 'postgres',
port: 5432,
})
client.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
function Pokemons(){ function Pokemons(){
return fetch("https://pokeapi.co/api/v2/pokemon/?limit=900") return fetch("https://pokeapi.co/api/v2/pokemon/?limit=900")
...@@ -36,13 +56,10 @@ function get(that) { ...@@ -36,13 +56,10 @@ function get(that) {
} }
let api = Pokemons(); let api = Pokemons();
api.then(pok =>{ api.then(pok =>{
for(let i = 0; i<807; i++){ for(let i = 0; i<809; i++){
let type1 = "none"; let type1 = "none";
let type2 = "none"; let type2 = "none";
...@@ -58,170 +75,23 @@ api.then(pok =>{ ...@@ -58,170 +75,23 @@ api.then(pok =>{
type1 = stat.types[0].type.name; type1 = stat.types[0].type.name;
} }
let pokemon = {
id: i+1,
name: pok.results[i].name,
type: {
0:type1,
1:type2 }
}
pokemons.push(pokemon);
})
}catch{
console.log("Api error !");
}
}
})
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false}));
app.use(express.static('../frontend'));
app.get(config.rootApi + "pokemons/:id",function (req,res){
let index = getIndex(req.params.id,pokemons);
(index < 0) ? res.sendFile(__dirname +'/html/error404.html') : res.json(success(pokemons[index]));
})
app.get(config.rootApi + "pokemons",function (req,res){
res.json(success(pokemons));
})
app.post('/register', async (req, res) => { const text = 'INSERT INTO pokeshop.Pokemon(id,name,type1,type2) VALUES($1,$2,$3,$4) RETURNING *'
console.log(req.body); const values = [i+1,pok.results[i].name, type1,type2]
client.query(text, values, (err, res) => {
try{ if (err) {
let foundUser = users.find((data) => req.body.e === data.email); //console.log(err.stack)
if (!foundUser) {
let hashPassword = await bcrypt.hash(req.body.p, 10);
let newUser = {
username: req.body.u,
email: req.body.e,
password: hashPassword,
cart:[]
};
users.push(newUser);
console.log('User list', users);
res.json({ msg: "success" });
} else { } else {
res.json({ msg: "invalid" }); //console.log(res.rows)
}
} catch{
res.json({ msg: "error" });
} }
});
app.post('/login', async (req, res) => {
try{
let foundUser = users.find((data) => req.body.email.toLowerCase() === data.email.toLowerCase());
if(!foundUser){
foundUser = users.find((data) => req.body.email.toLowerCase() === data.username.toLowerCase());
}
if (foundUser) {
let submittedPass = req.body.password;
let storedPass = foundUser.password;
const passwordMatch = await bcrypt.compare(submittedPass, storedPass);
if (passwordMatch) {
let usrname = foundUser.username;
const token = jwt.sign({ id: foundUser.usrname }, "bush", {
expiresIn: "15m",
}) })
res.json({msg: "login success", token,usrname})
} else {
res.json({ msg: "invalid" });
}
}
else {
res.json({ msg: "invalid" });
}
} catch{
res.json({ msg: "error" });
}
});
app.post('/me', async (req, res) => {
let token = req.body.token;
try{
let check = jwt.verify(token,"bush");
res.json({msg: "Connected", status: 1});
}catch{
if(!token){
res.json({msg: "Disconnected", status: 0});
}else{
res.json({msg: "Session expired", status: -1});
}
}
}) })
app.post("/yourcart", async function(req,res){
try{
let foundUser = users.find((data) => req.body.username ===data.username);
res.json(foundUser.cart);
}catch{
res.json({msg:"error occured"});
}
})
app.put('/add', async (req, res) => {
try{
let foundUser = users.find((data) => req.body.username === data.username);
foundUser.cart.push(req.body);
res.end();
}catch{ }catch{
res.json({msg: "error occured"}); console.log("Api error !");
}
})
app.delete('/remove', async (req, res) => {
try{
let foundUser = users.find((data) => req.body.username === data.username);
for(let i = 0; i<foundUser.cart.length; i++){
if(foundUser.cart[i].pokemon.toLowerCase() === req.body.name.toLowerCase()){
foundUser.cart.splice(i,1);
}
} }
res.end();
}catch{
res.json({msg: "error occured"});
} }
}) })
app.listen(config.port, ()=> console.log('Attrapez les tous !')) app.listen(config.port, ()=> console.log('Attrapez les tous !'))
function getIndex(id,tab){
return (tab.map((a) => a.id).indexOf(parseInt(id)));
}
\ No newline at end of file
version: "3.1"
services:
db:
image: mysql:8.0
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: test
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- ./conf:/etc/mysql/conf.d
- persistent:/var/lib/mysql
networks:
- default
volumes:
persistent:
CREATE SCHEMA IF NOT EXISTS `pokeshop` DEFAULT CHARACTER SET utf8 ; CREATE SCHEMA IF NOT EXISTS pokeshop;
USE pokeshop; --USE pokeshop;
CREATE TABLE IF NOT EXISTS `User` ( CREATE TABLE IF NOT EXISTS pokeshop.User (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, id SERIAL PRIMARY KEY,
`username` VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL,
`hash` VARCHAR(128) NOT NULL DEFAULT '', hash VARCHAR(128) NOT NULL DEFAULT ''
PRIMARY KEY (`id`), );
UNIQUE (`username`)
) ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS pokeshop.Panier (
id SERIAL PRIMARY KEY,
CREATE TABLE IF NOT EXISTS `Panier` ( nb_articles INT NOT NULL,
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, prix INT NOT NULL
`nb_articles` INT UNSIGNED NOT NULL, );
`prix` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`) CREATE TABLE IF NOT EXISTS pokeshop.Possede (
) ENGINE=InnoDB; panier_id INT NOT NULL,
user_id INT NOT NULL,
CREATE TABLE IF NOT EXISTS `Possede` ( PRIMARY KEY (panier_id,user_id),
PRIMARY KEY (`panier_id`) CONSTRAINT fk_pokemon_id
PRIMARY KEY (`username`) FOREIGN KEY (panier_id)
CONSTRAINT `fk_pokemon_id` REFERENCES pokeshop.Panier (id),
FOREIGN KEY (`panier_id`) CONSTRAINT fk_user_id
REFERENCES `pokeshop`.`Panier` (`id`) FOREIGN KEY (user_id)
ON DELETE CASCADE REFERENCES pokeshop.User (id)
ON UPDATE CASCADE, );
CONSTRAINT `fk_user_id`
FOREIGN KEY (`user_id`) CREATE TABLE IF NOT EXISTS pokeshop.Pokemon (
REFERENCES `pokeshop`.`user` (`id`) id SERIAL PRIMARY KEY,
ON DELETE CASCADE name VARCHAR(32) NOT NULL,
ON UPDATE CASCADE type1 VARCHAR(100) NOT NULL,
) ENGINE=InnoDB; type2 VARCHAR(100) NOT NULL
CREATE TABLE IF NOT EXISTS `Pokemon` ( );
`id` INT UNSIGNED NOT NULL,
`name` VARCHAR(32) NOT NULL, CREATE TABLE IF NOT EXISTS pokeshop.Contient (
`type1` VARCHAR(100) NOT NULL, pokemon_id INT NOT NULL,
`type2` VARCHAR(100) NOT NULL, panier_id INT NOT NULL,
`Image` VARCHAR(100) NOT NULL, PRIMARY KEY (pokemon_id,panier_id),
UNIQUE(`id`), CONSTRAINT fk_pokemon_id
FOREIGN KEY (pokemon_id)
) ENGINE=InnoDB; REFERENCES pokeshop.Pokemon (id),
CONSTRAINT fk_panier_id
CREATE TABLE IF NOT EXISTS `Contient` ( FOREIGN KEY (panier_id)
PRIMARY KEY (`pokemon_id`), REFERENCES pokeshop.Panier (id)
PRIMARY KEY (`panier_id`),
CONSTRAINT `fk_pokemon_id` );
FOREIGN KEY (`pokemon_id`)
REFERENCES `pokeshop`.`Pokemon` (`id`) CREATE TABLE IF NOT EXISTS pokeshop.Attaque (
ON DELETE CASCADE id SERIAL PRIMARY KEY,
ON UPDATE CASCADE, name VARCHAR(32) NOT NULL,
CONSTRAINT `fk_panier_id` description VARCHAR(128) NOT NULL,
FOREIGN KEY (`panier_id`) type VARCHAR(32) NOT NULL,
REFERENCES `pokeshop`.`Panier` (`id`) categorie VARCHAR(32) NOT NULL
ON DELETE CASCADE
ON UPDATE CASCADE );
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS pokeshop.Peut_apprendre (
CREATE TABLE IF NOT EXISTS `Peut_apprendre` ( attaque_id INT NOT NULL,
PRIMARY KEY (`attaque_id`), pokemon_id INT NOT NULL,
PRIMARY KEY (`pokemon_id`), PRIMARY KEY (pokemon_id,attaque_id),
CONSTRAINT `fk_pokemon_id` CONSTRAINT fk_pokemon_id
FOREIGN KEY (`pokemon_id`) FOREIGN KEY (pokemon_id)
REFERENCES `pokeshop`.`Pokemon` (`id`) REFERENCES pokeshop.Pokemon (id),
ON DELETE CASCADE CONSTRAINT fk_panier_id
ON UPDATE CASCADE, FOREIGN KEY (attaque_id)
CONSTRAINT `fk_panier_id` REFERENCES pokeshop.Attaque (id)
FOREIGN KEY (`attaque_id`)
REFERENCES `pokeshop`.`Attaque` (`id`) );
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS pokeshop.Appris (
attaque_id INT NOT NULL,
pokemon_id INT NOT NULL,
CREATE TABLE IF NOT EXISTS `Appris` ( PRIMARY KEY (pokemon_id,attaque_id),
PRIMARY KEY (`attaque_id`), CONSTRAINT fk_pokemon_id
PRIMARY KEY (`pokemon_id`), FOREIGN KEY (pokemon_id)
CONSTRAINT `fk_pokemon_id` REFERENCES pokeshop.Pokemon (id),
FOREIGN KEY (`pokemon_id`) CONSTRAINT fk_panier_id
REFERENCES `pokeshop`.`Pokemon` (`id`) FOREIGN KEY (attaque_id)
ON DELETE CASCADE REFERENCES pokeshop.Attaque (id)
ON UPDATE CASCADE, );
CONSTRAINT `fk_panier_id`
FOREIGN KEY (`attaque_id`)
REFERENCES `pokeshop`.`Attaque` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `Attaque` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL,
`desc` VARCHAR(2048) NOT NULL DEFAULT '',
`type` VARCHAR(32) NOT NULL,
`categorie` VARCHAR(32) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB;
FROM node:16
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
{
"rootApi": "/api/v1/",
"port": 8080
}
const userDB = [];
module.exports = {userDB};
exports.success = function(result){
return {
status: 'success',
result: result
}
}
exports.error = function(message){
return {
status: 'error',
result: message
}
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error 404</title>
</head>
<body>
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);">
<img src="https://cdn2.scratch.mit.edu/get_image/gallery/3004757_200x130.png?v=1472688587.05" alt="none">
<p style="text-align: center; font-size: x-large;">Error 404 Not found !</p>
</div>
</body>
</html>
\ No newline at end of file
...@@ -108,7 +108,7 @@ ...@@ -108,7 +108,7 @@
var img = document.createElement("img"); var img = document.createElement("img");
img.id = "pokemon" + i; img.id = "pokemon" + i;
img.className = "canvas"; img.className = "canvas";
img.src = "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/" + pok.result[i].id + ".png"; img.src = "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/" + pok[i].id + ".png";
var button = createStyleButton(); var button = createStyleButton();
button.id = i; button.id = i;
...@@ -122,7 +122,7 @@ ...@@ -122,7 +122,7 @@
let type = document.getElementById(i).parentNode.children[1].children; let type = document.getElementById(i).parentNode.children[1].children;
let data = [name,type[0].src,type[1].src,pok.result[i].id] let data = [name,type[0].src,type[1].src,pok[i].id]
tokenExpired(); tokenExpired();
...@@ -184,7 +184,7 @@ ...@@ -184,7 +184,7 @@
//let pokemons = Pokemons(); //let pokemons = Pokemons();
let api = get("http://localhost:8081/api/v1/pokemons"); let api = get("http://localhost:8080/api/v1/pokemons");
boxGenerator(0,809,api); boxGenerator(0,809,api);
...@@ -194,24 +194,24 @@ ...@@ -194,24 +194,24 @@
api.then(Poks => { api.then(Poks => {
for(let i = 0; i<Poks.result.length; i++){ for(let i = 0; i<Poks.length; i++){
names[i].innerHTML = Poks.result[i].name; names[i].innerHTML = Poks[i].name;
//Ajout d'un élément before pour donner le numéro pokedex du pokemon //Ajout d'un élément before pour donner le numéro pokedex du pokemon
let before = document.createElement("p"); let before = document.createElement("p");
before.innerHTML = Poks.result[i].id; before.innerHTML = Poks[i].id;
before.className ="before"; before.className ="before";
names[i].before(before); names[i].before(before);
//Parcours 2 par 2 des types //Parcours 2 par 2 des types
if(Poks.result[i].type[1] != "none"){ if(Poks[i].type2 != "none"){
types[2*i].src = "img/types/" + Poks.result[i].type[0] + ".png"; types[2*i].src = "img/types/" + Poks[i].type1 + ".png";
types[(2*i)+1].src = "img/types/" + Poks.result[i].type[1] + ".png"; types[(2*i)+1].src = "img/types/" + Poks[i].type2 + ".png";
} }
else{ else{
types[2*i].src = "img/types/" + Poks.result[i].type[0] + ".png"; types[2*i].src = "img/types/" + Poks[i].type1 + ".png";
types[(2*i)+1].style.display = "none"; types[(2*i)+1].style.display = "none";
} }
} }
......
../detect-libc/bin/detect-libc.js
\ No newline at end of file
../ejs/bin/cli.js
\ No newline at end of file
../is-ci/bin.js
\ No newline at end of file
../jake/bin/cli.js
\ No newline at end of file
../mime/cli.js
\ No newline at end of file
../mkdirp/bin/cmd.js
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment