Skip to content

Commit

Permalink
add https support for testing on localhost (thx @danmenzies-jerram)
Browse files Browse the repository at this point in the history
  • Loading branch information
gka committed Jan 16, 2021
1 parent 9c31858 commit 9d90030
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ config.json
schnack.json
npm-debug.log
*.sublime-*
.DS_Store
.DS_Store
certs/*
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
56 changes: 37 additions & 19 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
/* eslint no-console: "off" */
const express = require('express');
const app = express();
const cors = require('cors');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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}`);
});
}
}

0 comments on commit 9d90030

Please sign in to comment.