REFACTOR: Ajustando arquitetura de pastas e refatorando o projeto para deixar app mais enxuto e deixar lógica nos Controllers e Services.
This commit is contained in:
parent
e3c2352276
commit
1d2636926e
@ -1,15 +0,0 @@
|
|||||||
const axios = require('axios');
|
|
||||||
|
|
||||||
async function fetchJson(url, opts = {}) {
|
|
||||||
try {
|
|
||||||
const r = await axios.get(url, { timeout: 10000, ...opts });
|
|
||||||
return r.data;
|
|
||||||
} catch (e) {
|
|
||||||
console.warn(`fetchJson error ${url}: ${e.message}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
fetchJson
|
|
||||||
};
|
|
||||||
20
src/app.js
Normal file
20
src/app.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
const dotenv = require("dotenv");
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
const express = require("express");
|
||||||
|
const cors = require("cors");
|
||||||
|
const router = require("./routes");
|
||||||
|
|
||||||
|
function createApp() {
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use(express.json());
|
||||||
|
app.use(router);
|
||||||
|
app.use(cors());
|
||||||
|
|
||||||
|
return app;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createApp;
|
||||||
@ -1,24 +1,9 @@
|
|||||||
const dotenv = require("dotenv");
|
const { fetchCepJson } = require("../services/cepService");
|
||||||
|
const { getMinDistance } = require("../services/distanceService");
|
||||||
|
const { geocodeWithGoogle } = require("../services/geocodeService");
|
||||||
|
const { insertConsultaData } = require("../models/databaseModel");
|
||||||
|
|
||||||
dotenv.config();
|
const getConsultaCep = async (req, res) => {
|
||||||
|
|
||||||
const express = require("express");
|
|
||||||
const path = require("path");
|
|
||||||
const cors = require("cors");
|
|
||||||
const { fetchJson } = require("./services/jsonService");
|
|
||||||
const { getMinDistance } = require("./services/distanceService");
|
|
||||||
const { geocodeWithGoogle } = require("./services/geocodeService");
|
|
||||||
const { insertConsultaData } = require("./models/databaseModel");
|
|
||||||
|
|
||||||
function createApp() {
|
|
||||||
const app = express();
|
|
||||||
|
|
||||||
app.use(cors());
|
|
||||||
app.use(express.static(path.join(__dirname, "public")));
|
|
||||||
app.use(express.json());
|
|
||||||
|
|
||||||
// manual CEP+Numero query: resolves ViaCEP -> Nominatim -> Geogrid
|
|
||||||
app.get("/consulta-cep", async (req, res) => {
|
|
||||||
const { cep: rawCep, numero: rawNumero } = req.query;
|
const { cep: rawCep, numero: rawNumero } = req.query;
|
||||||
if (!rawCep) return res.status(400).json({ error: "cep é obrigatório" });
|
if (!rawCep) return res.status(400).json({ error: "cep é obrigatório" });
|
||||||
const cep = String(rawCep).trim().replace(/\D/g, "");
|
const cep = String(rawCep).trim().replace(/\D/g, "");
|
||||||
@ -27,13 +12,7 @@ function createApp() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const viaCepUrl = 'https://api.cep.rest/';
|
const viaCepUrl = 'https://api.cep.rest/';
|
||||||
let cepRestData = await fetch(viaCepUrl, {
|
let cepRestData = await fetchCepJson(viaCepUrl, cep);
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ cep }) })
|
|
||||||
.then(response => response.json());
|
|
||||||
// ViaCEP com retry e backoff (corrige shadowing e trata exceções)
|
// ViaCEP com retry e backoff (corrige shadowing e trata exceções)
|
||||||
|
|
||||||
const maxAttempts = 5;
|
const maxAttempts = 5;
|
||||||
@ -43,13 +22,7 @@ function createApp() {
|
|||||||
if (!cepRestData || cepRestData.erro) {
|
if (!cepRestData || cepRestData.erro) {
|
||||||
while (attempt < maxAttempts) {
|
while (attempt < maxAttempts) {
|
||||||
try {
|
try {
|
||||||
cepRestData = await fetch(viaCepUrl, {
|
cepRestData = await fetchCepJson(viaCepUrl, cep);
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ cep })
|
|
||||||
}).then(response => response.json());
|
|
||||||
if (cepRestData && !cepRestData.erro) break;
|
if (cepRestData && !cepRestData.erro) break;
|
||||||
console.info(`[INFO] ViaCEP retornou erro ou vazio na tentativa ${attempt + 1} para CEP ${cep}`);
|
console.info(`[INFO] ViaCEP retornou erro ou vazio na tentativa ${attempt + 1} para CEP ${cep}`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -132,9 +105,8 @@ function createApp() {
|
|||||||
console.error(err);
|
console.error(err);
|
||||||
return res.status(500).json({ error: "erro na consulta" });
|
return res.status(500).json({ error: "erro na consulta" });
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
return app;
|
module.exports = {
|
||||||
}
|
getConsultaCep
|
||||||
|
};
|
||||||
module.exports = createApp;
|
|
||||||
11
src/routes.js
Normal file
11
src/routes.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const express = require("express");
|
||||||
|
const consultaCepController = require("./controllers/consultaCepController");
|
||||||
|
const path = require("path");
|
||||||
|
const { Router } = express;
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.use(express.static(path.join(__dirname, "..", "public")));
|
||||||
|
router.get('/consulta-cep', consultaCepController.getConsultaCep);
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
|
||||||
21
src/services/cepService.js
Normal file
21
src/services/cepService.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
|
||||||
|
async function fetchCepJson(url, cep) {
|
||||||
|
try {
|
||||||
|
const r = await fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ cep }) })
|
||||||
|
.then(response => response.json());
|
||||||
|
return r;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[ERROR] Falha ao buscar JSON do CEP:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
fetchCepJson
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user