From c1049ee82c7d9de6a51193f3523bb824a214656f Mon Sep 17 00:00:00 2001 From: remyalline Date: Tue, 4 Dec 2018 18:21:31 +0100 Subject: [PATCH 1/4] final version --- Readme.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Readme.txt diff --git a/Readme.txt b/Readme.txt new file mode 100644 index 00000000..80bc9f6d --- /dev/null +++ b/Readme.txt @@ -0,0 +1 @@ +bonjour \ No newline at end of file From 62b4993d32b685c2b7e3054a3d67c11e7cd128ca Mon Sep 17 00:00:00 2001 From: remyalline Date: Tue, 4 Dec 2018 18:27:31 +0100 Subject: [PATCH 2/4] addition of express js project --- project.js | 848 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 848 insertions(+) create mode 100644 project.js diff --git a/project.js b/project.js new file mode 100644 index 00000000..cc26dddc --- /dev/null +++ b/project.js @@ -0,0 +1,848 @@ +const express = require('express'); +const mysql = require('mysql'); +const app = express(); +const bodyParser = require('body-parser'); +app.use(bodyParser.urlencoded({ extended: true })); + +var db = mysql.createConnection({ + host: "localhost", + user: "root", + password: "", + database: "zoo", + port: "3306" +}); + + +app.use(function(req, res, next) { + if ("key" in req.query) { // if there is a key in the request we try to find it in the table if there is no key or the key is not in the table we send the error 403 + var key = req.query["key"]; + var query = "SELECT * FROM users WHERE apikey='" + key + "'"; + db.query(query, function(err, result, fields) { + if (err) throw err; + if (result.length > 0) { + next(); + } + else { + res.status(403).send("access denied").end(); + } + }); + } else { + res.status(403).send("access denied").end(); + } +}); + + + + +//animal +app.get('/animals', function(req, res) { + var query = "SELECT * FROM animals"; + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { //for each conditions + if (conditions[index] in req.query) { //if the column condition[index] exist in the req + if (query.indexOf("WHERE") < 0) { //if there already is a where add a AND else add a WHERE + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + //add the condition exemple name = 'rex' + req.query[conditions[index]] + "'"; + } + } + + if ("sort" in req.query) { //if the app request the answer to be sorted + var sort = req.query["sort"].split(","); // + query += " ORDER BY"; //add the ORDER BY to the query + for (var index in sort) { //for each field requested to be sorted by + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") //add the ASC or DSC depending on the sign + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + + if ("fields" in req.query) { //if the app request certain fields + query = query.replace("*", req.query["fields"]); //we replace the * in the query by the fields + } + + if ("limit" in req.query) { //if the app request a limit + query += " LIMIT " + req.query["limit"]; //we add the LIMIT to the quey + if ("offset" in req.query) { //if there is also an OFFSET + query += " OFFSET " + req.query["offset"]; //add the offset + } + } + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/animals/:id', function(req, res) { + var id = req.params.id; + var query = "SELECT * FROM animals WHERE id=" + id; + + + + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { //for each conditions + if (conditions[index] in req.query) { //if the column condition[index] exist in the req + if (query.indexOf("WHERE") < 0) { //if there already is a where add a AND else add a WHERE + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + //add the condition exemple name = 'rex' + req.query[conditions[index]] + "'"; + } + } + + + + if ("fields" in req.query) { //if the app request certain fields + query = query.replace("*", req.query["fields"]); //we replace the * in the query by the fields + } + + db.query(query, function(err, result, fields) { + if (err) throw err+query; + res.send(JSON.stringify(result)); + }); +}); + + +app.get('/animals/:id/cages', function(req, res) { //the app request the cage corresponding to the animal id selected + var id = req.params.id; + var query = "SELECT cages.id FROM animals JOIN cages ON animals.id_cage = cages.id WHERE animals.id=" + id; + + var conditions = ["name", "description","area"]; + for (var index in conditions) { //for each conditions + if (conditions[index] in req.query) { //if the column condition[index] exist in the req + if (query.indexOf("WHERE") < 0) { //if there already is a where add a AND else add a WHERE + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + //add the condition exemple name = 'rex' + req.query[conditions[index]] + "'"; + } + } + + if ("sort" in req.query) { //if the app request the answer to be sorted + var sort = req.query["sort"].split(","); // + query += " ORDER BY"; //add the ORDER BY to the query + for (var index in sort) { //for each field requested to be sorted by + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") //add the ASC or DSC depending on the sign + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + + if ("fields" in req.query) { //if the app request certain fields + query = query.replace("cages.id", req.query["fields"]); //we replace the * in the query by the fields + } + + if ("limit" in req.query) { //if the app request a limit + query += " LIMIT " + req.query["limit"]; //we add the LIMIT to the quey + if ("offset" in req.query) { //if there is also an OFFSET + query += " OFFSET " + req.query["offset"]; //add the offset + } + } + + + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + + + +app.get('/animals/:id_animal/cages/:id_cage', function(req, res) { //the app request the cage and animal corresponding to the animal id selected and cage id selected if they are related + var id_animal = req.params.id_animal; + var id_cage = req.params.id_cage; + var query = "SELECT cages.id FROM animals JOIN cages ON animals.id_cage = cages.id WHERE animals.id=" + id_animal + " AND cages.id=" + id_cage; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + + + +app.get('/animals/:id/food', function(req, res) { //the app request the food corresponding to the animal id selected + var id = req.params.id; + var query = "SELECT food.id FROM animals JOIN food ON animals.id = food.id_animal WHERE animals.id=" + id; + + var conditions = ["name", "id_animal","quantity"]; + + for (var index in conditions) { //for each conditions + if (conditions[index] in req.query) { //if the column condition[index] exist in the req + if (query.indexOf("WHERE") < 0) { //if there already is a where add a AND else add a WHERE + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + //add the condition exemple name = 'rex' + req.query[conditions[index]] + "'"; + } + } + + if ("sort" in req.query) { //if the app request the answer to be sorted + var sort = req.query["sort"].split(","); // + query += " ORDER BY"; //add the ORDER BY to the query + for (var index in sort) { //for each field requested to be sorted by + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") //add the ASC or DSC depending on the sign + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + + if ("fields" in req.query) { //if the app request certain fields + query = query.replace("cage.id", req.query["fields"]); //we replace the * in the query by the fields + } + + if ("limit" in req.query) { //if the app request a limit + query += " LIMIT " + req.query["limit"]; //we add the LIMIT to the quey + if ("offset" in req.query) { //if there is also an OFFSET + query += " OFFSET " + req.query["offset"]; //add the offset + } + } + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + + +app.get('/animals/:id_animal/food/:id_food', function(req, res) { //the app request the food and the animal corresponding to the animal id selected and food id selected if they are related + var id_animal = req.params.id_animal; + var id_food = req.params.id_food; + var query = "SELECT food.id FROM animals INNER JOIN food ON animals.id = food.id_animal WHERE animals.id=" + id_animal + " AND food.id=" + + id_food; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + + + +app.post('/animals', function(req, res) { + var name = req.body.name; //we get all the variable necessary to create an animal + var breed = req.body.breed; + var food_per_day = req.body.food_per_day; + var birthday = req.body.birthday; + var entry_date = req.body.entry_date; + var id_cage = req.body.id_cage; + var query = "INSERT INTO animals (name,breed,food_per_day,birthday,entry_date,id_cage) VALUES ('" +name+"','"+breed+"','"+food_per_day+"','"+birthday+"','"+entry_date+"','"+id_cage + "')"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("success")); + }); +}); + +app.put('/animals/:id', function(req, res) { + var id = req.params.id; + + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + var query="UPDATE animals "; + for (var index in conditions) { + if (conditions[index] in req.body) { + if (query.indexOf("SET") < 0) { + query += "SET "; + } else { + query += ", "; + } + query += " " + conditions[index] + " = '" + req.body[conditions[index]] + "' "; + } + + } + query += " WHERE id = '" + id + "'"; + + + //var query = "UPDATE animals SET name = '" + name + "', breed ='"+breed+"', food_per_day ='"+food_per_day+"', birthday ='"+birthday+"', entry_date ='"+entry_date+"', id_cage ='"+id_cage+"' WHERE id=" + //+ id; + db.query(query, function(err, result, fields) { + if (err) throw err+query; + res.send(JSON.stringify("Success")); + }); +}); + + + + +app.delete('/animals', function(req, res) { //delete all animals + var query = "DELETE FROM animals"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +app.delete('/animals/:id', function(req, res) { //delete the selected animal + var id = req.params.id; + var query = "DELETE FROM animals WHERE id=" + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +///cage work the same as animal +app.get('/cages', function(req, res) { + var query = "SELECT * FROM cages"; + + var conditions = ["name", "description","area"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/cages/:id', function(req, res) { + var id = req.params.id; + var query = "SELECT * FROM cages WHERE id=" + id; + + var conditions = ["name", "description","area"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + + + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + + + + + + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/cages/:id/animals', function(req, res) { + var id = req.params.id; + var query = "SELECT animals.id FROM cages JOIN animals ON cages.id = animals.id_cage WHERE cages.id=" + id; + + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { //for each conditions + if (conditions[index] in req.query) { //if the column condition[index] exist in the req + if (query.indexOf("WHERE") < 0) { //if there already is a where add a AND else add a WHERE + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + //add the condition exemple name = 'rex' + req.query[conditions[index]] + "'"; + } + } + + if ("sort" in req.query) { //if the app request the answer to be sorted + var sort = req.query["sort"].split(","); // + query += " ORDER BY"; //add the ORDER BY to the query + for (var index in sort) { //for each field requested to be sorted by + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") //add the ASC or DSC depending on the sign + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + + if ("fields" in req.query) { //if the app request certain fields + query = query.replace("animals.id", req.query["fields"]); //we replace the * in the query by the fields + } + + if ("limit" in req.query) { //if the app request a limit + query += " LIMIT " + req.query["limit"]; //we add the LIMIT to the quey + if ("offset" in req.query) { //if there is also an OFFSET + query += " OFFSET " + req.query["offset"]; //add the offset + } + } + + + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/cages/:id_cages/animals/:id_animals', function(req, res) { + var id_animal = req.params.id_animal; + var id_cage = req.params.id_cage; + var query = "SELECT animals.id FROM animals JOIN cages ON animals.id_cage = cages.id WHERE animals.id=" + id_animals + " AND cages.id=" + + id_cages + djkbh.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.post('/cages', function(req, res) { + var name = req.body.name; + var description = req.body.description; + var area = req.body.area; + + var query = "INSERT INTO cages (name,description,area) VALUES ('" +name+"','"+description+"','"+area+"')"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("success")); + }); +}); + +app.put('/cages/:id', function(req, res) { + + var id = req.params.id; + var conditions = ["name", "description","area"]; + var query="UPDATE cages "; + for (var index in conditions) { + if (conditions[index] in req.body) { + if (query.indexOf("SET") < 0) { + query += "SET "; + } else { + query += ", "; + } + query += " " + conditions[index] + " = '" + req.body[conditions[index]] + "' "; + } + + } + query += " WHERE id = '" + id + "'"; + db.query(query, function(err, result, fields) { + if (err) throw err+query; + res.send(JSON.stringify("Success")); + }); +}); + +app.delete('/cages', function(req, res) { + var query = "DELETE FROM cages"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +app.delete('/cages/:id', function(req, res) { + var id = req.params.id; + var query = "DELETE FROM cages WHERE id=" + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +//food +app.get('/food', function(req, res) { + var query = "SELECT * FROM food"; + + var conditions = ["name", "id_animal","quantity"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); +app.get('/food/:id', function(req, res) { + var id = req.params.id; + var query = "SELECT * FROM food WHERE id=" + id; + + var conditions = ["name", "id_animal","quantity"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/food/:id/animals', function(req, res) { + var id = req.params.id; + var query = "SELECT animals.id FROM food JOIN animals ON food.id_animal = animals.id WHERE food.id=" + id; + + + + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + + for (var index in conditions) { //for each conditions + if (conditions[index] in req.query) { //if the column condition[index] exist in the req + if (query.indexOf("WHERE") < 0) { //if there already is a where add a AND else add a WHERE + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + //add the condition exemple name = 'rex' + req.query[conditions[index]] + "'"; + } + } + + + + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + + if ("fields" in req.query) { + query = query.replace("animals.id", req.query["fields"]); + } + + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + + + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/food/:id_food/animals/:id_animal', function(req, res) { + var id_animal = req.params.id_animal; + var id_food = req.params.id_food; + var query = "SELECT animals.id FROM animals JOIN food ON animals.id = food.id_animal WHERE animals.id=" + id_animal + " AND food.id=" + + id_food; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.post('/food', function(req, res) { + var name = req.body.name; + var id_animal = req.body.id_animal; + var quantity = req.body.quantity; + + var query = "INSERT INTO food (name,id_animal,quantity) VALUES ('" +name+"','"+id_animal+"','"+quantity+"')"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("success")); + }); +}); + +app.put('/food/:id', function(req, res) { + + var id = req.params.id; + var conditions = ["name", "id_animal","quantity"]; + var query="UPDATE food "; + for (var index in conditions) { + if (conditions[index] in req.body) { + if (query.indexOf("SET") < 0) { + query += "SET "; + } else { + query += ", "; + } + query += " " + conditions[index] + " = '" + req.body[conditions[index]] + "' "; + } + + } + query += " WHERE id = '" + id + "'"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.delete('/food', function(req, res) { + var query = "DELETE FROM food"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +app.delete('/food/:id', function(req, res) { + var id = req.params.id; + var query = "DELETE FROM food WHERE id=" + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +//Staff + +app.get('/staff', function(req, res) { + var query = "SELECT * FROM staff"; + + var conditions = ["firstname", "lastname","wage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + + + + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); +app.get('/staff/:id', function(req, res) { + var id = req.params.id; + var query = "SELECT * FROM staff WHERE id=" + id; + + var conditions = ["firstname", "lastname","wage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.post('/staff', function(req, res) { + var firstname = req.body.firstname; + var lastname = req.body.lastname; + var wage = req.body.wage; + + var query = "INSERT INTO staff (firstname,lastname,wage) VALUES ('" +firstname+"','"+lastname+"','"+wage+"')"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("success")); + }); +}); + +app.put('/staff/:id', function(req, res) { + + var id = req.params.id; + var conditions = ["firstname", "lastname","wage"]; + var query="UPDATE staff "; + for (var index in conditions) { + if (conditions[index] in req.body) { + if (query.indexOf("SET") < 0) { + query += "SET "; + } else { + query += ", "; + } + query += " " + conditions[index] + " = '" + req.body[conditions[index]] + "' "; + } + + } + query += " WHERE id = '" + id + "'"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.delete('/staff', function(req, res) { + var query = "DELETE FROM staff"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +app.delete('/staff/:id', function(req, res) { + var id = req.params.id; + var query = "DELETE FROM staff WHERE id=" + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.get('/food-stats', function(req, res) { + //for each animal that as a food linked to it we need the id and the number of days of food there is left for him + // we add a case to avoid dividing by 0 + var query = "SELECT a.id as id, CASE WHEN a.food_per_day=0 THEN 0 ELSE f.quantity/a.food_per_day END as days_left FROM food f INNER JOIN animals a ON f.id_animal=a.id"; + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + + + + +app.listen(3000, function() { + db.connect(function(err) { + if (err) throw err; + console.log('Connection to database successful!'); + }); + console.log('Example app listening on port 3000!'); +}); + From 7ae666814fd2a05422f0b23f2c6098677627daa5 Mon Sep 17 00:00:00 2001 From: remyalline Date: Sat, 15 Dec 2018 23:00:54 +0100 Subject: [PATCH 3/4] project.js --- Dockerfile | 6 +++++ answers.md | 58 ++++++++++++++++++++++++++++++++-------------- docker-compose.yml | 5 ++++ project.js | 19 +++++++++++---- 4 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..1b24c97b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ + +FROM node:8.14.0-alpine +COPY . /root +RUN npm install express mysql +CMD node /root/project.js +EXPOSE 3002 diff --git a/answers.md b/answers.md index b7025b97..c484d54e 100644 --- a/answers.md +++ b/answers.md @@ -1,41 +1,63 @@ # Answers -Lastname: -Firstname: +Lastname:ALLINE +Firstname:Rémy ## 2.2 -command: +command:sudo docker run app ## 2.3 -question: -command: +question:The port is not open so it can' access it +command: sudo docker run -p 3000:3000 project.js ## 2.5 -question: -command: +question:the local image name have to match the same as the repository +command:sudo docker tag project.js remyalline/devops_lab + sudo docker push remyalline/devops_lab ## 2.6 -command: +command:sudo docker system prune -a -question: -command: +question: we pull the image with : sudo docker pull remyalline/devops_lab and after that we start a container with the same name as the one we pushed +command:sudo docker create remyalline/devops_lab -command: +command:sudo docker run --detach r/devops_lab ## 2.7 +question:we can see the all the containers with their id, names, status and when they were created +the status of the congtainer is up +for this we used the command: sudo docker ps -a +the name of the container is quizzical_kirch + question: -question: -command: +command:sudo docker rename quizzical_kirch application command: ## 2.8 -question: -output: +question:the OS from the container is Ubuntu +output:DISTRIB_ID=Ubuntu +DISTRIB_RELEASE=18.04 +DISTRIB_CODENAME=bionic +DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS" +NAME="Ubuntu" +VERSION="18.04.1 LTS (Bionic Beaver)" +ID=ubuntu +ID_LIKE=debian +PRETTY_NAME="Ubuntu 18.04.1 LTS" +VERSION_ID="18.04" +HOME_URL="https://www.ubuntu.com/" +SUPPORT_URL="https://help.ubuntu.com/" +BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" +PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" +VERSION_CODENAME=bionic +UBUNTU_CODENAME=bionic + ## 3.1 -command: +command:sudo docker-compose up ## 3.4 -command: -command: +command:sudo docker-compose up -d +command:sudo docker-compose logs + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..a5b1081b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,5 @@ +version: '3' +services: + my-service: + image: remyalline/devops_lab + diff --git a/project.js b/project.js index cc26dddc..379b81b1 100644 --- a/project.js +++ b/project.js @@ -1,15 +1,24 @@ + + + const express = require('express'); const mysql = require('mysql'); const app = express(); const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: true })); +const Thost = process.environment.MYSQL_HOST; +const Tport = process.environment.MYSQL_PORT; +const Tdatabase = process.environment.MYSQL_DATABASE; +const Tuser = process.environment.MYSQL_LOGIN; +const Tpassword = process.environment.MYSQL_PASSWORD; + var db = mysql.createConnection({ - host: "localhost", - user: "root", - password: "", - database: "zoo", - port: "3306" + host:Thost, + user: Tuser, + password: Tpassword, + database: Tdatabase, + port: Tport }); From 027d412b222f1da19af6fca796943707a20fce5b Mon Sep 17 00:00:00 2001 From: remyalline Date: Wed, 19 Dec 2018 13:19:02 +0100 Subject: [PATCH 4/4] project.js --- project.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/project.js b/project.js index 379b81b1..bd822d42 100644 --- a/project.js +++ b/project.js @@ -7,11 +7,11 @@ const app = express(); const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: true })); -const Thost = process.environment.MYSQL_HOST; -const Tport = process.environment.MYSQL_PORT; -const Tdatabase = process.environment.MYSQL_DATABASE; -const Tuser = process.environment.MYSQL_LOGIN; -const Tpassword = process.environment.MYSQL_PASSWORD; +const Thost = process.env.MYSQL_HOST; +const Tport = process.env.MYSQL_PORT; +const Tdatabase = process.env.MYSQL_DATABASE; +const Tuser = process.env.MYSQL_LOGIN; +const Tpassword = process.env.MYSQL_PASSWORD; var db = mysql.createConnection({ host:Thost, @@ -22,7 +22,7 @@ var db = mysql.createConnection({ }); -app.use(function(req, res, next) { +/*app.use(function(req, res, next) { if ("key" in req.query) { // if there is a key in the request we try to find it in the table if there is no key or the key is not in the table we send the error 403 var key = req.query["key"]; var query = "SELECT * FROM users WHERE apikey='" + key + "'"; @@ -38,7 +38,7 @@ app.use(function(req, res, next) { } else { res.status(403).send("access denied").end(); } -}); +});*/