Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/.git
**/node_modules
**/fastAPI
**/src/Deployments
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:20

WORKDIR /safemapsbackend

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "fetchCrimeData.js"]
51 changes: 47 additions & 4 deletions fetchCrimeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ const express = require("express");
const axios = require("axios");
const bodyParser = require("body-parser");
const cors = require("cors");
const objectHash = require("object-hash");

const ResponseStatus = require("./ResponseStatus");
const Response = require("./Response");
const ResponseUtils = require("./ResponseUtils");
const {
initializeRedisClient,
readData,
writeData,
} = require("./src/middleware/redis");

const app = express();
app.use(cors());
Expand All @@ -24,10 +30,10 @@ class FetchCrimePostBody {
}
}

console.log("Setting up API for post")
app.post("/fetchCrimeData", async (req, res) => {
try {
const { latitude, longitude, radius } = req.body;
console.log(req.body);

if (!latitude || !longitude) {
ResponseUtils.setResponseError(
Expand All @@ -40,6 +46,21 @@ app.post("/fetchCrimeData", async (req, res) => {

const userLocation = new FetchCrimePostBody(latitude, longitude, radius);

const cacheKey = objectHash.sha1([
userLocation.latitude,
userLocation.longitude,
userLocation.radius,
]);
console.log('Cached key' + cacheKey)
cachedValue = await readData(cacheKey);
console.log('Cached value found: ' + cachedValue)
if (cachedValue) {
// No need to call API as we have values in cache and can skip it.
const responseStatus = new ResponseStatus(200, "OK", "Success");
res.json(new Response(cachedValue, responseStatus));
return;
}

const apiUrl =
"https://spotcrime.com/map?lat=${userLocation.latitude}&lon=${userLocation.longitude}";
const response = await axios.get(apiUrl);
Expand All @@ -55,6 +76,9 @@ app.post("/fetchCrimeData", async (req, res) => {
);
if (crimeDataResponse.status == 200) {
const responseStatus = new ResponseStatus(200, "OK", "Success");

console.log("Saving in cache" + cacheKey + ", value= " + crimeDataResponse.data.crimes)
writeData(cacheKey, crimeDataResponse.data.crimes);
res.json(new Response(crimeDataResponse.data.crimes, responseStatus));
} else {
ResponseUtils.setResponseError(
Expand All @@ -77,6 +101,25 @@ app.post("/fetchCrimeData", async (req, res) => {
}
});

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
// app.listen(port, () => {
// console.log(`Server is running on http://localhost:${port}`);
// });

async function initializeExpressServer() {
//initialize an Express application
app.use(express.json());

//connect to Redis
if (process.env.REDIS_URL) {
await initializeRedisClient();
}

// start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
}

initializeExpressServer()
.then()
.catch((e) => console.error(e));
121 changes: 119 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"axios": "^1.6.7",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2"
"express": "^4.18.2",
"object-hash": "^3.0.0",
"redis": "^4.6.13"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -21,5 +23,8 @@
"bugs": {
"url": "https://github.com/T-I-P/SafeMapsBackend/issues"
},
"homepage": "https://github.com/T-I-P/SafeMapsBackend#readme"
"homepage": "https://github.com/T-I-P/SafeMapsBackend#readme",
"devDependencies": {
"prettier": "3.2.5"
}
}
Loading