-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
executable file
·64 lines (51 loc) · 1.87 KB
/
app.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
var NODE_PORT = 50001;
var NODE_TYPE = "CLOUDLET";
const uuidv4 = require('uuid/v4');
var express = require('express');
var multer = require('multer')
var upload = multer({ dest: './tmp_upload/' })
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://127.0.0.1/vstore_node');
var dbConn = mongoose.connection;
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var app = express();
var port = process.env.PORT || NODE_PORT;
// Middleware for MongoDB admin interface
var mongo_express = require('mongo-express/lib/middleware')
var mongo_express_config = require('./mongo_express_config.js')
app.use('/admin', mongo_express(mongo_express_config))
dbConn.once('open', function() {
console.log("Mongoose DB connection to mongoDB now open!");
});
var ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath('/usr/bin/avconv');
var NODE_UUID;
//Check if we have generated a UUID for this node.
var uuidSchema = new mongoose.Schema({configured: Boolean, uuid: String});
var UUID = mongoose.model('UUID', uuidSchema);
UUID.findOne({configured: true}, function(err, obj) {
if(!obj) {
//No UUID in database, so we generate one and save it.
NODE_UUID = uuidv4();
var new_uuid = new UUID({ configured: true, uuid: NODE_UUID});
new_uuid.save(function (err) {
if(err) {
console.log(err);
process.exit(1);
} else {
console.log("New UUID generated for this storage node: " + NODE_UUID);
}
});
} else {
NODE_UUID = obj.uuid;
}
console.log("Server running with UUID: " + NODE_UUID);
// Require the API routes
var routes = require('./routes.js')(app, upload, mongoose, dbConn,
NODE_UUID, NODE_TYPE, ffmpeg);
});
// Start server
app.listen(port);
console.log('Listening on port ' + port + '...');