From 1972390481f215c542c53272f2d04929132c01b9 Mon Sep 17 00:00:00 2001 From: DaviMatheus Date: Wed, 22 Nov 2023 18:44:11 -0300 Subject: [PATCH 1/2] adding_newprintergateway --- config.yml | 4 +++- nodemon.json | 6 ++++++ src/gateway.js | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 nodemon.json diff --git a/config.yml b/config.yml index c231c8c..922632b 100644 --- a/config.yml +++ b/config.yml @@ -1,3 +1,5 @@ services: - name: user - url: http://localhost:8000 \ No newline at end of file + url: ${USER_SERVICE_URL} + - name: printer + url: ${PRINTER_SERVICE_URL} \ No newline at end of file diff --git a/nodemon.json b/nodemon.json new file mode 100644 index 0000000..ae3ebcf --- /dev/null +++ b/nodemon.json @@ -0,0 +1,6 @@ +{ + "watch": ["./src", "."], + "ext": "js,yml", + "ignore": ["node_modules"], + "exec": "node src/gateway.js" + } \ No newline at end of file diff --git a/src/gateway.js b/src/gateway.js index adc7c57..ae9a490 100644 --- a/src/gateway.js +++ b/src/gateway.js @@ -6,8 +6,8 @@ import httProxy from 'express-http-proxy' import { resolve } from 'path' import { readFileSync } from 'fs' import { load } from 'js-yaml' -const app = express(); +const app = express(); const pathFile = resolve(process.cwd(), 'config.yml') const readConfig = readFileSync(pathFile, {encoding: 'utf8'}) const { services } = load(readConfig, {json: true}) From 5157b2c85ed388748c0c3c6dfe0f3f8c52bbdf26 Mon Sep 17 00:00:00 2001 From: Natanael Fernandes Date: Wed, 22 Nov 2023 22:06:36 -0300 Subject: [PATCH 2/2] :repeat: adicionando novos endpoints do api gateway Co-authored-by: DaviMatheus --- .eslintrc.js => .eslintrc.cjs | 9 ++++++++- .gitignore | 4 +++- README.md | 2 ++ babel.config.js | 6 ++++++ config.dev.yml | 7 +++++++ config.prod.yml | 7 +++++++ config.yml | 4 ++-- nodemon.json | 8 +++++--- package.json | 23 +++++++++++++++++++---- sonar-project.properties | 4 ++-- src/{gateway.js => gateway.ts} | 10 +++++++--- tsconfig.json | 14 ++++++++++++++ 12 files changed, 82 insertions(+), 16 deletions(-) rename .eslintrc.js => .eslintrc.cjs (70%) create mode 100644 babel.config.js create mode 100644 config.dev.yml create mode 100644 config.prod.yml rename src/{gateway.js => gateway.ts} (77%) create mode 100644 tsconfig.json diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 70% rename from .eslintrc.js rename to .eslintrc.cjs index 791d970..c706c0f 100644 --- a/.eslintrc.js +++ b/.eslintrc.cjs @@ -3,7 +3,10 @@ module.exports = { "es2021": true, "node": true }, - "extends": "eslint:recommended", + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended" + ], "overrides": [ { "env": { @@ -17,10 +20,14 @@ module.exports = { } } ], + "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaVersion": "latest", "sourceType": "module" }, + "plugins": [ + "@typescript-eslint" + ], "rules": { } } diff --git a/.gitignore b/.gitignore index d734e7b..9e46eef 100644 --- a/.gitignore +++ b/.gitignore @@ -145,4 +145,6 @@ dist package-lock.json yarn.lock -reports/* \ No newline at end of file +reports/* + +config.yml \ No newline at end of file diff --git a/README.md b/README.md index a2488b4..929fbb7 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ ## Quality Control +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=fga-eps-mds_2023.2-PrintGo-PrinterService&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=fga-eps-mds_2023.2-PrintGo-PrinterService) + ## Sobre diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 0000000..a50f080 --- /dev/null +++ b/babel.config.js @@ -0,0 +1,6 @@ +module.exports = { + presets: [ + ['@babel/preset-env', {targets: {node: 'current'}}], + '@babel/preset-typescript', + ], + }; \ No newline at end of file diff --git a/config.dev.yml b/config.dev.yml new file mode 100644 index 0000000..0ac9894 --- /dev/null +++ b/config.dev.yml @@ -0,0 +1,7 @@ +services: + - name: user + url: http://localhost:8000 + - name: printer + url: http://localhost:8001 + - name: schedular + url: 2023-1-schedula-gerenciador-de-localidades.vercel.app \ No newline at end of file diff --git a/config.prod.yml b/config.prod.yml new file mode 100644 index 0000000..0ac9894 --- /dev/null +++ b/config.prod.yml @@ -0,0 +1,7 @@ +services: + - name: user + url: http://localhost:8000 + - name: printer + url: http://localhost:8001 + - name: schedular + url: 2023-1-schedula-gerenciador-de-localidades.vercel.app \ No newline at end of file diff --git a/config.yml b/config.yml index 922632b..1a1b97a 100644 --- a/config.yml +++ b/config.yml @@ -1,5 +1,5 @@ services: - name: user - url: ${USER_SERVICE_URL} + url: http://localhost:8000 - name: printer - url: ${PRINTER_SERVICE_URL} \ No newline at end of file + url: http://localhost:8001 \ No newline at end of file diff --git a/nodemon.json b/nodemon.json index ae3ebcf..71f74ea 100644 --- a/nodemon.json +++ b/nodemon.json @@ -1,6 +1,8 @@ { "watch": ["./src", "."], - "ext": "js,yml", - "ignore": ["node_modules"], - "exec": "node src/gateway.js" + "ext": ".ts,.js,.yml", + "ignore": [], + "exec": "ts-node ./src/gateway.ts", + "legacyWatch": true + } \ No newline at end of file diff --git a/package.json b/package.json index 5652d8b..e42bc9e 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,10 @@ { "name": "printgo-gateway", "version": "1.0.0", - "type": "module", "description": "", "main": "index.js", "scripts": { - "start": "nodemon src/gateway.js", + "start": "nodemon", "test": "jest --passWithNoTests --no-cache --runInBand", "test:all": "CI=true npm run test -- --coverage", "lint": "eslint --ext .ts", @@ -15,6 +14,10 @@ "author": "", "license": "ISC", "dependencies": { + "@types/cors": "^2.8.17", + "@types/express-http-proxy": "^1.6.6", + "@types/js-yaml": "^4.0.9", + "@types/morgan": "^1.9.9", "axios": "^1.6.0", "cors": "^2.8.5", "cross-env": "^7.0.3", @@ -26,10 +29,22 @@ "morgan": "^1.10.0" }, "devDependencies": { - "eslint": "^8.52.0", + "@babel/cli": "^7.23.4", + "@babel/core": "^7.23.3", + "@babel/preset-env": "^7.23.3", + "@babel/preset-typescript": "^7.23.3", + "@types/express": "^4.17.21", + "@types/jest": "^29.5.10", + "@types/node": "^20.9.4", + "@typescript-eslint/eslint-plugin": "^6.12.0", + "@typescript-eslint/parser": "^6.12.0", + "eslint": "^8.54.0", "jest-mock-extended": "^3.0.5", "jest-sonar": "^0.2.16", "jest-sonar-reporter": "^2.0.0", - "nodemon": "^3.0.1" + "nodemon": "^3.0.1", + "ts-node": "^10.9.1", + "ts-node-dev": "^2.0.0", + "typescript": "^5.3.2" } } diff --git a/sonar-project.properties b/sonar-project.properties index 54e1248..24671f8 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -3,9 +3,9 @@ sonar.organization=fga-eps-mds-1 sonar.sources=src sonar.tests=tests -sonar.language=js +sonar.language=ts -sonar.test.inclusions=**/*.spec.jsx,**/*.spec.js, **/*.test.js +sonar.test.inclusions=**/*.spec.tsx,**/*.spec.ts, **/*.test.ts sonar.exclusions=node_modules/**, test/**, assets/**, .github/**, scripts/** diff --git a/src/gateway.js b/src/gateway.ts similarity index 77% rename from src/gateway.js rename to src/gateway.ts index ae9a490..9e9ba1b 100644 --- a/src/gateway.js +++ b/src/gateway.ts @@ -10,7 +10,9 @@ import { load } from 'js-yaml' const app = express(); const pathFile = resolve(process.cwd(), 'config.yml') const readConfig = readFileSync(pathFile, {encoding: 'utf8'}) -const { services } = load(readConfig, {json: true}) + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const {services}: any= load(readConfig, {json: true}) app.use(express.json()); @@ -23,8 +25,10 @@ app.get('/', (req, res) => { return res.json({ message: 'Running Gateway' }) }) -services.forEach(({ name, url }) => { - app.use(`/${name}`, httProxy(url, {timeout: 3000})) +services.forEach(({ url }) => { + console.log(url); + + app.use("/", httProxy(url, {timeout: 5000})) }); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f928171 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "module": "NodeNext", /* Specify what module code is generated. */ + "rootDir": "./src", /* Specify the root folder within your source files. */ + "outDir": "./dist", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + "strict": false, /* Enable all strict type-checking options. */ + "skipLibCheck": true, + + }, +}