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 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 getConsultaCep = async (req, res) => {
|
||||
const { cep: rawCep, numero: rawNumero } = req.query;
|
||||
if (!rawCep) return res.status(400).json({ error: "cep é obrigatório" });
|
||||
const cep = String(rawCep).trim().replace(/\D/g, "");
|
||||
@ -27,13 +12,7 @@ function createApp() {
|
||||
|
||||
try {
|
||||
const viaCepUrl = 'https://api.cep.rest/';
|
||||
let cepRestData = await fetch(viaCepUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ cep }) })
|
||||
.then(response => response.json());
|
||||
let cepRestData = await fetchCepJson(viaCepUrl, cep);
|
||||
// ViaCEP com retry e backoff (corrige shadowing e trata exceções)
|
||||
|
||||
const maxAttempts = 5;
|
||||
@ -43,13 +22,7 @@ function createApp() {
|
||||
if (!cepRestData || cepRestData.erro) {
|
||||
while (attempt < maxAttempts) {
|
||||
try {
|
||||
cepRestData = await fetch(viaCepUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ cep })
|
||||
}).then(response => response.json());
|
||||
cepRestData = await fetchCepJson(viaCepUrl, cep);
|
||||
if (cepRestData && !cepRestData.erro) break;
|
||||
console.info(`[INFO] ViaCEP retornou erro ou vazio na tentativa ${attempt + 1} para CEP ${cep}`);
|
||||
} catch (e) {
|
||||
@ -132,9 +105,8 @@ function createApp() {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "erro na consulta" });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
module.exports = createApp;
|
||||
module.exports = {
|
||||
getConsultaCep
|
||||
};
|
||||
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