-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
84 lines (72 loc) · 2.46 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var http = require('http');
var https = require('https');
var express = require('express');
var url = require('url');
var CONFIG = require('./config/development.json'); // You must change this to match the actual name of your configuration file.
var fs = require('fs');
passport = require("passport");
LocalStrategy = require('passport-local').Strategy;
var mongoose = require('mongoose');
var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var session = require("express-session");
var serveStatic = require("serve-static");
var router = require('./controllers/router.js');
var app = express();
var theServer;
if (CONFIG.isSSL)
{
var privateKey = fs.readFileSync('sslcert/ssl.key', 'utf8');
var certificate = fs.readFileSync('sslcert/ssl.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate, passphrase: CONFIG.sslpass};
theServer = https.createServer(credentials, app);
}
else
theServer = http.createServer(app);
app.use(cookieParser());
app.use(serveStatic(__dirname + '/public'));
app.use(bodyParser());
app.use(session({ secret: 'SECRET' }));
app.use(passport.initialize());
app.use(passport.session());
app.set('port', process.env.PORT || CONFIG.usePort);
var connection = mongoose.createConnection("mongodb://" + CONFIG.user + ":" + CONFIG.password + "@" + CONFIG.server + "/" + CONFIG.db);
connection.once('open', function ()
{
console.log('opened database');
});
var models = require("./models/index")(CONFIG, connection);
var SCHEMAS = models.schemasModel;
var notify = models.notifyModel;
router(app, models);
//var Users = connection.model("Users", SCHEMAS.UserSchema, "GoodUsers");
var Version = connection.model("Version", SCHEMAS.VersionSchema, "Version");
// need to refactor a tools model before I can get this to work right...
app.get('/patches', function(req, res)
{
Version.find({}).sort("-_id").exec(function(err, patches)
{
if (err)
{
console.log("Error! " + err);
res.send(500, err);
}
else
res.send(patches);
});
});
app.get('*', function(req, res)
{
res.sendfile('./public/index.html');
});
Version.findOne({}).sort("-_id").exec(function(err, member)
{
if (member.published == false) // broadcast stuff goes here
{
member.published = true;
member.save();
}
app.set('version', member.version);
theServer.listen(app.get('port'));
console.log("Listening on port: " + app.get('port'));
});