Skip to content
This repository was archived by the owner on Sep 14, 2022. It is now read-only.

Support custom host and port for editor #219

Merged
merged 1 commit into from
Jun 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/swagger-project.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ app
app
.command('edit [directory]')
.description('open Swagger editor for this project or the specified project directory')
.option('-s --silent', 'do not open the browser')
.option('-s, --silent', 'do not open the browser')
.option('--host <host>', 'the hostname the editor is served from')
.option('-p, --port <port>', 'the port the editor is served from')
.action(execute(project.edit));

app
Expand Down
2 changes: 2 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Options:

* -h, --help: Outputs usage information.
* -s, --silent: Do not open the browser.
* --host <host>: The hostname the editor is served from (default: 127.0.0.1).
* -p, --port <port>: The port the editor is served from (default: random port).

Example:

Expand Down
10 changes: 6 additions & 4 deletions lib/commands/project/swagger_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ function edit(project, options, cb) {

var http = require('http');
var server = http.createServer(app);
server.listen(0, '127.0.0.1', function() {
var port = server.address().port;
var editorUrl = util.format('http://127.0.0.1:%d/#/edit', port);
var editApiUrl = util.format('http://127.0.0.1:%d/editor/spec', port);
var port = options.port || 0;
var hostname = options.host || '127.0.0.1';
server.listen(port, hostname, function() {
port = server.address().port;
var editorUrl = util.format('http://%s:%d/#/edit', hostname, port);
var editApiUrl = util.format('http://%s:%d/editor/spec', hostname, port);
var dontKillMessage = 'Do not terminate this process or close this window until finished editing.';
emit('Starting Swagger Editor.');

Expand Down
18 changes: 17 additions & 1 deletion test/commands/project/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,30 @@ describe('project', function() {
});
});

it('edit should exec editor with --silent flag', function(done) {
it('edit should exec editor with --silent flag', function(done) {
project.edit(projPath, {silent: true}, function(err) {
should.not.exist(err);
should(didEdit).true;
done();
});
});

it('edit should exec editor with --host parameter', function(done) {
project.edit(projPath, {host: 'somehost'}, function(err) {
should.not.exist(err);
should(didEdit).true;
done();
});
});

it('edit should exec editor with --port parameter', function(done) {
project.edit(projPath, {port: '8080'}, function(err) {
should.not.exist(err);
should(didEdit).true;
done();
});
});

it('open should exec browser', function(done) {
project.open(projPath, {}, function(err) {
should.not.exist(err);
Expand Down
20 changes: 20 additions & 0 deletions test/commands/project/swagger_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ describe('swagger editor', function() {
var projPath;
var swaggerFile;
var baseUrl;
var hostname;
var port;

before(function(done) {
tmp.setGracefulCleanup();
Expand All @@ -78,6 +80,8 @@ describe('swagger editor', function() {
url.hash = null;
url.query = null;
baseUrl = Url.format(url);
hostname = url.hostname;
port = url.port;
cb(new Error());
}
}
Expand Down Expand Up @@ -105,6 +109,22 @@ describe('swagger editor', function() {
});
});

it('should be able to run on a custom host and port', function(done) {
var fileContents = fs.readFileSync(swaggerFile, 'utf8');

project.read(projPath, {}, function(err, project) {
if (err) {
cb(err);
}

editor.edit(project, { host: 'localhost', port: '1234' }, function () {
hostname.should.equal('localhost');
port.should.equal('1234');
done();
});
});
});

it('should be able to save swagger', function(done) {
var url = Url.resolve(baseUrl, SWAGGER_EDITOR_SAVE_PATH);
request
Expand Down