diff --git a/.gitignore b/.gitignore
index c8018b3..d1cfd7b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
-.mysql
\ No newline at end of file
+.mysql
+node_modules
\ No newline at end of file
diff --git a/default.conf b/default.conf
index baecf73..9563dce 100644
--- a/default.conf
+++ b/default.conf
@@ -1,10 +1,16 @@
server {
- listen 80;
- listen [::]:80;
- server_name localhost;
+ listen 80;
+ # listen [::]:80;
+ server_name fullcycle-node;
#access_log /var/log/nginx/host.access.log main;
+ location = /favicon.ico {
+ return 204;
+ access_log off;
+ log_not_found off;
+ }
+
location / {
proxy_pass http://fullcycle-node:3000;
}
@@ -13,10 +19,10 @@ server {
# redirect server error pages to the static page /50x.html
#
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root /usr/share/nginx/html;
- }
+ # error_page 500 502 503 504 /50x.html;
+ # location = /50x.html {
+ # root /usr/share/nginx/html;
+ # }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 7c207fc..31c8ece 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -4,7 +4,7 @@ services:
node:
image: node:20-alpine
container_name: fullcycle-node
- command: npm run start
+ command: sh -c "npm install && npm start"
volumes:
- .:/app
working_dir: /app
@@ -18,8 +18,6 @@ services:
container_name: fullcycle-nginx
ports:
- "8080:80"
- expose:
- - "80"
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
depends_on:
@@ -32,8 +30,7 @@ services:
- "3306:3306"
volumes:
- ./.mysql:/var/lib/mysql
- - bootstrap.sql:/docker-entrypoint-initdb.d/1.sql
+ - ./bootstrap.sql:/docker-entrypoint-initdb.d/1.sql
environment:
- MYSQL_USER: "root"
- MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
+ MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: fullcycle
\ No newline at end of file
diff --git a/index.js b/index.js
index 457d845..1d8400a 100644
--- a/index.js
+++ b/index.js
@@ -1,12 +1,51 @@
-const http = require("node:http");
+import http from "node:http";
+import mysql from "mysql2/promise";
+import { faker } from "@faker-js/faker";
const hostname = "0.0.0.0";
const port = 3000;
-const server = http.createServer((req, res) => {
+const connection = await mysql.createConnection({
+ host: "fullcycle-mysql",
+ user: "root",
+ password: "root",
+ database: "fullcycle",
+});
+
+const server = http.createServer(async (req, res) => {
+ connection.connect();
+
+ let response = `
+
Fullcycle Rocks!
+ People:
+
+ `;
+
+ try {
+ const name = faker.person.fullName();
+
+ await connection.query(
+ `INSERT INTO people (name) VALUES ('${name.replace(/'/g, "\\'")}')`
+ );
+
+ console.log(`Person name ${name} inserted!`);
+ } catch (error) {
+ console.error("Error:", error);
+ }
+
+ try {
+ const [results] = await connection.query("SELECT * FROM people");
+
+ results.forEach((person) => (response += `- ${person.name}
`));
+ } catch (error) {
+ console.error("Error:", error);
+ }
+
+ response += "
";
+
res.statusCode = 200;
- res.setHeader("Content-Type", "text/plain");
- res.end("Fullcycle Rocks!
");
+ res.setHeader("Content-Type", "text/html");
+ res.end(response);
});
server.listen(port, hostname, () => {
diff --git a/package-lock.json b/package-lock.json
index 27398b6..67f75ee 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -7,7 +7,126 @@
"": {
"name": "desafio-proxy-fullcycle",
"version": "1.0.0",
- "license": "MIT"
+ "license": "MIT",
+ "dependencies": {
+ "@faker-js/faker": "^8.4.1",
+ "mysql2": "^3.9.1"
+ }
+ },
+ "node_modules/@faker-js/faker": {
+ "version": "8.4.1",
+ "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.4.1.tgz",
+ "integrity": "sha512-XQ3cU+Q8Uqmrbf2e0cIC/QN43sTBSC8KF12u29Mb47tWrt2hAgBXSgpZMj4Ao8Uk0iJcU99QsOCaIL8934obCg==",
+ "funding": [
+ {
+ "type": "opencollective",
+ "url": "https://opencollective.com/fakerjs"
+ }
+ ],
+ "engines": {
+ "node": "^14.17.0 || ^16.13.0 || >=18.0.0",
+ "npm": ">=6.14.13"
+ }
+ },
+ "node_modules/denque": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz",
+ "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==",
+ "engines": {
+ "node": ">=0.10"
+ }
+ },
+ "node_modules/generate-function": {
+ "version": "2.3.1",
+ "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz",
+ "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==",
+ "dependencies": {
+ "is-property": "^1.0.2"
+ }
+ },
+ "node_modules/iconv-lite": {
+ "version": "0.6.3",
+ "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
+ "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
+ "dependencies": {
+ "safer-buffer": ">= 2.1.2 < 3.0.0"
+ },
+ "engines": {
+ "node": ">=0.10.0"
+ }
+ },
+ "node_modules/is-property": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz",
+ "integrity": "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g=="
+ },
+ "node_modules/long": {
+ "version": "5.2.3",
+ "resolved": "https://registry.npmjs.org/long/-/long-5.2.3.tgz",
+ "integrity": "sha512-lcHwpNoggQTObv5apGNCTdJrO69eHOZMi4BNC+rTLER8iHAqGrUVeLh/irVIM7zTw2bOXA8T6uNPeujwOLg/2Q=="
+ },
+ "node_modules/lru-cache": {
+ "version": "8.0.5",
+ "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-8.0.5.tgz",
+ "integrity": "sha512-MhWWlVnuab1RG5/zMRRcVGXZLCXrZTgfwMikgzCegsPnG62yDQo5JnqKkrK4jO5iKqDAZGItAqN5CtKBCBWRUA==",
+ "engines": {
+ "node": ">=16.14"
+ }
+ },
+ "node_modules/mysql2": {
+ "version": "3.9.1",
+ "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.9.1.tgz",
+ "integrity": "sha512-3njoWAAhGBYy0tWBabqUQcLtczZUxrmmtc2vszQUekg3kTJyZ5/IeLC3Fo04u6y6Iy5Sba7pIIa2P/gs8D3ZeQ==",
+ "dependencies": {
+ "denque": "^2.1.0",
+ "generate-function": "^2.3.1",
+ "iconv-lite": "^0.6.3",
+ "long": "^5.2.1",
+ "lru-cache": "^8.0.0",
+ "named-placeholders": "^1.1.3",
+ "seq-queue": "^0.0.5",
+ "sqlstring": "^2.3.2"
+ },
+ "engines": {
+ "node": ">= 8.0"
+ }
+ },
+ "node_modules/named-placeholders": {
+ "version": "1.1.3",
+ "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.3.tgz",
+ "integrity": "sha512-eLoBxg6wE/rZkJPhU/xRX1WTpkFEwDJEN96oxFrTsqBdbT5ec295Q+CoHrL9IT0DipqKhmGcaZmwOt8OON5x1w==",
+ "dependencies": {
+ "lru-cache": "^7.14.1"
+ },
+ "engines": {
+ "node": ">=12.0.0"
+ }
+ },
+ "node_modules/named-placeholders/node_modules/lru-cache": {
+ "version": "7.18.3",
+ "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.18.3.tgz",
+ "integrity": "sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==",
+ "engines": {
+ "node": ">=12"
+ }
+ },
+ "node_modules/safer-buffer": {
+ "version": "2.1.2",
+ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
+ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
+ },
+ "node_modules/seq-queue": {
+ "version": "0.0.5",
+ "resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz",
+ "integrity": "sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q=="
+ },
+ "node_modules/sqlstring": {
+ "version": "2.3.3",
+ "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.3.tgz",
+ "integrity": "sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==",
+ "engines": {
+ "node": ">= 0.6"
+ }
}
}
}
diff --git a/package.json b/package.json
index 657ece3..ca163ac 100644
--- a/package.json
+++ b/package.json
@@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
+ "type": "module",
"scripts": {
"start": "node index.js"
},
@@ -15,5 +16,9 @@
"bugs": {
"url": "https://github.com/diegosurita/desafio-proxy-fullcycle/issues"
},
- "homepage": "https://github.com/diegosurita/desafio-proxy-fullcycle#readme"
+ "homepage": "https://github.com/diegosurita/desafio-proxy-fullcycle#readme",
+ "dependencies": {
+ "@faker-js/faker": "^8.4.1",
+ "mysql2": "^3.9.1"
+ }
}