Skip to content
This repository was archived by the owner on Dec 9, 2022. It is now read-only.
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
7 changes: 5 additions & 2 deletions config.sample.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
config = {
mongodb_server_url: 'mongodb://127.0.0.1:27017/ptpush',
server_port: 3000,
/* enable_https: true,
private_key_path: './cert/private.pem',
certificate_path: './cert/file.crt' */
certificate_path: './cert/file.crt', */
/* auth: {
"file": ""
}, */
server_port: 3000
}

module.exports = config;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"mongoose": "^4.11.7",
"node-schedule": "^1.2.4",
"request": "^2.81.0",
"request-promise-native": "^1.0.4"
"request-promise-native": "^1.0.4",
"http-auth": "^3.2.3"
}
}
19 changes: 17 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');
const Http = require('http');
const Https = require('https');
var auth = require('http-auth');
const fs = require('fs');
const mongoose = require('mongoose');
const subscribeapi = require('./subscribeapi');
Expand Down Expand Up @@ -34,15 +35,29 @@ mongoose.connect(DB_URL, { useMongoClient: true }).then(
let certificate = fs.readFileSync(Config.certificate_path, 'utf8');
let credentials = {key: privateKey, cert: certificate};

let httpsServer = Https.createServer(credentials, app);
if (!Config.auth) {
let basicAuth = auth.basic(Config.auth);
let httpsServer = Https.createServer(basicAuth, credentials, app);
console.log("Http authorization is enabled.");
} else {
let httpsServer = Https.createServer(credentials, app);
}

httpsServer.listen(Config.server_port, () => {
let host = httpsServer.address().address;
let port = httpsServer.address().port;

console.log("Https Server running at https://%s:%s", host, port);
});
} else {
let httpServer = Http.createServer(app);
if (!Config.auth) {
let basicAuth = auth.basic(Config.auth);
let httpServer = Http.createServer(basicAuth, app);
console.log("Http authorization is enabled.");
} else {
let httpServer = Http.createServer(app);
}

httpServer.listen(Config.server_port, () => {
let host = httpServer.address().address;
let port = httpServer.address().port;
Expand Down