60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
const express = require("express");
|
|
const path = require("path");
|
|
const bodyParser = require("body-parser");
|
|
const layout = require("express-layout");
|
|
const compression = require("compression");
|
|
const forceSSL = require("./app/helpers/forceSSL");
|
|
const { logDebug } = require("./app/helpers/log");
|
|
|
|
const {
|
|
APP_PORT,
|
|
CRAWLER_INTERVAL,
|
|
STOP_CRAWLER
|
|
} = require("./app/config/appConfig");
|
|
const routes = require("./app/routes");
|
|
const { crawlAll } = require("./app/crawler/crawl");
|
|
const { checkUpNotify } = require("./app/services/notificationService");
|
|
const {
|
|
notifyForNewRealEstates
|
|
} = require("./app/services/notificationService");
|
|
|
|
const app = express();
|
|
|
|
app.use(forceSSL());
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
app.set("views", path.join(__dirname, "/app/views"));
|
|
app.set("view engine", "ejs");
|
|
app.use(layout());
|
|
|
|
app.use(compression());
|
|
app.use("/", routes);
|
|
|
|
app.use("/assets", express.static("./app/public"));
|
|
|
|
app.listen(APP_PORT, () =>
|
|
console.log(`Example app listening on port ${APP_PORT}!`)
|
|
);
|
|
|
|
let crawlerRunning = STOP_CRAWLER;
|
|
const crawl = () => {
|
|
logDebug("Crawl start. crawlerRunning: ", crawlerRunning);
|
|
if (!crawlerRunning) {
|
|
crawlerRunning = true;
|
|
crawlAll().then(newRealEstates => {
|
|
logDebug("crawlAll done, new real estate len: ", newRealEstates.length);
|
|
notifyForNewRealEstates(newRealEstates);
|
|
}).catch(e => {
|
|
console.error('Error happened: ', e);
|
|
}).finally(()=> {
|
|
crawlerRunning = false;
|
|
logDebug('Finally done!');
|
|
});
|
|
}
|
|
};
|
|
|
|
setInterval(crawl, CRAWLER_INTERVAL * 1000);
|
|
|
|
setInterval(checkUpNotify, 1000 * 60 * 60 * 24);
|