Skip to content

Commit

Permalink
allow enabling the built-in web server via config
Browse files Browse the repository at this point in the history
  • Loading branch information
jahudka committed Aug 9, 2023
1 parent 553ebcf commit 538e4e5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 24 deletions.
27 changes: 14 additions & 13 deletions docs/user/03-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
Nodesockd is configured using one or more YAML config files. The available
configuration options are:

| Option | Type | Description |
|--------------|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `extends` | `string` | An optional path to another configuration file to merge with the current file. |
| `script` | `string` | **Required.** The path to the worker script / entry point. |
| `tmpDir` | `string` | **Required.** The path to the directory which should hold worker sockets, as well as the Nodesockd IPC socket. |
| `name` | `string` | The name to use in the process titles of all processes launched by Nodesockd; default: `app`. |
| `socketFile` | `string` | The name for the application socket file. This value must contain the `{worker}` placeholder, which will be replaced by the worker ID. Default: `app.{worker}.sock`. |
| `ipcFile` | `string` | The name of the IPC socket used to communicate between the daemon, the worker processes and the Nodesockd CLI. Defaults to `nodesockd.ipc`. |
| `workers` | `int` | The number of workers Nodesockd should launch; defaults to `1`. |
| `standby` | `int` | The number of standby workers Nodesockd should launch; default is zero. |
| `options` | `object` | An object with some advanced options, see below. |
| `env` | `string[]` | An array of environment variable names to pass through to workers. `NODE_ENV` will be always included automatically. |
| `debug` | `boolean` | Set to `true` to enable more verbose output from the daemon if things get hairy. |
| Option | Type | Description |
|--------------|-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `extends` | `string` | An optional path to another configuration file to merge with the current file. |
| `script` | `string` | **Required.** The path to the worker script / entry point. |
| `tmpDir` | `string` | **Required.** The path to the directory which should hold worker sockets, as well as the Nodesockd IPC socket. |
| `name` | `string` | The name to use in the process titles of all processes launched by Nodesockd; default: `app`. |
| `socketFile` | `string` | The name for the application socket file. This value must contain the `{worker}` placeholder, which will be replaced by the worker ID. Default: `app.{worker}.sock`. |
| `ipcFile` | `string` | The name of the IPC socket used to communicate between the daemon, the worker processes and the Nodesockd CLI. Defaults to `nodesockd.ipc`. |
| `workers` | `int` | The number of workers Nodesockd should launch; defaults to `1`. |
| `standby` | `int` | The number of standby workers Nodesockd should launch; default is zero. |
| `options` | `object` | An object with some advanced options, see below. |
| `env` | `string[]` | An array of environment variable names to pass through to workers. `NODE_ENV` will be always included automatically. |
| `devServer` | `boolean` or `number` | Configures the built-in development web server. `true` to enable the server on the default port `8000`, `number` to enable on a different port; `false` by default. |
| `debug` | `boolean` | Set to `true` to enable more verbose output from the daemon if things get hairy. |

The `options` object can contain the following keys:

Expand Down
6 changes: 2 additions & 4 deletions docs/user/05-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ document, the path to the `nodesockd` executable is left out for brevity.
- `--config=<path>` _or_ `-c <path>`
The path to the Nodesockd config file. See the [Configuration][1] section
for more about how the config file will be resolved.
- `--dev-server[=port]` _or_ `-d [port]`
Pass this option to enable the built-in development web server. This allows
you to use Nodesockd in development without the need to configure Nginx. If
`port` is omitted, it defaults to `8000`.
- `--dev-server[=port]` _or_ `-d [port]` _or_ `--no-dev-server`
This option can be used to override the `devServer` config option.

---

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodesockd",
"version": "2.0.0-rc.7",
"version": "2.0.0-rc.8",
"description": "Bridges a Nginx reverse proxy to a Node.js service using Unix sockets",
"homepage": "https://github.com/cdn77/node-socket-daemon",
"license": "MIT",
Expand Down
5 changes: 3 additions & 2 deletions src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Daemon } from '../daemon';
import { IpcRequestError } from '../ipc';
import { shortId, sleep } from '../utils';
import {
coerceBooleanOrNumber,
coerceJson,
compareWorkerStates,
createClient,
Expand All @@ -17,15 +18,15 @@ import {

export type DaemonArgs = {
config?: string;
devServer?: number;
devServer?: number | boolean;
};

export const daemon: CommandModule<DaemonArgs, DaemonArgs> = {
command: 'daemon',
describe: 'Run the Nodesockd daemon',
builder: (args) => args
.string('config').alias('c', 'config')
.number('dev-server').alias('d', 'dev-server').coerce('dev-server', (v) => v ?? 8000),
.string('dev-server').alias('d', 'dev-server').coerce('dev-server', coerceBooleanOrNumber),
handler: async (args) => {
const [config, files] = await resolveConfig(args.config);
const daemon = new Daemon(config, files, args.devServer);
Expand Down
18 changes: 18 additions & 0 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ export function coerceJson(value?: string): JsonObject | undefined {
return data;
}

export function coerceBooleanOrNumber(value?: any): number | boolean {
if (value === undefined || value === '') {
return true;
}

switch (typeof value) {
case 'number':
case 'boolean':
return value;
case 'string': {
const numeric = Number(value);
return Number.isNaN(numeric) ? true : numeric;
}
default:
throw new Error('Value must be a number or a boolean');
}
}

export function formatErrors(errors: string[]): string {
return errors.length > 1 ? `\n - ${errors.join('\n - ')}` : ` ${errors.join('')}`;
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const partialConfigSchema = z.strictObject({
standby: z.number().int().min(0).optional(),
options: partialOptionsSchema.optional(),
env: z.array(z.string()).optional(),
devServer: z.union([z.boolean(), z.number().int()]).optional(),
debug: z.boolean().optional(),
});

Expand All @@ -49,6 +50,7 @@ const finalConfigSchema = z.strictObject({
standby: z.number().int().min(0).default(0),
options: finalOptionsSchema.default({}),
env: z.array(z.string()).default([]),
devServer: z.union([z.boolean(), z.number().int()]).default(false),
debug: z.boolean().default(false),
});

Expand Down
13 changes: 11 additions & 2 deletions src/daemon/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Daemon {
private readonly startTs: number;
private terminating: boolean = false;

constructor(config: Config, configFiles: string[], devServerPort?: number) {
constructor(config: Config, configFiles: string[], devServer?: number | boolean) {
this.config = config;
this.configFiles = configFiles;
this.logger = new Logger({
Expand All @@ -43,7 +43,16 @@ export class Daemon {
this.pm = new ProcessManager(this.logger, config);
this.version = getNodesockdVersion();
this.startTs = Date.now();
devServerPort && (this.devServer = new DevServer(devServerPort));

if (devServer !== false && (devServer !== undefined || this.config.devServer !== false)) {
const devServerPort =
typeof devServer === 'number' ? devServer
: typeof this.config.devServer === 'number' ? this.config.devServer
: 8000;

this.devServer = new DevServer(devServerPort);
}

this.handleSignal = this.handleSignal.bind(this);
this.handleIpcConnection = this.handleIpcConnection.bind(this);
this.handleUncaughtError = this.handleUncaughtError.bind(this);
Expand Down

0 comments on commit 538e4e5

Please sign in to comment.