-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
124 lines (104 loc) · 3.41 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// NPM dependencies
var express = require('express');
var sharejs = require('../ShareJS/').server;
var faye = require('faye');
var fs = require('fs');
// Local Modules
var DashboardManager = require('./js/server/dashboardsManager');
var DataSetsManager = require('./js/server/dataSetsManager');
var GadgetsManager = require('./public/gadgets/gadgetsInfo');
var xhr = require("./js/shared/xhr");
var app = express.createServer();
var dashboardsManager;
var dataSetsManager;
var gadgetsManager;
// Share JS options
var options = {
rest: { path : '/dashboard/:name/state'},
db: { type: 'none'},
auth: function(client, action) {
// This auth handler rejects any ops bound for docs starting with 'readonly'.
if (action.name === 'submit op' && action.docName.match(/^readonly/)) {
action.reject();
} else {
action.accept();
}
}
};
// Lets try and enable redis persistance if redis is installed...
try {
require('redis');
options.db = {type: 'redis'};
} catch (e) {}
// Attach the sharejs REST and Socket.io interfaces to the server
sharejs.attach(app, options);
// Faye adapter for pub/sub of dashboards
var adapter = new faye.NodeAdapter({ mount: '/faye', timeout: 45 });
adapter.attach(app);
// Configuration
app.configure(function(){
app.set('views', __dirname + '/templates');
app.set('view engine', 'mustache');
app.register(".mustache", require('stache'));
app.set('view options', {layout: false });
app.use(express.bodyParser());
app.use(express.static(__dirname + '/js/client'));
app.use(express.static(__dirname + '/js/shared'));
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
dashboardsManager = new DashboardManager(app, app.model);
app.get('/xhrProxy/:request', function(req, res){
var options = {
url: req.params.request,
type: "GET",
success: function(response) { res.send(response); }
}
xhr.ajax(options);
});
app.get('/gadgets/', function(req, res){
res.send(GadgetsManager.all);
});
app.get('/dataSet/:id', function(req, res){
var dataSetFound = function (dataSet) {
res.send(JSON.stringify(dataSet));
};
DataSetsManager.find(req.params.id, dataSetFound);
});
app.post('/dataSet/', function(req, res){
var queryInfo = req.body || undefined;
var dataSetCreated = function(datasetId){
res.send(JSON.stringify(datasetId));
}
DataSetsManager.createDataSet(queryInfo, dataSetCreated);
});
app.post('/uploadFITS/:dashboardId', function(req, res) {
// Add the image to /images/fitsViewer/(dashboardId)
var tmpPath = req.files.fitsImage.path;
var dashboardId = req.params.dashboardId;
var targetPath = './public/images/fitsViewer/';
// Create the directory if it doesnt exist
if (!fs.existsSync(targetPath)) {
fs.mkdirSync(targetPath);
}
if (!fs.existsSync(targetPath + dashboardId)) {
fs.mkdirSync(targetPath + dashboardId);
}
targetPath += dashboardId + '/' + req.files.fitsImage.name;
fs.rename(tmpPath, targetPath, function(err) {
if (err) throw err;
fs.unlink(tmpPath, function() {
if (err) throw err;
res.send('/images/fitsViewer/' + dashboardId + '/' + req.files.fitsImage.name);
});
});
});
if (!module.parent) {
app.listen(80);
console.log("ASCOT server listening on port %d", app.address().port);
}