Skip to content

Commit

Permalink
feat: supports retrieving the port from the configuration file (#251)
Browse files Browse the repository at this point in the history
Adjust the order in which ports are obtained:option `--port` > [_egg.js_
configuration](https://www.eggjs.org/basics/config) `config/config.*.js`
> `process.env.EGG_BIN_DEFAULT_PORT` > 7001 > other available ports.


![image](https://github.com/eggjs/egg-bin/assets/35088591/ab8ffafd-c3f3-414b-a46e-a0a8fb897a17)

closes #250

---------

Co-authored-by: tiga <tigazhuang@futunn.com>
  • Loading branch information
JarryChung and tiga committed Feb 1, 2024
1 parent 56fa39f commit 07e150f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -74,7 +74,7 @@ egg-bin dev
#### dev options

- `--framework` egg web framework root path.
- `--port` server port, default to `7001`.
- `--port` server port. If not specified, the port is obtained in the following order: [_egg.js_ configuration](https://www.eggjs.org/basics/config) `config/config.*.js` > `process.env.EGG_BIN_DEFAULT_PORT` > 7001 > other available ports.
- `--workers` worker process number, default to `1` worker at local mode.
- `--sticky` start a sticky cluster server, default to `false`.

Expand Down
35 changes: 26 additions & 9 deletions src/cmd/dev.ts
Expand Up @@ -55,19 +55,36 @@ export class DevCommand extends BaseCommand {
}

protected async formatEggStartOptions() {
if (!this.port) {
const defaultPort = process.env.EGG_BIN_DEFAULT_PORT ?? 7001;
debug('detect available port');
this.port = await detect(defaultPort);
if (this.port !== defaultPort) {
console.warn('[egg-bin] server port %s is in use, now using port %o', defaultPort, this.port);
}
debug(`use available port ${this.port}`);
}
this.framework = utils.getFrameworkPath({
framework: this.framework,
baseDir: this.base,
});

if (!this.port) {
let configuredPort;
try {
const configuration = utils.getConfig({
framework: this.framework,
baseDir: this.base,
env: 'local',
});
configuredPort = configuration?.cluster?.listen?.port;
} catch (_) { /** skip when failing to read the configuration */ }

if (configuredPort) {
this.port = configuredPort;
debug(`use port ${this.port} from configuration file`);
} else {
const defaultPort = process.env.EGG_BIN_DEFAULT_PORT ?? 7001;
debug('detect available port');
this.port = await detect(defaultPort);
if (this.port !== defaultPort) {
console.warn('[egg-bin] server port %s is in use, now using port %o', defaultPort, this.port);
}
debug(`use available port ${this.port}`);
}
}

return {
baseDir: this.base,
workers: this.workers,
Expand Down
12 changes: 12 additions & 0 deletions test/cmd/dev.test.ts
Expand Up @@ -174,4 +174,16 @@ describe('test/cmd/dev.test.ts', () => {
.end(done);
});
});

describe('obtain the port from config.*.js', () => {
const cwd = path.join(fixtures, 'example-port');
it('should obtain the port from config.default.js', () => {
coffee.fork(eggBin, [ 'dev' ], {
cwd,
})
.expect('stdout', /"port":6001/)
.expect('code', 0)
.end();
});
});
});
7 changes: 7 additions & 0 deletions test/fixtures/example-port/app/router.js
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', ctx => {
ctx.body = 'hi, egg';
});
};
9 changes: 9 additions & 0 deletions test/fixtures/example-port/config/config.default.js
@@ -0,0 +1,9 @@
'use strict';

exports.key = '12345';

exports.cluster = {
listen: {
port: 6001,
},
};
3 changes: 3 additions & 0 deletions test/fixtures/example-port/package.json
@@ -0,0 +1,3 @@
{
"name": "example"
}

0 comments on commit 07e150f

Please sign in to comment.