-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
107 lines (83 loc) · 2.86 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
"use strict";
const http = require("http");
const path = require("path");
const fs = require('fs');
const Mirror = require('./mirror');
const {format} = require('util');
const drain = require('nyks/stream/drain');
const express = require('express');
class server {
constructor(config_path = (process.env["MIRROR_CONFIG_PATH"] || './example/config.json')) {
this.config = null;
if(typeof config_path == "string" && fs.existsSync(config_path)) {
console.log("Loading configuration from", config_path);
this.config = require(path.resolve(config_path));
}
if(typeof config_path == "object")
this.config = config_path;
if(!this.config)
throw `Invalid service configuration (check process.env.MIRROR_CONFIG_PATH)`;
this.port = this.config.port || 0;
this.mirror = new Mirror(this.config);
this.http_packages_root = this.config.http_packages_root || '/';
this.http_pool_root = this.config.http_pool_root || '/-/pool/';
this.app = express();
this.app.use(function(req, res, next) {
console.log("Incoming query %s:%s", req.method, req.url);
next();
});
//preserve %2f style in express/static/send
this.app.use("/", function(req, res, next) {
req.url = req.url.replace("%2F", "%2f");
if(/%2f/.test(req.url)) {
req.url = req.url.replace("%", "%25");
req.originalUrl = req.url;
}
next();
});
this.app.use(this.http_packages_root, express.static(this.mirror.packages_dir));
this.app.use(this.http_pool_root, express.static(this.mirror.pool_dir));
this.app.post("/process", async (req, res) => {
try {
return await this.process(req, res);
} catch(err) {
res.statusCode = 500;
res.end(String(err));
}
});
this.app.put("/feed", async (req, res) => {
try {
return await this.feed(req, res);
} catch(err) {
res.statusCode = 500;
res.end(String(err));
}
});
this.app.use((err, req, res, next) => { // eslint-disable-line
console.error("Cannot dispatch", err);
res.status(500).send(String(err));
});
this.server = http.createServer(this.app);
}
async feed(req, res) {
let manifest = JSON.parse(await drain(req));
let manifest_path = this.mirror.feed(manifest);
res.end(`Write manifest in ${manifest_path}`);
}
async process(req, res) {
//todo : lock processor
this.mirror.trace = function(...line) {
line = format(...line);
return new Promise(resolve => res.write(line + "\n", resolve));
};
await this.mirror.process();
await res.end();
}
async start() {
this.port = await new Promise(resolve => this.server.listen(this.port, () => {
resolve(this.server.address().port);
}));
console.log("Server is now ready on port", this.port);
}
}
module.exports = server;