Optimized and added logging

This commit is contained in:
Alexis
2023-04-28 22:43:12 +02:00
parent 9bfbc9e09e
commit 690ba0489f
3 changed files with 76 additions and 24 deletions

View File

@@ -1,7 +1,8 @@
const fs = require('fs')
const path = require('path')
const { createReadStream, readFileSync } = require('fs')
const { join } = require('path')
const express = require('express')
const cors = require('cors')
const morgan = require('morgan')
const json = require('big-json')
const GeoJsonGeometriesLookup = require("geojson-geometries-lookup")
@@ -9,13 +10,14 @@ const lookupTable = require('./rep-lookup')
const app = express()
const PORT = 3000
const port = 3000
// Add modules to express app
app.use(cors())
app.use(morgan('common'))
app.use(express.static(path.join(__dirname, 'public')))
// Static files
app.use(express.static(join(__dirname, 'public')))
/**
* MAIN ROUTE
@@ -26,13 +28,11 @@ app.use(express.static(path.join(__dirname, 'public')))
app.use('/rep', async (req, res) => {
let { lat, lon } = req.query
const missingRequiredQuery = !lat || !lon
// If the query doesn't contain the required params...
if (missingRequiredQuery) {
res.json({
if (!lat || !lon) {
res.status(401).json({
"message": "The request is missing geolocation data. You must use 'lat' and 'lon' in your query."
}).status(401).end();
});
return
}
@@ -43,13 +43,13 @@ app.use('/rep', async (req, res) => {
const closestCity = await (await fetch(
`https://geo.api.gouv.fr/communes?lat=${lat}&lon=${lon}`
)).json();
let codeDepartement = String(closestCity[0]?.codeDepartement)
let codeDepartement = String(closestCity[0].codeDepartement)
// If somehow, the gov api can't find the nearest city...
if (!codeDepartement || codeDepartement === "") {
res.json({
res.status(400).json({
"message": "The geolocation wasn't able to pinpoint the region the coords were in."
}).status(400).end();
});
return
}
@@ -57,8 +57,8 @@ app.use('/rep', async (req, res) => {
codeDepartement = codeDepartement.padStart(3, "0");
// Gets the associated features with a read and parse stream
const pathToFeatures = path.join(__dirname, `public/cirs/${codeDepartement}.json`)
const readStream = fs.createReadStream(pathToFeatures)
const pathToFeatures = join(__dirname, `public/cirs/${codeDepartement}.json`)
const readStream = createReadStream(pathToFeatures)
const parseStream = json.createParseStream()
parseStream.on('data', (pojo) => {
@@ -75,25 +75,25 @@ app.use('/rep', async (req, res) => {
// If no code is found, send a 404
if (!code) {
res.json({
res.status(404).json({
"message": "The requested data couldn't be found. Maybe it was moved or the data isn't generated properly."
}).status(404).end();
});
return
}
// If a code is found, send the associated file containing the representant data
const repFile = fs.readFileSync(path.join(__dirname, `public/reps/${lookupTable[code]}.json`), 'utf-8')
const repFile = readFileSync(join(__dirname, `public/reps/${lookupTable[code]}.json`), 'utf-8')
res.end(repFile)
})
readStream.pipe(parseStream);
})
app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
app.listen(port, () => console.log(`Server listening on port: ${port}`));
/**
* CUT DOWN CIRCO JSON
*/
// const f = fs.readFileSync(path.join(__dirname, 'public/circonscriptions-legislatives.json'), 'utf-8')
// const f = fs.readFileSync(join(__dirname, 'public/circonscriptions-legislatives.json'), 'utf-8')
// const data = JSON.parse(f)
// const features = Object.values(data.features)
@@ -106,7 +106,7 @@ app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
// "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
// "features": value
// }
// fs.writeFileSync(path.join(__dirname, `public/cirs/${key}.json`), JSON.stringify(output))
// fs.writeFileSync(join(__dirname, `public/cirs/${key}.json`), JSON.stringify(output))
// }
// function groupBy(list, keyGetter) {

53
package-lock.json generated
View File

@@ -9,7 +9,8 @@
"big-json": "^3.2.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"geojson-geometries-lookup": "^0.5.0"
"geojson-geometries-lookup": "^0.5.0",
"morgan": "^1.10.0"
}
},
"node_modules/@turf/bbox": {
@@ -118,6 +119,22 @@
"node": ">=0.8"
}
},
"node_modules/basic-auth": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
"dependencies": {
"safe-buffer": "5.1.2"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/basic-auth/node_modules/safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"node_modules/big-json": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/big-json/-/big-json-3.2.0.tgz",
@@ -554,6 +571,32 @@
"node": ">= 0.6"
}
},
"node_modules/morgan": {
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz",
"integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==",
"dependencies": {
"basic-auth": "~2.0.1",
"debug": "2.6.9",
"depd": "~2.0.0",
"on-finished": "~2.3.0",
"on-headers": "~1.0.2"
},
"engines": {
"node": ">= 0.8.0"
}
},
"node_modules/morgan/node_modules/on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
"integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==",
"dependencies": {
"ee-first": "1.1.1"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
@@ -594,6 +637,14 @@
"node": ">= 0.8"
}
},
"node_modules/on-headers": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",

View File

@@ -6,7 +6,8 @@
"big-json": "^3.2.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"geojson-geometries-lookup": "^0.5.0"
"geojson-geometries-lookup": "^0.5.0",
"morgan": "^1.10.0"
},
"scripts": {
"start": "node index.js"