1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-12-02 22:49:09 +02:00

Cli: Added headless server command (Beta) (#1860)

* Trying to implement headless server

* Cli: Cleaned up and completed server command so that it is usable. Added warnings as it is advanced usage only at this point.

* Restored welcome assets
This commit is contained in:
Laurent Cozic
2019-09-08 17:16:45 +01:00
committed by GitHub
parent 4488a1b95f
commit 3d6c932e1b
11 changed files with 333 additions and 177 deletions

View File

@@ -1,7 +1,7 @@
const urlParser = require('url');
const Setting = require('lib/models/Setting');
const { Logger } = require('lib/logger.js');
const randomClipperPort = require('lib/randomClipperPort');
const { randomClipperPort, startPort } = require('lib/randomClipperPort');
const enableServerDestroy = require('server-destroy');
const Api = require('lib/services/rest/Api');
const ApiResponse = require('lib/services/rest/ApiResponse');
@@ -73,13 +73,22 @@ class ClipperServer {
throw new Error('All potential ports are in use or not available.');
}
async isRunning() {
const tcpPortUsed = require('tcp-port-used');
const port = Setting.value('api.port') ? Setting.value('api.port') : startPort(Setting.value('env'));
const inUse = await tcpPortUsed.check(port);
return inUse ? port : 0;
}
async start() {
this.setPort(null);
this.setStartState('starting');
const settingPort = Setting.value('api.port');
try {
const p = await this.findAvailablePort();
const p = settingPort ? settingPort : await this.findAvailablePort();
this.setPort(p);
} catch (error) {
this.setStartState('idle');
@@ -200,6 +209,10 @@ class ClipperServer {
this.server_.listen(this.port_, '127.0.0.1');
this.setStartState('started');
// We return an empty promise that never resolves so that it's possible to `await` the server indefinitely.
// This is used only in command-server.js
return new Promise(() => {});
}
async stop() {