-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
123 lines (107 loc) · 3.64 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
/* server.js: run this with Node.js in the publish/ folder to start your server.
* Copyright © 2011 Jan Keromnes, Thaddee Tyl. All rights reserved.
* Code covered by the LGPL license. */
// Import the required modules:
//
// - fs: deals with hard drive interaction (to save the configuration file).
// - camp: creates the HTTP server.
var fs = require('fs'),
camp = require ('./camp/camp.js');
// --- Start the server ---
//
// This function is launched once the `config.json` file is read and parsed into
// memory.
function start(config) {
// Get current configuration with Ajax.
// This uses the API defined in the Camp library.
camp.add('pullconfig', function(data) {
return config;
});
// Overwrite the configuration (in RAM) through Ajax.
// Again, it uses the API defined in Camp.
camp.add('pushconfig', function(data) {
config = data.config;
return;
});
// The following is a helper macro that is used in the template system.
// Note: the index.html file is templated, using a template system
// provided by the ScoutCamp server library.
// The template replaces {{ curly braced content }} with relevant information.
camp.Plate.macros['l'] = function ( literal, params ) {
var rail = literal[params[0]];
// `nl`: contains the path that will be used in the SVG.
var nl = "M" + (literal.airport.nodes[rail.points[0]].x - 0.5) + " "
+ (literal.airport.nodes[rail.points[0]].y - 0.5);
for (var i=1; i<rail.points.length; i++) {
nl += " L" + (literal.airport.nodes[rail.points[i]].x - 0.5) + " "
+ (literal.airport.nodes[rail.points[i]].y - 0.5);
}
return nl;
};
// Add objects from config to index.html.
// The following function returns the aggregated data that the templated file
// `index.html` will need.
function handleindex(query, path) {
console.log('templating index from', path[0]);
path[0] = '/index.html';
var desks = {},
slides = {},
treads = {},
carousels = {},
garages = {};
for (var i in config.airport.nodes){
switch (config.airport.nodes[i].type) {
case 'desk':
desks[i] = config.airport.nodes[i];
break;
case 'slide':
slides[i] = config.airport.nodes[i];
break;
case 'tread':
treads[i] = config.airport.nodes[i];
break;
case 'carousel':
carousels[i] = config.airport.nodes[i];
break;
case 'garage':
garages[i] = config.airport.nodes[i];
break;
}
}
return {
auto: config.auto,
airport: config.airport,
desks: desks,
slides: slides,
treads: treads,
carousels: carousels,
garages: garages,
nodes: config.airport.nodes,
background: config.background
};
}
// Reroute the following paths so that they are handled by the `index.html`
// template.
camp.handle(/^\/index.html$/, handleindex);
camp.handle(/^\/$/, handleindex);
// Download the JSON configuration file.
camp.handle('/config.json', function(query, path) {
return {content: JSON.stringify(config, null, 2)};
});
// Finally start the server
camp.start(config.port || 80, config.debug || 0);
}
// --- Main function ---
//
// This function is needed only before starting the server in order to get
// all the information stored in the `config.json` file.
var configfile = process.argv[2] || '../configfile.json';
(function main() {
console.log('starting...');
// Reading the `config.json` file...
fs.readFile(configfile, function(err, data) {
if ( err ) throw err;
start(JSON.parse(data));
});
})();
// vim: ts=8 et