Skip to content

Commit

Permalink
feat: support https liriliri#25
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Aug 2, 2022
1 parent cde8f38 commit d2b916e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
14 changes: 6 additions & 8 deletions bin/chii.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ program
.option('-h, --host <host>', 'set the host. defaults to 0.0.0.0')
.option('-d, --domain <domain>', 'set the domain. defaults to localhost:port')
.option('--cdn <cdn>', 'use cdn like jsdelivr')
.action(({ port, host, domain, cdn }) => {
server.start({
port,
host,
domain,
cdn,
});
.option('--https', 'serve chii over https')
.option('--ssl-cert <cert>', 'provide an ssl certificate')
.option('--ssl-key <key>', 'provide an ssl key')
.action(options => {
server.start(options);
});

program
.command('help [command]')
.description('display help information for a command')
.action(command => {
let cmd = program.commands.find(c => c.name() === command) || program;
const cmd = program.commands.find(c => c.name() === command) || program;
cmd.help();
});

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"dev": "webpack --mode=development -w",
"dev:front_end": "cd devtools/devtools-frontend && gn gen out/Default && autoninja -C out/Default && lsla shx cp -R out/Default/gen/front_end ../../public",
"lint": "npm run lint:server && npm run lint:target",
"lint:server": "eslint server/**/*.js",
"lint:target": "tslint target/**/*.ts",
"lint:server": "eslint \"server/**/*.js\" \"bin/*.js\"",
"lint:target": "tslint \"target/**/*.ts\"",
"init:front_end": "cd devtools && rm -rf devtools-frontend && gclient sync --with_branch_heads --verbose && cd ../ && python3 script/apply_all_patches.py patches/config.json",
"es5": "es-check es5 public/target.js"
},
Expand Down
24 changes: 20 additions & 4 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const Koa = require('koa');
const https = require('https');

const router = require('./middle/router');
const compress = require('./middle/compress');
const util = require('./lib/util');
const fs = require('licia/fs');
const WebSocketServer = require('./lib/WebSocketServer');

function start({ port = 8080, host, domain, server, cdn } = {}) {
async function start({ port = 8080, host, domain, server, cdn, https: useHttps, sslCert, sslKey } = {}) {
domain = domain || 'localhost:' + port;

const app = new Koa();
Expand All @@ -18,9 +20,23 @@ function start({ port = 8080, host, domain, server, cdn } = {}) {
wss.start(server);
} else {
util.log(`starting server at ${domain}`);
const server = app.listen(port, host);

wss.start(server);
if (useHttps) {
const cert = await fs.readFile(sslCert, 'utf8');
const key = await fs.readFile(sslKey, 'utf8');
const server = https
.createServer(
{
key,
cert,
},
app.callback()
)
.listen(port, host);
wss.start(server);
} else {
const server = app.listen(port, host);
wss.start(server);
}
}
}

Expand Down

0 comments on commit d2b916e

Please sign in to comment.