2019-09-13 01:03:04 +00:00
|
|
|
const { promisify } = require('util')
|
2019-09-20 01:55:32 +00:00
|
|
|
const path = require('path')
|
2018-09-15 07:01:19 +00:00
|
|
|
const express = require('express');
|
2019-09-13 01:03:04 +00:00
|
|
|
const MongoClient = require('mongodb').MongoClient;
|
2019-09-20 01:55:32 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const routes = require('./routes')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const cors = require('cors')
|
|
|
|
const http = require('http')
|
|
|
|
const https = require('https')
|
|
|
|
const basicAuth = require('express-basic-auth');
|
|
|
|
|
|
|
|
const config = require('./config.json');
|
|
|
|
const { USER, PASS, DOMAIN, KEY_PATH, CERT_PATH, PORT, PORT_HTTPS } = config;
|
|
|
|
|
|
|
|
const app = express();
|
2019-09-13 01:03:04 +00:00
|
|
|
// Connection URL
|
|
|
|
const url = 'mongodb://localhost:27017';
|
|
|
|
|
|
|
|
const dbSetup = require('./db/setup');
|
|
|
|
// Database Name
|
|
|
|
const dbName = 'test';
|
|
|
|
|
|
|
|
// Create a new MongoClient
|
|
|
|
const client = new MongoClient(url, {useUnifiedTopology: true});
|
|
|
|
|
|
|
|
let db;
|
|
|
|
|
2019-09-20 01:55:32 +00:00
|
|
|
|
2018-09-15 07:01:19 +00:00
|
|
|
let sslOptions;
|
|
|
|
|
|
|
|
sslOptions = {
|
2019-09-20 01:55:32 +00:00
|
|
|
key: fs.readFileSync(path.join(__dirname, KEY_PATH)),
|
|
|
|
cert: fs.readFileSync(path.join(__dirname, CERT_PATH))
|
2018-09-15 07:01:19 +00:00
|
|
|
};
|
2019-09-20 01:55:32 +00:00
|
|
|
|
2018-09-15 07:01:19 +00:00
|
|
|
|
|
|
|
app.set('domain', DOMAIN);
|
2019-09-20 01:55:32 +00:00
|
|
|
app.set('port', process.env.PORT || PORT);
|
|
|
|
app.set('port-https', process.env.PORT_HTTPS || PORT_HTTPS);
|
2019-09-13 01:03:04 +00:00
|
|
|
app.use(bodyParser.json({type: [
|
|
|
|
'application/activity+json',
|
|
|
|
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
|
|
|
|
]})); // support json encoded bodies
|
2018-09-15 07:01:19 +00:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
|
|
|
|
|
|
|
|
// basic http authorizer
|
|
|
|
let basicUserAuth = basicAuth({
|
|
|
|
authorizer: asyncAuthorizer,
|
|
|
|
authorizeAsync: true,
|
|
|
|
challenge: true
|
|
|
|
});
|
|
|
|
|
|
|
|
function asyncAuthorizer(username, password, cb) {
|
|
|
|
let isAuthorized = false;
|
|
|
|
const isPasswordAuthorized = username === USER;
|
|
|
|
const isUsernameAuthorized = password === PASS;
|
|
|
|
isAuthorized = isPasswordAuthorized && isUsernameAuthorized;
|
|
|
|
if (isAuthorized) {
|
|
|
|
return cb(null, true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return cb(null, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 01:03:04 +00:00
|
|
|
app.param('name', function (req, res, next, id) {
|
|
|
|
req.user = id
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2018-09-15 07:01:19 +00:00
|
|
|
app.get('/', (req, res) => res.send('Hello World!'));
|
|
|
|
|
|
|
|
// admin page
|
|
|
|
app.options('/api', cors());
|
|
|
|
app.use('/api', cors(), routes.api);
|
|
|
|
app.use('/api/admin', cors({ credentials: true, origin: true }), basicUserAuth, routes.admin);
|
|
|
|
app.use('/.well-known/webfinger', cors(), routes.webfinger);
|
|
|
|
app.use('/u', cors(), routes.user);
|
2018-09-25 03:10:39 +00:00
|
|
|
app.use('/m', cors(), routes.message);
|
2019-09-15 00:00:26 +00:00
|
|
|
app.use('/u/:name/inbox', routes.inbox)
|
|
|
|
app.use('/u/:name/outbox', routes.outbox)
|
2019-09-13 01:03:04 +00:00
|
|
|
app.use('/admin', express.static('public/admin'));
|
|
|
|
app.use('/f', express.static('public/files'));
|
2019-09-20 01:55:32 +00:00
|
|
|
// app.use('/hubs', express.static('../hubs/dist'));
|
2018-09-15 07:01:19 +00:00
|
|
|
|
2019-09-13 01:03:04 +00:00
|
|
|
// Use connect method to connect to the Server
|
|
|
|
client.connect({useNewUrlParser: true})
|
|
|
|
.then(() => {
|
|
|
|
console.log("Connected successfully to server");
|
|
|
|
db = client.db(dbName);
|
|
|
|
app.set('db', db);
|
|
|
|
return dbSetup(db, DOMAIN)
|
|
|
|
})
|
|
|
|
.then(() => {
|
2019-09-20 01:55:32 +00:00
|
|
|
https.createServer(sslOptions, app).listen(app.get('port-https'), function () {
|
|
|
|
console.log('Express server listening on port ' + app.get('port-https'));
|
2019-09-13 01:03:04 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
throw new Error(err)
|
2019-09-09 22:43:06 +00:00
|
|
|
});
|