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");
|
2020-09-13 04:48:11 -07:00
|
|
|
const { logDebug } = require("./app/helpers/log");
|
2019-09-12 06:23:03 +02: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
|
|
|
|
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 = () => {
|
2020-09-13 23:45:08 +02:00
|
|
|
logDebug("Crawl start. crawlerRunning: ", crawlerRunning);
|
2019-09-26 17:30:06 +02:00
|
|
|
if (!crawlerRunning) {
|
|
|
|
|
crawlerRunning = true;
|
|
|
|
|
crawlAll().then(newRealEstates => {
|
2020-09-13 04:48:11 -07:00
|
|
|
logDebug("crawlAll done, new real estate len: ", newRealEstates.length);
|
2019-09-30 13:53:35 +02:00
|
|
|
notifyForNewRealEstates(newRealEstates);
|
2020-09-14 14:46:21 -07:00
|
|
|
}).catch(e => {
|
|
|
|
|
console.error('Error happened: ', e);
|
|
|
|
|
}).finally(()=> {
|
|
|
|
|
crawlerRunning = false;
|
|
|
|
|
logDebug('Finally done!');
|
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);
|