﻿"use strict";

const path = require("path");
const config = require("config");
const express = require("express");
const favicon = require("serve-favicon");
const bodyParser = require("body-parser");
const compression = require("compression");

const routes = require("./routes");
const crawler = require("./controllers/crawler");
const dbhelper = require("./controllers/dbhelper");
const assetsMap = require("./public/assets/assets.json");

// init db.
dbhelper.start((err) =>
{
    let msg = "\nDatabase Server not started, some features will be shut down.";
    if (err)
    {
        console.error(err.message + msg);
    }
    else
    {
        dbhelper.connect((err) =>
        {
            if (err)
            {
                console.error(msg);
            }
            else
            {
                if (config.crawler.enabled)
                {
                    setTimeout(crawler.start, config.crawler.delay);
                }
            }
        });
    }
});

// init express.
const app = express();

// view engine setup.
app.set("views", __dirname + "/views/");
app.set("view engine", "jade");

// compression.
app.use(compression());

// favicon setup.
app.use(favicon(__dirname + "/public/favicon.ico"));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// static file setup.
app.use(express.static(__dirname + "/public/", {
    maxAge: 2592000000
}));

// router setup.
app.use("/api/4", routes.api);
app.use("/", routes.web);

// assets map setup.
app.locals.map = assetsMap;

// catch 404 and forward to global error handler.
app.use((req, res, next) =>
{
    const err = new Error("404 Not Found");
    err.status = 404;
    next(err);
});

// global error handler.
app.use((err, req, res, next) =>
{
    res.status(err.status || 500).render("error_404", { map: app.locals.map });
});

module.exports = app;

@@ -476,27 +476,102 @@
 per.
-start((err) =%3E%0D%0A%7B%0D%0A
+connect((err) =%3E%0D%0A%7B%0D%0A    if (err)%0D%0A    %7B%0D%0A        dbhelper.start((err) =%3E%0D%0A        %7B%0D%0A        
     
@@ -648,16 +648,24 @@
 own.%22;%0D%0A
+        
     if (
@@ -674,27 +674,42 @@
 r)%0D%0A    
-%7B%0D%0A
+        %7B%0D%0A       
         
 console.
@@ -692,32 +692,33 @@
 %0A               
+ 
 console.error(er
@@ -719,36 +719,59 @@
 ror(
-err.message + msg);%0D%0A
+msg + %22%5Cn%22 + err.message);%0D%0A        
     %7D%0D%0A
+        
     
@@ -776,27 +776,43 @@
   else%0D%0A    
-%7B%0D%0A
+        %7B%0D%0A        
         dbhe
@@ -834,35 +834,51 @@
 rr) =%3E%0D%0A        
+    
-%7B%0D%0A
+    %7B%0D%0A        
             if (
@@ -887,35 +887,51 @@
 r)%0D%0A            
+  
-%7B%0D%0A
+      %7B%0D%0A        
                 
@@ -947,20 +947,48 @@
 rror(msg
-);%0D%0A
+ + %22%5Cn%22 + err.message);%0D%0A       
         
@@ -983,35 +983,44 @@
                 
+ 
 %7D%0D%0A
+        
             else
@@ -1025,35 +1025,51 @@
 se%0D%0A            
+     
-%7B%0D%0A
+   %7B%0D%0A        
                 
@@ -1105,35 +1105,51 @@
                 
+   
-%7B%0D%0A
+     %7B%0D%0A        
                 
@@ -1210,35 +1210,50 @@
                 
+ 
-%7D%0D%0A
+       %7D%0D%0A       
             %7D%0D%0A 
@@ -1240,32 +1240,33 @@
                 
+ 
 %7D%0D%0A        %7D);%0D%0A
@@ -1260,21 +1260,202 @@
         
+      
+  
 %7D);%0D%0A
+            %7D%0D%0A        %7D);%0D%0A    %7D%0D%0A    else%0D%0A    %7B%0D%0A        if (config.crawler.enabled)%0D%0A        %7B%0D%0A            setTimeout(crawler.start, config.crawler.delay);%0D%0A        %7D%0D%0A
     %7D%0D%0A%7D

add mongodb service check