From 13be6bcbd6d322a9e1e2ae382527f09299fd5895 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 8 Nov 2024 22:12:34 -0300 Subject: [PATCH] docker file configurado com o banco de dados --- backend/docker-compose.yml | 17 ++++ backend/package-lock.json | 81 +++++++++++++++---- backend/package.json | 8 +- .../middlewares/error-handling.ts | 23 ++++++ backend/src/infrastructure/utils/AppError.ts | 11 +++ backend/src/server.ts | 5 ++ backend/tsconfig.json | 12 ++- 7 files changed, 137 insertions(+), 20 deletions(-) create mode 100644 backend/docker-compose.yml create mode 100644 backend/src/infrastructure/middlewares/error-handling.ts create mode 100644 backend/src/infrastructure/utils/AppError.ts diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml new file mode 100644 index 00000000..28d159a4 --- /dev/null +++ b/backend/docker-compose.yml @@ -0,0 +1,17 @@ +version: "3.9" + +services: + postgres: + container_name: postgres-urbanize + image: "bitnami/postgresql:latest" + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: api-urbanize + ports: + - "5432:5432" + volumes: + - database:/var/lib/postgresql/data + +volumes: + database: \ No newline at end of file diff --git a/backend/package-lock.json b/backend/package-lock.json index 4cc4fb2e..e10e2789 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -10,10 +10,14 @@ "license": "ISC", "dependencies": { "backend": "file:", - "express": "^4.21.1" + "cors": "^2.8.5", + "express": "^4.21.0", + "express-async-errors": "^3.1.1", + "zod": "^3.23.8" }, "devDependencies": { - "@types/express": "^5.0.0", + "@types/cors": "^2.8.17", + "@types/express": "^4.17.21", "@types/node": "^22.9.0", "ts-node": "^10.9.2", "tsx": "^4.19.2", @@ -484,22 +488,31 @@ "@types/node": "*" } }, + "node_modules/@types/cors": { + "version": "2.8.17", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.17.tgz", + "integrity": "sha512-8CGDvrBj1zgo2qE+oS3pOCyYNqCPryMWY2bGfwA0dcfopWGgxs+78df0Rs3rc9THP4JkOhLsAa+15VdpAqkcUA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/express": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/@types/express/-/express-5.0.0.tgz", - "integrity": "sha512-DvZriSMehGHL1ZNLzi6MidnsDhUZM/x2pRdDIKdwbUNqqwHxMlRdkxtn6/EPKyqKpHqTl/4nRZsRNLpZxZRpPQ==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.21.tgz", + "integrity": "sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==", "dev": true, "dependencies": { "@types/body-parser": "*", - "@types/express-serve-static-core": "^5.0.0", + "@types/express-serve-static-core": "^4.17.33", "@types/qs": "*", "@types/serve-static": "*" } }, "node_modules/@types/express-serve-static-core": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-5.0.1.tgz", - "integrity": "sha512-CRICJIl0N5cXDONAdlTv5ShATZ4HEwk6kDDIW2/w9qOWKg+NU/5F8wYRWCrONad0/UKkloNSmmyN/wX4rtpbVA==", + "version": "4.19.6", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.19.6.tgz", + "integrity": "sha512-N4LZ2xG7DatVqhCZzOGb1Yi5lMbXSZcmdLDe9EzSndPV2HpWYWzRbaerl2n27irrm94EPpprqa8KpskPT085+A==", "dev": true, "dependencies": { "@types/node": "*", @@ -682,9 +695,9 @@ } }, "node_modules/cookie": { - "version": "0.7.1", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.1.tgz", - "integrity": "sha512-6DnInpx7SJ2AK3+CTUE/ZM0vWTUboZCegxhC2xiIydHR9jNuTAASBrfEpHhiGOZw/nX51bHt6YQl8jsGo4y/0w==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", "engines": { "node": ">= 0.6" } @@ -694,6 +707,18 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/create-require": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", @@ -835,16 +860,16 @@ } }, "node_modules/express": { - "version": "4.21.1", - "resolved": "https://registry.npmjs.org/express/-/express-4.21.1.tgz", - "integrity": "sha512-YSFlK1Ee0/GC8QaO91tHcDxJiE/X4FbpAyQWkxAvG6AXCuR65YzK8ua6D9hvi/TzUfZMpc+BwuM1IPw8fmQBiQ==", + "version": "4.21.0", + "resolved": "https://registry.npmjs.org/express/-/express-4.21.0.tgz", + "integrity": "sha512-VqcNGcj/Id5ZT1LZ/cfihi3ttTn+NJmkli2eZADigjq29qTlWi/hAQ43t/VLPq8+UX06FCEx3ByOYet6ZFblng==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", "body-parser": "1.20.3", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.7.1", + "cookie": "0.6.0", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", @@ -875,6 +900,14 @@ "node": ">= 0.10.0" } }, + "node_modules/express-async-errors": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/express-async-errors/-/express-async-errors-3.1.1.tgz", + "integrity": "sha512-h6aK1da4tpqWSbyCa3FxB/V6Ehd4EEB15zyQq9qe75OZBp0krinNKuH4rAY+S/U/2I36vdLAUFSjQJ+TFmODng==", + "peerDependencies": { + "express": "^4.16.2" + } + }, "node_modules/finalhandler": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.3.1.tgz", @@ -1127,6 +1160,14 @@ "node": ">= 0.6" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.13.2", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz", @@ -1478,6 +1519,14 @@ "engines": { "node": ">=6" } + }, + "node_modules/zod": { + "version": "3.23.8", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz", + "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } } } } diff --git a/backend/package.json b/backend/package.json index 6509f9aa..3d86223d 100644 --- a/backend/package.json +++ b/backend/package.json @@ -13,10 +13,14 @@ "license": "ISC", "dependencies": { "backend": "file:", - "express": "^4.21.1" + "cors": "^2.8.5", + "express": "^4.21.0", + "express-async-errors": "^3.1.1", + "zod": "^3.23.8" }, "devDependencies": { - "@types/express": "^5.0.0", + "@types/cors": "^2.8.17", + "@types/express": "^4.17.21", "@types/node": "^22.9.0", "ts-node": "^10.9.2", "tsx": "^4.19.2", diff --git a/backend/src/infrastructure/middlewares/error-handling.ts b/backend/src/infrastructure/middlewares/error-handling.ts new file mode 100644 index 00000000..d0059663 --- /dev/null +++ b/backend/src/infrastructure/middlewares/error-handling.ts @@ -0,0 +1,23 @@ +import { Request, Response, NextFunction } from 'express'; +import { ZodError } from "zod"; +import { AppError } from "../utils/AppError"; + +export function errorHandling( + error: any, + request: Request, + response: Response, + _: NextFunction +) { + // if the error was caused by me + if (error instanceof AppError) { + return response.status(error.statusCode).json({ message: error.message}) + } + // if the error was caused by Zod + if (error instanceof ZodError) { + return response + .status(400) + .json({ message: 'Validation error', issues: error.format() }) + } + + return response.status(500).json({ message: error.message }) +} \ No newline at end of file diff --git a/backend/src/infrastructure/utils/AppError.ts b/backend/src/infrastructure/utils/AppError.ts new file mode 100644 index 00000000..781ebd49 --- /dev/null +++ b/backend/src/infrastructure/utils/AppError.ts @@ -0,0 +1,11 @@ +class AppError { + message: string; + statusCode: number; + + constructor(message: string, statusCode: number = 400) { + this.message = message; + this.statusCode = statusCode; + } +} + +export { AppError } \ No newline at end of file diff --git a/backend/src/server.ts b/backend/src/server.ts index 99c9ab09..41a4cc7c 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -1,8 +1,13 @@ import express from 'express'; +import cors from 'cors'; +import 'express-async-errors' +import { errorHandling } from './infrastructure/middlewares/error-handling'; const app = express(); +app.use(cors()); app.use(express.json()); +app.use(errorHandling); const PORT = process.env.PORT || 3333; app.listen(PORT, () => { diff --git a/backend/tsconfig.json b/backend/tsconfig.json index 511e2434..68874d3a 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -1,10 +1,18 @@ { "compilerOptions": { - "target": "es2016", - "module": "commonjs", + "lib": ["ES2023"], + "target": "ES2022", + "outDir": "./build", + + "paths": { + "@/*": ["./src/*"] + }, + + "module": "node16", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, + "strict": true, "skipLibCheck": true }