Skip to content

Commit

Permalink
feat: add name insertion feature on people table and some other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosurita committed Feb 10, 2024
1 parent 5b557e6 commit 6aae72d
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.mysql
.mysql
node_modules
20 changes: 13 additions & 7 deletions default.conf
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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
#
Expand Down
9 changes: 3 additions & 6 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
47 changes: 43 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 = `
<h1>Fullcycle Rocks!</h1>
<h3>People:</h3>
<ul>
`;

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 += `<li>${person.name}</li>`));
} catch (error) {
console.error("Error:", error);
}

response += "</ul>";

res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("<h1>Fullcycle Rocks!</h1>");
res.setHeader("Content-Type", "text/html");
res.end(response);
});

server.listen(port, hostname, () => {
Expand Down
121 changes: 120 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js"
},
Expand All @@ -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"
}
}

0 comments on commit 6aae72d

Please sign in to comment.