rubberguppe/index.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

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')
const bodyParser = require('body-parser')
const cors = require('cors')
const https = require('https')
const routes = require('./routes')
const pub = require('./pub')
const config = require('./config.json')
2019-09-22 05:20:37 +00:00
const { DOMAIN, KEY_PATH, CERT_PATH, PORT, PORT_HTTPS } = config
2019-09-22 05:20:37 +00:00
const app = express()
// Connection URL
2019-09-22 05:20:37 +00:00
const url = 'mongodb://localhost:27017'
2019-09-22 05:20:37 +00:00
const store = require('./store')
// Database Name
2019-09-22 05:20:37 +00:00
const dbName = 'test'
// Create a new MongoClient
2019-09-22 05:20:37 +00:00
const client = new MongoClient(url, { useUnifiedTopology: true })
2019-09-22 05:20:37 +00:00
let db
2019-09-22 05:20:37 +00:00
const sslOptions = {
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-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)
app.use(bodyParser.json({
type: [
'application/activity+json',
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
]
})) // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })) // support encoded bodies
app.param('name', function (req, res, next, id) {
req.user = id
next()
})
2019-09-22 05:20:37 +00:00
app.get('/', (req, res) => res.send('Hello World!'))
2018-09-15 07:01:19 +00:00
// admin page
2019-09-22 05:20:37 +00:00
app.use('/.well-known/webfinger', cors(), routes.webfinger)
app.use('/u', cors(), routes.user)
app.use('/m', cors(), routes.message)
app.use('/u/:name/inbox', routes.inbox)
app.use('/u/:name/outbox', routes.outbox)
2019-09-22 05:20:37 +00:00
app.use('/admin', express.static('public/admin'))
app.use('/f', express.static('public/files'))
// app.use('/hubs', express.static('../hubs/dist'));
2018-09-15 07:01:19 +00:00
// Use connect method to connect to the Server
2019-09-22 05:20:37 +00:00
client.connect({ useNewUrlParser: true })
.then(() => {
2019-09-22 05:20:37 +00:00
console.log('Connected successfully to server')
db = client.db(dbName)
app.set('db', db)
store.connection.setDb(db)
return pub.actor.createLocalActor('dummy', 'Person')
})
.then(dummy => {
return store.setup(DOMAIN, dummy)
})
.then(() => {
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'))
})
})
.catch(err => {
throw new Error(err)
2019-09-22 05:20:37 +00:00
})