Skip to content

Commit

Permalink
Merge pull request #1 from fga-eps-mds/173_organizar_gateway
Browse files Browse the repository at this point in the history
173 organizar gateway
  • Loading branch information
DaviMatheus authored Nov 15, 2023
2 parents 95ad500 + eab959f commit ce1e3c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
- name: user
url: http://localhost:8000
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "printgo-gateway",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -18,7 +19,11 @@
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^16.3.1",
"express": "^4.18.2"
"express": "^4.18.2",
"express-http-proxy": "^2.0.0",
"helmet": "^7.0.0",
"js-yaml": "^4.1.0",
"morgan": "^1.10.0"
},
"devDependencies": {
"eslint": "^8.52.0",
Expand Down
29 changes: 25 additions & 4 deletions src/gateway.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
const express = require('express');
const cors = require('cors');

import express from 'express';
import cors from 'cors';
import logger from 'morgan';
import helmet from 'helmet';
import httProxy from 'express-http-proxy'
import { resolve } from 'path'
import { readFileSync } from 'fs'
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})

app.use(express.json());

app.use(cors());
app.use(logger('start'))
app.use(helmet())
app.use(express.urlencoded({extended: true}))

app.get('/', (req, res) => {
return res.json({ message: 'Running Gateway' })
})

services.forEach(({ name, url }) => {
app.use(`/${name}`, httProxy(url, {timeout: 3000}))

});

const PORT = process.env.PORT || 4000;

const gateway = app.listen(PORT, () => {
console.log(`Gateway is running on ${PORT}`);
});

module.exports = { gateway, app };
export { gateway, app };

0 comments on commit ce1e3c9

Please sign in to comment.