Skip to content

Commit

Permalink
feat: specify host
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed May 5, 2020
1 parent 377386a commit d635ce6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
10 changes: 8 additions & 2 deletions bin/chii.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ program
.command('start')
.description('starts chi server')
.option('-p, --port <port>', 'set the port to start on. defaults to 3000', parseInt)
.action(command => {
server.start(command.port);
.option('-h, --host <host>', 'set the host. defaults to 0.0.0.0')
.option('-d, --domain <domain>', 'set the domain. defaults to localhost:port')
.action(({ port, host, domain }) => {
server.start({
port,
host,
domain,
});
});

program
Expand Down
10 changes: 6 additions & 4 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ const Koa = require('koa');
const router = require('./middle/router');
const WebSocketServer = require('./lib/WebSocketServer');

function start(port = 8080) {
function start({ port = 8080, host, domain } = {}) {
domain = domain || 'localhost:' + port;

const app = new Koa();
const wss = new WebSocketServer();

app.use(router(wss.channelManager, port));
app.use(router(wss.channelManager, domain));

console.log(`starting server at http://localhost:${port}`);
const server = app.listen(port);
console.log(`starting server at ${domain}`);
const server = host ? app.listen(port, host) : app.listen(port, host);

wss.start(server);
}
Expand Down
5 changes: 3 additions & 2 deletions server/middle/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const pairs = require('licia/pairs');
const reverse = require('licia/reverse');
const map = require('licia/map');

module.exports = function (channelManager, port) {
module.exports = function (channelManager, domain) {
const router = new Router();

router.get('/', async ctx => {
Expand All @@ -21,7 +21,8 @@ module.exports = function (channelManager, port) {
const tpl = await readTpl('index');
ctx.body = tpl({
targets,
port,
protocol: ctx.protocol,
domain,
});
});

Expand Down
2 changes: 1 addition & 1 deletion server/tpl/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
{{else}}
<div class="description">
You can use this script to inject the chii target code into your web page.<br/>
<a target="_blank" href="http://localhost:{{port}}/target.js">http://localhost:{{port}}/target.js</a>
<a target="_blank" href="{{protocol}}://{{domain}}/target.js">{{protocol}}://{{domain}}/target.js</a>
</div>
{{/each}}
<script>
Expand Down

0 comments on commit d635ce6

Please sign in to comment.