2019-09-20 01:55:32 +00:00
|
|
|
const path = require('path')
|
2019-09-22 05:20:37 +00:00
|
|
|
const express = require('express')
|
|
|
|
const MongoClient = require('mongodb').MongoClient
|
|
|
|
const fs = require('fs')
|
2019-09-20 01:55:32 +00:00
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const cors = require('cors')
|
|
|
|
const https = require('https')
|
2019-09-28 03:24:35 +00:00
|
|
|
const morgan = require('morgan')
|
2019-10-04 02:47:44 +00:00
|
|
|
const history = require('connect-history-api-fallback')
|
2019-09-20 01:55:32 +00:00
|
|
|
|
2019-09-22 05:05:30 +00:00
|
|
|
const routes = require('./routes')
|
|
|
|
const pub = require('./pub')
|
2019-09-22 05:20:37 +00:00
|
|
|
const store = require('./store')
|
2019-09-28 03:17:54 +00:00
|
|
|
const net = require('./net')
|
2019-09-25 03:12:31 +00:00
|
|
|
const { DOMAIN, KEY_PATH, CERT_PATH, CA_PATH, PORT, PORT_HTTPS, DB_URL, DB_NAME } = require('./config.json')
|
2019-09-13 01:03:04 +00:00
|
|
|
|
2019-09-24 03:18:35 +00:00
|
|
|
const app = express()
|
2019-09-28 03:17:54 +00:00
|
|
|
|
2019-09-24 03:18:35 +00:00
|
|
|
const client = new MongoClient(DB_URL, { useUnifiedTopology: true, useNewUrlParser: true })
|
2019-09-13 01:03:04 +00:00
|
|
|
|
2019-09-22 05:20:37 +00:00
|
|
|
const sslOptions = {
|
|
|
|
key: fs.readFileSync(path.join(__dirname, KEY_PATH)),
|
2019-09-25 03:12:31 +00:00
|
|
|
cert: fs.readFileSync(path.join(__dirname, CERT_PATH)),
|
|
|
|
ca: CA_PATH ? fs.readFileSync(path.join(__dirname, CA_PATH)) : undefined
|
2018-09-15 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-22 05:20:37 +00:00
|
|
|
app.set('domain', DOMAIN)
|
|
|
|
app.set('port', process.env.PORT || PORT)
|
|
|
|
app.set('port-https', process.env.PORT_HTTPS || PORT_HTTPS)
|
2019-09-28 03:24:35 +00:00
|
|
|
app.use(morgan('combined'))
|
2019-10-04 02:47:44 +00:00
|
|
|
app.use(history({
|
2019-11-18 01:31:58 +00:00
|
|
|
index: '/web/index.html',
|
|
|
|
rewrites: [
|
|
|
|
// do not redirect webfinger et c.
|
|
|
|
{ from: /^\/\.well-known\//, to: context => context.request.originalUrl }
|
|
|
|
]
|
2019-10-04 02:47:44 +00:00
|
|
|
}))
|
2019-09-22 05:20:37 +00:00
|
|
|
app.use(bodyParser.json({
|
2019-09-28 03:17:54 +00:00
|
|
|
type: pub.consts.jsonldTypes
|
2019-09-22 05:20:37 +00:00
|
|
|
})) // support json encoded bodies
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
|
|
|
|
|
2019-09-13 01:03:04 +00:00
|
|
|
app.param('name', function (req, res, next, id) {
|
|
|
|
req.user = id
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2019-09-28 03:17:54 +00:00
|
|
|
// json only routes
|
2019-09-28 04:28:12 +00:00
|
|
|
app.use('/.well-known/webfinger', cors(), routes.webfinger)
|
2019-09-28 03:17:54 +00:00
|
|
|
app.use('/o', net.validators.jsonld, cors(), routes.object)
|
|
|
|
app.use('/s', net.validators.jsonld, cors(), routes.stream)
|
|
|
|
app.use('/u/:name/inbox', net.validators.jsonld, routes.inbox)
|
|
|
|
app.use('/u/:name/outbox', net.validators.jsonld, routes.outbox)
|
2019-09-24 03:18:35 +00:00
|
|
|
app.use('/u', cors(), routes.user)
|
2019-09-28 03:17:54 +00:00
|
|
|
|
|
|
|
// html/static routes
|
2019-09-22 05:20:37 +00:00
|
|
|
app.use('/f', express.static('public/files'))
|
2019-10-04 02:47:44 +00:00
|
|
|
app.use('/web', express.static('web/dist'))
|
2018-09-15 07:01:19 +00:00
|
|
|
|
2019-12-25 21:48:09 +00:00
|
|
|
// error logging
|
|
|
|
app.use(function (err, req, res, next) {
|
|
|
|
console.error(err.message, req.body, err.stack)
|
|
|
|
res.status(500).send('An error occurred while processing the request')
|
|
|
|
})
|
|
|
|
|
2019-09-22 05:20:37 +00:00
|
|
|
client.connect({ useNewUrlParser: true })
|
2019-09-13 01:03:04 +00:00
|
|
|
.then(() => {
|
2019-09-24 03:18:35 +00:00
|
|
|
console.log('Connected successfully to db')
|
|
|
|
const db = client.db(DB_NAME)
|
2019-09-22 05:20:37 +00:00
|
|
|
app.set('db', db)
|
2019-09-23 17:52:22 +00:00
|
|
|
store.connection.setDb(db)
|
2019-09-22 05:05:30 +00:00
|
|
|
return pub.actor.createLocalActor('dummy', 'Person')
|
|
|
|
})
|
|
|
|
.then(dummy => {
|
2019-09-23 17:52:22 +00:00
|
|
|
return store.setup(DOMAIN, dummy)
|
2019-09-13 01:03:04 +00:00
|
|
|
})
|
|
|
|
.then(() => {
|
2019-09-20 01:55:32 +00:00
|
|
|
https.createServer(sslOptions, app).listen(app.get('port-https'), function () {
|
2019-09-22 05:20:37 +00:00
|
|
|
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-22 05:20:37 +00:00
|
|
|
})
|