From 9d90030630c717892f9e62b2f27f5ac9e86d294e Mon Sep 17 00:00:00 2001 From: Gregor Aisch Date: Sat, 16 Jan 2021 02:53:05 +0100 Subject: [PATCH] add https support for testing on localhost (thx @danmenzies-jerram) --- .gitignore | 3 ++- README.md | 14 +++++++++++++ src/server.js | 56 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 530c02b..8f220cd 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ config.json schnack.json npm-debug.log *.sublime-* -.DS_Store \ No newline at end of file +.DS_Store +certs/* \ No newline at end of file diff --git a/README.md b/README.md index e469f30..7885a1b 100644 --- a/README.md +++ b/README.md @@ -152,3 +152,17 @@ This is not a new idea, so there are a few projects that are doing almost the sa - [Isso](https://github.com/posativ/isso/) - Python + SQLite3 - [Mouthful](https://mouthful.dizzy.zone) – Go + Preact +### Developer notes + +If you want to run your Schnack server on https on localhost, add the following section to your `schnack.json`: + +```js +{ + "ssl": { + "certificate_path": "./certs/local.crt", + "certificate_key": "./certs/local.key" + } +} +``` + +If you want to contribute further plugins, check out the source code for the existing plugins first. We happily accept pull requests on [schnack-plugins](https://github.com/schn4ck/schnack-plugins). \ No newline at end of file diff --git a/src/server.js b/src/server.js index f50e801..e9d244a 100755 --- a/src/server.js +++ b/src/server.js @@ -1,4 +1,5 @@ #!/usr/bin/env node +/* eslint no-console: "off" */ const express = require('express'); const app = express(); const cors = require('cors'); @@ -129,7 +130,7 @@ async function run(db) { const action = request.params[1] || request.params[3]; const target_id = +(request.params[0] || request.params[2]); try { - await db.run(queries[action], target_id) + await db.run(queries[action], target_id); reply.send({ status: 'ok' }); } catch (err) { error(err, request, reply); @@ -158,19 +159,16 @@ async function run(db) { site_url: config.get('schnack_host') }); try { - await db.each( - queries.awaiting_moderation, - (err, row) => { - if (err) console.error(err.message); - feed.item({ - title: `New comment on "${row.slug}"`, - description: `A new comment on "${row.slug}" is awaiting moderation`, - url: row.slug + '/' + row.id, - guid: row.slug + '/' + row.id, - date: row.created_at - }); - } - ); + await db.each(queries.awaiting_moderation, (err, row) => { + if (err) console.error(err.message); + feed.item({ + title: `New comment on "${row.slug}"`, + description: `A new comment on "${row.slug}" is awaiting moderation`, + url: row.slug + '/' + row.id, + guid: row.slug + '/' + row.id, + date: row.created_at + }); + }); reply.send(feed.xml({ indent: true })); } catch (err) { console.error(err); @@ -190,7 +188,7 @@ async function run(db) { if (!isAdmin(user)) return reply.status(403).send(request.params); const setting = value ? 1 : 0; try { - await db.run(queries.set_settings, [property, setting]) + await db.run(queries.set_settings, [property, setting]); reply.send({ status: 'ok' }); } catch (err) { error(err, request, reply); @@ -204,8 +202,28 @@ async function run(db) { ); } - var server = app.listen(config.get('port'), config.get('host'), err => { - if (err) throw err; - console.error(`server listening on ${server.address().port}`); - }); + const configSsl = config.get('ssl'); + let server; + + if (configSsl && configSsl.certificate_path) { + const https = require('https'); + const fs = require('fs'); + + const sslOptions = { + key: fs.readFileSync(configSsl.certificate_key), + cert: fs.readFileSync(configSsl.certificate_path), + requestCert: false, + rejectUnauthorized: false + }; + + server = https.createServer(sslOptions, app); + server.listen(config.get('port'), () => { + console.log(`server listening on ${server.address().port}`); + }); + } else { + server = app.listen(config.get('port'), config.get('host'), err => { + if (err) throw err; + console.log(`server listening on ${server.address().port}`); + }); + } }