-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
executable file
·159 lines (145 loc) · 4.13 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node
var restify = require('restify');
var path = require('path');
var fs = require('fs');
/* read in configuration */
var config = require('./config');
var serve_dir;
if (config["serve_dir"]){
serve_dir = config["serve_dir"];
}
else {
serve_dir = process.cwd();
}
/*
* We autogenerate the url property for each file to ensure that it is correct.
* This overrides whatever may have been place into the manifest.
* Adjust config.json if these urls are coming out wrong
*/
function process_manifest(req, uuid) {
var manifest;
try {
manifest = require(path.join(serve_dir, uuid + '/manifest'));
var url_prefix = config.prefix + req.header('Host') + config.suffix + "/datasets/" + uuid + "/";
for (entry in manifest.files) {
manifest.files[entry].url = url_prefix + manifest.files[entry].path
};
return manifest;
}
catch(err) {
req.log.error("Failed to parse manifest for " + uuid + " error: " + err);
return false;
}
}
/*
* Smoosh together all manifests into an array and return it
*/
function alldatasets(req, res, next) {
var everything = [];
fs.readdir(serve_dir, function (err, dirlist) {
if (err) {
res.send(500, 'Internal Server Error');
return;
}
else {
res.header("Access-Control-Allow-Headers", "Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token");
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
res.header("Access-Control-Allow-Origin", "*");
for (entry in dirlist) {
if (fs.existsSync(path.join(serve_dir, dirlist[entry] + '/manifest.json'))) {
var manifest = process_manifest(req, dirlist[entry]);
if ( manifest ) {
everything.push(manifest);
};
};
};
res.send(everything);
req.log.info("served up /datasets");
};
});
return next();
}
/*
* Process and return the requested manifest
*/
function manifest(req, res, next) {
var manifest = process_manifest(req, req.params.id);
if (!manifest) {
res.send(404, '404 Not Found');
return next();
}
res.send(process_manifest(req, req.params.id));
req.log.info("served up /datasets/" + req.params.id);
return next();
}
/*
* Serve up the requested image file
*/
function imagefile(req, res, next) {
var filename = path.join(serve_dir, req.params.id + '/' + req.params.path);
req.log.info("serving up /datasets/" + req.params.id + '/' + req.params.path);
fs.exists(filename, function (exists) {
if (!exists) {
res.send(404, '404 Not Found');
return;
} else {
res.header("Content-Type", "application/octet-stream");
res.header("Content-Length", fs.statSync(filename).size);
var stream = fs.createReadStream(filename, { bufferSize: 64 * 1024 });
stream.pipe(res);
}
});
return next();
}
/*
* Implement ping
*/
function ping(req, res, next) {
res.send({"ping":"pong"});
req.log.info("served up /ping");
return next();
}
/*
* Serve /
*/
function slash(req, res, next) {
var accept = req.header("Accept");
if (accept && (accept.search("application/xhtml+xml") != -1 || accept.search("text/html") != -1)) {
var stream = fs.createReadStream(serve_dir + "/index.html", { bufferSize: 64 * 1024 });
stream.pipe(res);
} else {
res.header("Content-Type", "application/json");
var stream = fs.createReadStream(serve_dir + "/index.json", { bufferSize: 64 * 1024 });
stream.pipe(res);
}
req.log.info("served up /");
return next();
}
/*
* route creation helper
*/
function setup_routes(server, route, handler) {
server.get(route, handler);
server.head(route, handler);
}
/* node restify rocks! */
var server = restify.createServer({
name: 'dsapi',
version: '0.1.1'
});
var server = restify.createServer();
setup_routes(server, '/datasets', alldatasets);
setup_routes(server, '/datasets/:id', manifest);
setup_routes(server, '/datasets/:id/:path', imagefile);
setup_routes(server, '/ping', ping);
setup_routes(server, '/', slash);
server.log.level(config.loglevel);
server.listen(config.listen_port, function() {
if ( typeof config.listen_port == "string") {
console.log('listening on unix socket: %s', config.listen_port);
}
else {
console.log('%s listening at %s', server.name, server.url);
}
console.log('serving from %s', serve_dir);
});