This repository has been archived on 2021-02-16. You can view files and clone it, but cannot push or open issues or pull requests.
Nodezzarella/app.js

85 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2017-07-23 01:17:20 +02:00
const express = require('express'),
cors = require("cors"),
app = express(),
session = require('express-session'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
helmet = require('helmet'),
log = require("color-logs")(true, true, "Nodezzarella app"),
fs = require("fs"),
promise = require('promise');
2017-07-03 23:31:12 +02:00
log.info("Application started and now preparing");
var corsOptions = {
"origin": "*",
"Access-Control-Allow-Origin": "*",
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false
};
2017-07-19 22:49:13 +02:00
2017-07-20 23:50:30 +02:00
global.config = require("./config.json");
2017-07-03 23:31:12 +02:00
2017-07-20 23:50:30 +02:00
class Nodezzarella {
constructor(rootPath){
this.getDBType().then((configDB) => {
2017-07-19 22:49:13 +02:00
if(!configDB.flatFile){
2017-07-20 23:50:30 +02:00
var db = require('./lib/db/'+config.db+'/db');
new db().connectDB(configDB).then((connexion) => {
global.dbConnexion = connexion;
2017-07-22 23:22:50 +02:00
global.dbConf = configDB;
2017-07-20 23:50:30 +02:00
this.run();
});
2017-07-19 22:49:13 +02:00
}
2017-07-20 23:50:30 +02:00
else{
this.run();
}
}).catch((err) => {
new Error("Nodezzarella can't initialize");
2017-07-03 23:31:12 +02:00
});
}
2017-07-20 23:50:30 +02:00
getDBType(){
2017-07-19 22:49:13 +02:00
return new Promise((resolve, reject) => {
2017-07-20 23:50:30 +02:00
fs.access("./lib/db/"+config.db+"/config.json", fs.constants.F_OK || fs.constants.R_OK, (error) => {
if(error){
reject(new Error("File not exist"));
}
else{
resolve(require("./lib/db/"+config.db+"/config.json"));
}
});
2017-07-19 22:49:13 +02:00
});
2017-07-20 23:50:30 +02:00
}
run(){
var routes = require("./lib/router");
log.info("HTTP server listening on port", config.appPort);
log.info("Application ready");
2017-07-23 17:37:50 +02:00
app.set('trust proxy', 1);
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
2017-07-23 01:17:20 +02:00
app.use(session({
secret: 'Nodezzarella',
2017-07-23 17:37:50 +02:00
resave: true,
2017-07-23 01:17:20 +02:00
saveUninitialized: true,
2017-07-23 17:37:50 +02:00
cookie: { secure: false, maxAge: 3600*24*31 }
2017-07-23 01:17:20 +02:00
}))
2017-07-20 23:50:30 +02:00
app.use(config.webroot || "/", routes);
app.use(function(req, res, next) {
res.status(404);
2017-07-22 01:24:15 +02:00
res.send("404 not found");
2017-07-20 23:50:30 +02:00
return;
2017-07-19 22:49:13 +02:00
});
2017-07-20 23:50:30 +02:00
/**
* Listen to http://0.0.0.0:port
*/
app.disable('x-powered-by');
app.listen(config.appPort || 8888);
2017-07-19 22:49:13 +02:00
}
2017-07-03 23:31:12 +02:00
}
2017-07-20 23:50:30 +02:00
new Nodezzarella("/");