2018-05-16 15:16:14 +02:00
|
|
|
const { netUtils } = require('lib/net-utils');
|
|
|
|
const urlParser = require("url");
|
2018-05-23 13:14:38 +02:00
|
|
|
const Setting = require('lib/models/Setting');
|
|
|
|
const { Logger } = require('lib/logger.js');
|
2018-05-25 12:08:22 +02:00
|
|
|
const randomClipperPort = require('lib/randomClipperPort');
|
2018-05-25 14:30:27 +02:00
|
|
|
const enableServerDestroy = require('server-destroy');
|
2018-09-27 10:14:05 +02:00
|
|
|
const Api = require('lib/services/rest/Api');
|
2018-09-30 11:15:46 +02:00
|
|
|
const ApiResponse = require('lib/services/rest/ApiResponse');
|
2018-09-28 20:24:57 +02:00
|
|
|
const multiparty = require('multiparty');
|
2018-05-16 15:16:14 +02:00
|
|
|
|
|
|
|
class ClipperServer {
|
|
|
|
|
2018-05-23 13:14:38 +02:00
|
|
|
constructor() {
|
|
|
|
this.logger_ = new Logger();
|
2018-05-25 14:30:27 +02:00
|
|
|
this.startState_ = 'idle';
|
|
|
|
this.server_ = null;
|
|
|
|
this.port_ = null;
|
2018-09-28 20:24:57 +02:00
|
|
|
this.api_ = new Api(() => {
|
|
|
|
return Setting.value('api.token');
|
|
|
|
});
|
2018-05-25 14:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static instance() {
|
|
|
|
if (this.instance_) return this.instance_;
|
|
|
|
this.instance_ = new ClipperServer();
|
|
|
|
return this.instance_;
|
2018-05-23 13:14:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setLogger(l) {
|
|
|
|
this.logger_ = l;
|
2018-09-27 10:14:05 +02:00
|
|
|
this.api_.setLogger(l);
|
2018-05-23 13:14:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
|
|
|
}
|
|
|
|
|
2018-05-25 14:30:27 +02:00
|
|
|
setDispatch(d) {
|
|
|
|
this.dispatch_ = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(action) {
|
|
|
|
if (!this.dispatch_) throw new Error('dispatch not set!');
|
|
|
|
this.dispatch_(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
setStartState(v) {
|
|
|
|
if (this.startState_ === v) return;
|
|
|
|
this.startState_ = v;
|
|
|
|
this.dispatch({
|
|
|
|
type: 'CLIPPER_SERVER_SET',
|
|
|
|
startState: v,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setPort(v) {
|
|
|
|
if (this.port_ === v) return;
|
|
|
|
this.port_ = v;
|
|
|
|
this.dispatch({
|
|
|
|
type: 'CLIPPER_SERVER_SET',
|
|
|
|
port: v,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-25 12:08:22 +02:00
|
|
|
async findAvailablePort() {
|
|
|
|
const tcpPortUsed = require('tcp-port-used');
|
|
|
|
|
|
|
|
let state = null;
|
|
|
|
for (let i = 0; i < 10000; i++) {
|
2018-05-26 12:18:54 +02:00
|
|
|
state = randomClipperPort(state, Setting.value('env'));
|
2018-05-25 12:08:22 +02:00
|
|
|
const inUse = await tcpPortUsed.check(state.port);
|
|
|
|
if (!inUse) return state.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('All potential ports are in use or not available.')
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:16:14 +02:00
|
|
|
async start() {
|
2018-05-25 14:30:27 +02:00
|
|
|
this.setPort(null);
|
|
|
|
|
|
|
|
this.setStartState('starting');
|
2018-05-25 12:08:22 +02:00
|
|
|
|
|
|
|
try {
|
2018-05-25 14:30:27 +02:00
|
|
|
const p = await this.findAvailablePort();
|
|
|
|
this.setPort(p);
|
2018-05-25 12:08:22 +02:00
|
|
|
} catch (error) {
|
2018-05-25 14:30:27 +02:00
|
|
|
this.setStartState('idle');
|
2018-05-25 12:08:22 +02:00
|
|
|
this.logger().error(error);
|
2018-05-23 15:25:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-05-16 15:16:14 +02:00
|
|
|
|
2018-05-25 14:30:27 +02:00
|
|
|
this.server_ = require('http').createServer();
|
2018-05-16 15:16:14 +02:00
|
|
|
|
2018-05-26 16:46:57 +02:00
|
|
|
this.server_.on('request', async (request, response) => {
|
2018-05-16 15:16:14 +02:00
|
|
|
|
2018-09-30 11:15:46 +02:00
|
|
|
const writeCorsHeaders = (code, contentType = "application/json", additionalHeaders = null) => {
|
|
|
|
const headers = Object.assign({}, {
|
2018-05-26 16:46:57 +02:00
|
|
|
"Content-Type": contentType,
|
2018-05-16 15:16:14 +02:00
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
|
|
|
|
'Access-Control-Allow-Headers': 'X-Requested-With,content-type',
|
2018-09-30 11:15:46 +02:00
|
|
|
}, additionalHeaders ? additionalHeaders : {});
|
|
|
|
response.writeHead(code, headers);
|
2018-05-16 15:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const writeResponseJson = (code, object) => {
|
|
|
|
writeCorsHeaders(code);
|
|
|
|
response.write(JSON.stringify(object));
|
|
|
|
response.end();
|
|
|
|
}
|
|
|
|
|
2018-05-25 12:08:22 +02:00
|
|
|
const writeResponseText = (code, text) => {
|
2018-05-26 16:46:57 +02:00
|
|
|
writeCorsHeaders(code, 'text/plain');
|
2018-05-25 12:08:22 +02:00
|
|
|
response.write(text);
|
|
|
|
response.end();
|
|
|
|
}
|
|
|
|
|
2018-09-30 11:15:46 +02:00
|
|
|
const writeResponseInstance = (code, instance) => {
|
|
|
|
if (instance.type === 'attachment') {
|
|
|
|
const filename = instance.attachmentFilename ? instance.attachmentFilename : 'file';
|
|
|
|
writeCorsHeaders(code, instance.contentType ? instance.contentType : 'application/octet-stream', {
|
|
|
|
'Content-disposition': 'attachment; filename=' + filename,
|
|
|
|
'Content-Length': instance.body.length,
|
|
|
|
});
|
|
|
|
response.end(instance.body);
|
|
|
|
} else {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 10:14:05 +02:00
|
|
|
const writeResponse = (code, response) => {
|
2018-09-30 11:15:46 +02:00
|
|
|
if (response instanceof ApiResponse) {
|
|
|
|
writeResponseInstance(code, response);
|
|
|
|
} else if (typeof response === 'string') {
|
2018-09-27 10:14:05 +02:00
|
|
|
writeResponseText(code, response);
|
|
|
|
} else {
|
|
|
|
writeResponseJson(code, response);
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 15:16:14 +02:00
|
|
|
|
2018-09-27 10:14:05 +02:00
|
|
|
this.logger().info('Request: ' + request.method + ' ' + request.url);
|
2018-05-16 15:16:14 +02:00
|
|
|
|
2018-09-27 10:14:05 +02:00
|
|
|
const url = urlParser.parse(request.url, true);
|
2018-05-26 16:46:57 +02:00
|
|
|
|
2018-09-28 20:24:57 +02:00
|
|
|
const execRequest = async (request, body = '', files = []) => {
|
2018-09-27 10:14:05 +02:00
|
|
|
try {
|
2018-09-28 20:24:57 +02:00
|
|
|
const response = await this.api_.route(request.method, url.pathname, url.query, body, files);
|
2019-02-07 00:36:39 +02:00
|
|
|
writeResponse(200, response ? response : '');
|
2018-09-27 10:14:05 +02:00
|
|
|
} catch (error) {
|
2018-10-04 09:05:22 +02:00
|
|
|
this.logger().error(error);
|
2018-09-27 10:14:05 +02:00
|
|
|
writeResponse(error.httpCode ? error.httpCode : 500, error.message);
|
2018-05-26 16:46:57 +02:00
|
|
|
}
|
2018-09-27 10:14:05 +02:00
|
|
|
}
|
2018-09-23 19:03:11 +02:00
|
|
|
|
2018-09-28 20:24:57 +02:00
|
|
|
const contentType = request.headers['content-type'] ? request.headers['content-type'] : '';
|
|
|
|
|
2018-09-27 10:14:05 +02:00
|
|
|
if (request.method === 'OPTIONS') {
|
|
|
|
writeCorsHeaders(200);
|
|
|
|
response.end();
|
|
|
|
} else {
|
2018-09-28 20:24:57 +02:00
|
|
|
if (contentType.indexOf('multipart/form-data') === 0) {
|
|
|
|
const form = new multiparty.Form();
|
|
|
|
|
|
|
|
form.parse(request, function(error, fields, files) {
|
|
|
|
if (error) {
|
|
|
|
writeResponse(error.httpCode ? error.httpCode : 500, error.message);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
execRequest(
|
|
|
|
request,
|
|
|
|
fields && fields.props && fields.props.length ? fields.props[0] : '',
|
|
|
|
files && files.data ? files.data : []
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2018-05-16 15:16:14 +02:00
|
|
|
} else {
|
2018-10-04 09:05:22 +02:00
|
|
|
if (request.method === 'POST' || request.method === 'PUT') {
|
2018-09-28 20:24:57 +02:00
|
|
|
let body = '';
|
|
|
|
|
|
|
|
request.on('data', (data) => {
|
|
|
|
body += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on('end', async () => {
|
|
|
|
execRequest(request, body);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
execRequest(request);
|
|
|
|
}
|
2018-05-16 15:16:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-25 14:30:27 +02:00
|
|
|
enableServerDestroy(this.server_);
|
|
|
|
|
|
|
|
this.logger().info('Starting Clipper server on port ' + this.port_);
|
|
|
|
|
2018-06-01 17:44:16 +02:00
|
|
|
this.server_.listen(this.port_, '127.0.0.1');
|
2018-05-25 14:30:27 +02:00
|
|
|
|
|
|
|
this.setStartState('started');
|
|
|
|
}
|
2018-05-16 15:16:14 +02:00
|
|
|
|
2018-05-25 14:30:27 +02:00
|
|
|
async stop() {
|
|
|
|
this.server_.destroy();
|
|
|
|
this.server_ = null;
|
|
|
|
this.setStartState('idle');
|
|
|
|
this.setPort(null);
|
2018-05-16 15:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ClipperServer;
|