-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
111 lines (97 loc) · 2.65 KB
/
index.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
"use strict";
const stream = require("stream");
const http = require("http");
const Promise = require("bluebird");
const Sealious = require("sealious");
const Hapi = require("hapi");
const Boom = require("boom");
const merge = require("merge");
const handle_request = require("./handle-request.js");
const get_request_body = require("./get-request-body.js");
const extract_context = require("./extract-context.js");
const handle_response = require("./handle-response.js");
const handle_error = require("./handle-error.js");
module.exports = function(App){
const channel = App.createChip(Sealious.Channel, {name:"www-server"});
const config = App.ConfigManager.get_config()["www-server"];
const server = new Hapi.Server();
server.connection({port: config.port});
const rest_url_base = config["api-base"];
const path = `${rest_url_base}/{elements*}`;
server.state(config["session-cookie-name"], {
ttl: 24 * 60 * 60 * 1000, // One day
path: '/',
isSecure: false,
});
server.state(config["anonymous-cookie-name"], {
ttl: 24 * 60 * 60 * 1000, // One day
path: '/',
isSecure: false,
});
server.register(require("inert"), function(){
server.route({
method: ["GET", "DELETE"],
path: path,
handler: handle_request.bind({}, App),
});
server.route({
method: ["PATCH", "PUT", "POST"],
path: path,
config: {
payload: {
multipart: {
output: "annotated",
},
maxBytes: config["max-payload-bytes"],
},
handler: handle_request.bind({}, App),
}
});
});
channel.static_route = function(local_path, public_path){
server.register(require("inert"), function(){
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: local_path
}
}
});
});
};
channel.custom_route = function(method, path, handler){
server.register(require("inert"), function(){
server.route({
method: method,
path: path,
handler: function(request, reply){
let context = null;
return extract_context(App, request)
.then(function(_context){
context = _context;
return get_request_body(context, request);
})
.then(function(body){
return handler(App, context, body);
})
.then((result) => handle_response(App, context, reply)(result))
.catch((result) => handle_error(App, reply)(result));
},
});
});
};
channel.custom_raw_route = server.route.bind(server);
channel.start = function(){
return new Promise(function(resolve, reject){
server.start(function(err){
if (err) {
throw err;
}
App.Logger.info(`www-server: running at ${server.info.uri}`);
resolve();
});
});
};
};