Skip to content

Commit 32be0a2

Browse files
committed
Merge branch 'add-ssl-support' of https://github.com/tanase-sebastian/crontab-ui into tanase-sebastian-add-ssl-support
2 parents 6cf4ce4 + 2c21b0d commit 32be0a2

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ If you need to apply basic HTTP authentication, you can set user name and passwo
4242

4343
Also, you may have to **set permissions** for your `node_modules` folder. Refer [this](https://docs.npmjs.com/getting-started/fixing-npm-permissions).
4444

45+
If you need to use SSL, you can pass the private key and certificat through environment variables:
46+
47+
SSL_CERT=/path/to/ssl_certificate SSL_KEY=/path/to/ssl_private_key
48+
49+
Make sure node has the correct **permissions** to read the certificate and the key.
50+
4551
If you need to autosave your changes to crontab directly:
4652

4753
crontab-ui --autosave

app.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var crontab = require("./crontab");
55
var restore = require("./restore");
66
var moment = require('moment');
77
var basicAuth = require('express-basic-auth');
8+
var http = require('http');
9+
var https = require('https');
810

911
var path = require('path');
1012
var mime = require('mime-types');
@@ -32,6 +34,22 @@ if (BASIC_AUTH_USER && BASIC_AUTH_PWD) {
3234
}))
3335
}
3436

37+
// ssl credentials
38+
var credentials = {
39+
key: process.env.SSL_KEY ? fs.readFileSync(process.env.SSL_KEY) : '',
40+
cert: process.env.SSL_CERT ? fs.readFileSync(process.env.SSL_CERT) : '',
41+
}
42+
43+
if (
44+
(credentials.key && !credentials.cert) ||
45+
(credentials.cert && !credentials.key)
46+
) {
47+
console.error('Please provide both SSL_KEY and SSL_CERT');
48+
process.exit(1);
49+
}
50+
51+
var startHttpsServer = credentials.key && credentials.cert;
52+
3553
// include the routes
3654
var routes = require("./routes").routes;
3755
var routes_relative = require("./routes").relative
@@ -238,7 +256,10 @@ process.on('SIGTERM', function() {
238256
process.exit();
239257
})
240258

241-
app.listen(app.get('port'), app.get('host'), function() {
259+
var server = startHttpsServer ?
260+
https.createServer(credentials, app) : http.createServer(app);
261+
262+
server.listen(app.get('port'), app.get('host'), function() {
242263
console.log("Node version:", process.versions.node);
243264
fs.access(crontab.db_folder, fs.W_OK, function(err) {
244265
if(err){
@@ -277,5 +298,7 @@ app.listen(app.get('port'), app.get('host'), function() {
277298

278299
crontab.reload_db();
279300
}
280-
console.log("Crontab UI is running at http://" + app.get('host') + ":" + app.get('port') + base_url);
301+
302+
var protocol = startHttpsServer ? "https" : "http";
303+
console.log("Crontab UI is running at " + protocol + "://" + app.get('host') + ":" + app.get('port') + base_url);
281304
});

0 commit comments

Comments
 (0)