Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use a new library to parse command line args #233

Merged
merged 7 commits into from
Dec 27, 2020
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
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,10 @@ downloaded Aspine, and run `npm install`.
+ On Windows, double-click on the script `npminstall.bat`. The `.bat` file
extension may be invisible depending on your system configuration.

- Run the Aspine server.

+ On Unix-like operating systems, run `node . insecure` or `./start.sh` in a
terminal from the directory where you cloned or downloaded Aspine.

+ On Windows, double-click on the script `start.bat`. The `.bat` file
extension may be invisible depending on your system configuration.
- Start the Aspine server by running `npm start -- -i` in a terminal window (or
Command Prompt on Windows). If you are on a Unix-like system, you can run
`npm start -- -i &` to run the server in the background and then run
`killall node` when you want to kill the server.

## Authors

Expand Down
10 changes: 5 additions & 5 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
Expand Up @@ -25,6 +25,7 @@
"animate.css": "^4.1.1",
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.3",
"commander": "^6.2.1",
"compression": "^1.7.4",
"d3-array": "^2.8.0",
"d3-axis": "^2.0.0",
Expand All @@ -38,7 +39,6 @@
"jsdom": "^16.4.0",
"material-icons": "^0.5.1",
"memorystore": "^1.6.4",
"minimist": "^1.2.5",
"node-fetch": "^2.6.1",
"pdfjs-dist": "^2.5.207",
"tabulator-tables": "~4.1.5",
Expand Down
2 changes: 1 addition & 1 deletion restart-serve.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sudo pkill "node"
sudo $NVM_BIN/node ./serve.js &
sudo $NVM_BIN/npm start &
80 changes: 31 additions & 49 deletions serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,54 @@


const express = require('express');
const { program } = require('commander');
const scraper = require('./scrape.js');
const bodyParser = require('body-parser');
const session = require('express-session');
const MemoryStore = require('memorystore')(session)
const crypto = require('crypto');
const http = require('http');
const fs = require('fs');
const https = require('https');
const args = require('minimist')(process.argv.slice(2));
const compression = require('compression');
const child_process = require('child_process');
// -------------------------------------------

if (args.hasOwnProperty('help') || args._.includes('help')) {
console.log(`Usage: ./serve.js [OPTION]...
Starts the Aspine web server.
// Get Aspine version number without leading 'v'
const version = child_process.execSync('git describe')
.toString().trim().match(/^v?(.*)/)[1];

Options:
--insecure do not secure connections with TLS (HTTPS)
--help display this help and exit
`);
process.exit();
}
program
.version(version)
.option('-i, --insecure', 'do not secure connections with TLS (HTTPS)')
.option('-p, --port <number>', 'port to listen on', 8080)
.option('-P, --port-https <number>', 'port to listen on for HTTPS', 4430)
.parse();

// ------------ Web Server -------------------
const port = 8080;

const app = express();
app.use(compression({ filter: (..._) => true }));
app.listen(port, () => console.log(`Aspine listening on port ${port}!`));

if(!(args.hasOwnProperty('insecure') || args._.includes("insecure"))) {
// Certificate
const privateKey = fs.readFileSync('/etc/ssl/certs/private-key.pem', 'utf8');
const certificate = fs.readFileSync('/etc/ssl/certs/public-key.pem', 'utf8');
const ca = fs.readFileSync('/etc/ssl/certs/CA-key.pem', 'utf8');
app.use(compression());
app.listen(program.port, () =>
console.log(`Aspine listening on port ${program.port}!`)
);

if (!program.insecure) {
app.all('*', (req, res, next) => {
if (req.secure) {
return next();
}
// handle port numbers if you need non defaults
res.redirect('https://' + req.hostname + req.url);
}); // at top of routing calls

const credentials = {
key: privateKey,
cert: certificate,
ca: ca
key: fs.readFileSync('/etc/ssl/certs/private-key.pem', 'utf8'),
cert: fs.readFileSync('/etc/ssl/certs/public-key.pem', 'utf8'),
ca: fs.readFileSync('/etc/ssl/certs/CA-key.pem', 'utf8'),
};

app.all('*', ensureSecure); // at top of routing calls

http.createServer(app).listen(8090);
https.createServer(credentials, app).listen(4430, () => { //443
console.log('HTTPS Server running on port 4430'); //443
});

function ensureSecure(req, res, next){
if(req.secure){
// OK, continue
return next();
}
// handle port numbers if you need non defaults
// res.redirect('https://' + req.host + req.url); // express 3.x
res.redirect('https://' + req.hostname + req.url); // express 4.x
}
https.createServer(credentials, app).listen(4430, () =>
console.log(`HTTPS Server running on port ${program.portHttps}`)
);
}

// Expose frontend dependencies from node-modules
Expand Down Expand Up @@ -155,14 +144,7 @@ app.use('/fonts/material-icons/iconfont', express.static(
));

// Endpoint to expose version number to client
app.get('/version', async (req, res) => {
child_process.exec('git describe',
(error, stdout, stderr) => res.send(
// Trim 'v' from version number
stdout.trim().match(/^v?(.*)/)[1]
)
);
});
app.get('/version', (req, res) => res.send(version));

app.use(function(req, res, next) { // enable cors
res.header("Access-Control-Allow-Origin", "*");
Expand Down Expand Up @@ -248,7 +230,7 @@ app.post('/login', async (req, res) => {
});

app.get('/logout', async (req, res) => {
req.session.destroy();
req.session.destroy();
res.redirect('/login');
});

Expand Down
3 changes: 0 additions & 3 deletions start.bat

This file was deleted.

3 changes: 0 additions & 3 deletions start.sh

This file was deleted.