2019-09-12 06:23:03 +02:00
|
|
|
const express = require("express");
|
2019-01-12 14:09:17 +01:00
|
|
|
const path = require("path");
|
|
|
|
|
const bodyParser = require("body-parser");
|
2019-09-04 07:00:27 -07:00
|
|
|
const layout = require("express-layout");
|
2019-09-12 06:23:03 +02:00
|
|
|
const compression = require("compression");
|
2019-10-16 12:49:58 +02:00
|
|
|
const forceSSL = require("./app/helpers/forceSSL");
|
2019-09-12 06:23:03 +02:00
|
|
|
|
2020-02-13 17:08:49 +01:00
|
|
|
const { Storage } = require("@google-cloud/storage");
|
2020-02-12 11:44:56 +01:00
|
|
|
const validate = require("validate.js");
|
2020-02-18 21:40:40 +01:00
|
|
|
const cors = require("cors");
|
2020-02-12 11:44:56 +01:00
|
|
|
|
2019-09-26 23:55:34 +02:00
|
|
|
const {
|
|
|
|
|
APP_PORT,
|
|
|
|
|
CRAWLER_INTERVAL,
|
2020-01-24 16:04:28 +01:00
|
|
|
STOP_CRAWLER
|
2019-09-26 23:55:34 +02:00
|
|
|
} = require("./app/config/appConfig");
|
2019-09-12 06:23:03 +02:00
|
|
|
const routes = require("./app/routes");
|
2019-09-26 17:30:06 +02:00
|
|
|
const { crawlAll } = require("./app/crawler/crawl");
|
2020-01-24 01:01:10 +01:00
|
|
|
const { checkUpNotify } = require("./app/services/notificationService");
|
2019-09-30 13:53:35 +02:00
|
|
|
const {
|
|
|
|
|
notifyForNewRealEstates
|
|
|
|
|
} = require("./app/services/notificationService");
|
2019-01-12 14:09:17 +01:00
|
|
|
|
|
|
|
|
const app = express();
|
2019-09-12 06:23:03 +02:00
|
|
|
|
2020-02-18 23:49:00 +01:00
|
|
|
app.use(cors());
|
|
|
|
|
|
2019-10-16 12:49:58 +02:00
|
|
|
app.use(forceSSL());
|
2019-01-19 16:53:27 +01:00
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2019-01-19 19:20:21 +01:00
|
|
|
|
2019-09-04 07:00:27 -07:00
|
|
|
app.set("views", path.join(__dirname, "/app/views"));
|
|
|
|
|
app.set("view engine", "ejs");
|
2019-04-09 06:01:21 +02:00
|
|
|
app.use(layout());
|
2019-03-26 05:09:53 +01:00
|
|
|
|
2019-04-13 10:38:25 +02:00
|
|
|
app.use(compression());
|
2019-09-12 06:23:03 +02:00
|
|
|
app.use("/", routes);
|
2019-09-04 07:00:27 -07:00
|
|
|
|
|
|
|
|
app.use("/assets", express.static("./app/public"));
|
2019-03-13 05:43:48 +01:00
|
|
|
|
2019-09-05 14:24:29 +02:00
|
|
|
app.listen(APP_PORT, () =>
|
|
|
|
|
console.log(`Example app listening on port ${APP_PORT}!`)
|
|
|
|
|
);
|
2019-09-26 17:30:06 +02:00
|
|
|
|
2019-09-26 23:55:34 +02:00
|
|
|
let crawlerRunning = STOP_CRAWLER;
|
2019-09-26 17:30:06 +02:00
|
|
|
const crawl = () => {
|
|
|
|
|
if (!crawlerRunning) {
|
|
|
|
|
crawlerRunning = true;
|
|
|
|
|
crawlAll().then(newRealEstates => {
|
|
|
|
|
crawlerRunning = false;
|
2019-09-30 13:53:35 +02:00
|
|
|
notifyForNewRealEstates(newRealEstates);
|
2019-09-26 17:30:06 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-01-24 01:01:10 +01:00
|
|
|
|
2019-09-26 17:30:06 +02:00
|
|
|
setInterval(crawl, CRAWLER_INTERVAL * 1000);
|
2020-01-24 01:01:10 +01:00
|
|
|
|
2020-01-24 16:04:28 +01:00
|
|
|
setInterval(checkUpNotify, 1000 * 60 * 60 * 24);
|
2020-02-13 17:08:49 +01:00
|
|
|
|
|
|
|
|
//Google storage req
|
2020-03-10 22:03:32 +01:00
|
|
|
const PROJECT_ID = "marketalarm";
|
|
|
|
|
const KEY_FILENAME = ""; //relative path
|
2020-02-16 01:06:53 +01:00
|
|
|
const BUCKET_NAME = "marketalarm-photos";
|
2020-02-26 14:56:00 +01:00
|
|
|
const storage = new Storage();
|
2020-02-16 01:06:53 +01:00
|
|
|
|
2020-02-14 00:37:11 +01:00
|
|
|
const bucket = storage.bucket(BUCKET_NAME);
|
|
|
|
|
|
|
|
|
|
app.get("/generateSignedURL", (req, res) => {
|
2020-02-14 15:34:33 +01:00
|
|
|
async function generateSignedUrl() {
|
2020-03-11 22:32:10 +01:00
|
|
|
// console.log("Started server function!");
|
2020-03-10 22:03:32 +01:00
|
|
|
|
2020-02-14 15:34:33 +01:00
|
|
|
const options = {
|
2020-03-10 22:03:32 +01:00
|
|
|
//Tried to define Google ID and private key while debugging
|
|
|
|
|
version: "v2", //tried v4 also
|
2020-02-14 15:34:33 +01:00
|
|
|
action: "write",
|
2020-03-10 22:03:32 +01:00
|
|
|
contentType: "image/*", //tried without and with specific image/png ex.
|
2020-02-14 15:34:33 +01:00
|
|
|
expires: Date.now() + 86400000
|
|
|
|
|
};
|
|
|
|
|
const filename = req.query.filename;
|
|
|
|
|
|
2020-03-11 22:32:10 +01:00
|
|
|
// console.log("Filename: ", filename);
|
|
|
|
|
// console.log("Bucket name:", bucket.name);
|
2020-02-14 00:37:11 +01:00
|
|
|
|
2020-02-14 15:34:33 +01:00
|
|
|
const [url] = await bucket.file(filename).getSignedUrl(options);
|
2020-02-14 00:37:11 +01:00
|
|
|
|
2020-03-11 22:32:10 +01:00
|
|
|
//console.log(`The signed url is ${url}.`);
|
2020-02-18 23:49:00 +01:00
|
|
|
|
|
|
|
|
res.status(200).send(url);
|
2020-02-14 15:34:33 +01:00
|
|
|
}
|
|
|
|
|
generateSignedUrl().catch(console.error);
|
2020-02-14 00:37:11 +01:00
|
|
|
});
|