feat(server): Converter top-level server library into a class

This commit is contained in:
Chris Troutner
2021-08-08 09:58:24 -07:00
parent 758de6b088
commit 24684ac25a
3 changed files with 66 additions and 54 deletions
+61 -52
View File
@@ -1,3 +1,12 @@
/*
This Koa server has two interfaces:
- REST API over HTTP
- JSON RPC over IPFS
The architecture of the code follows the Clean Architecture pattern:
https://troutsblog.com/blog/clean-architecture
*/
// npm libraries // npm libraries
const Koa = require('koa') const Koa = require('koa')
const bodyParser = require('koa-bodyparser') const bodyParser = require('koa-bodyparser')
@@ -12,75 +21,75 @@ const cors = require('kcors')
// Local libraries // Local libraries
const config = require('../config') // this first. const config = require('../config') // this first.
// const IPFSLib = require('../src/lib/ipfs')
const AdminLib = require('../src/adapters/admin') const AdminLib = require('../src/adapters/admin')
const adminLib = new AdminLib()
// const JSONRPC = require('../src/rpc')
// const rpc = new JSONRPC()
const errorMiddleware = require('../src/controllers/rest-api/middleware/error') const errorMiddleware = require('../src/controllers/rest-api/middleware/error')
const { wlogger } = require('../src/adapters/wlogger') const { wlogger } = require('../src/adapters/wlogger')
async function startServer () { class Server {
// Create a Koa instance. constructor () {
const app = new Koa() this.adminLib = new AdminLib()
app.keys = [config.session] }
// Connect to the Mongo Database. async startServer () {
mongoose.Promise = global.Promise try {
mongoose.set('useCreateIndex', true) // Stop deprecation warning. // Create a Koa instance.
await mongoose.connect(config.database, { const app = new Koa()
useUnifiedTopology: true, app.keys = [config.session]
useNewUrlParser: true
})
// MIDDLEWARE START // Connect to the Mongo Database.
mongoose.Promise = global.Promise
mongoose.set('useCreateIndex', true) // Stop deprecation warning.
await mongoose.connect(config.database, {
useUnifiedTopology: true,
useNewUrlParser: true
})
app.use(convert(logger())) // MIDDLEWARE START
app.use(bodyParser())
app.use(session())
app.use(errorMiddleware())
// Used to generate the docs. app.use(convert(logger()))
app.use(mount('/', serve(`${process.cwd()}/docs`))) app.use(bodyParser())
app.use(session())
app.use(errorMiddleware())
// Mount the page for displaying logs. // Used to generate the docs.
app.use(mount('/logs', serve(`${process.cwd()}/config/logs`))) app.use(mount('/', serve(`${process.cwd()}/docs`)))
// User Authentication // Mount the page for displaying logs.
require('../config/passport') app.use(mount('/logs', serve(`${process.cwd()}/config/logs`)))
app.use(passport.initialize())
app.use(passport.session())
// Attach REST API and JSON RPC controllers to the app. // User Authentication
const Controllers = require('../src/controllers') require('../config/passport')
const controllers = new Controllers() app.use(passport.initialize())
await controllers.attachControllers(app) app.use(passport.session())
app.controllers = controllers // Attach REST API and JSON RPC controllers to the app.
const Controllers = require('../src/controllers')
const controllers = new Controllers()
await controllers.attachControllers(app)
// Enable CORS for testing app.controllers = controllers
// THIS IS A SECURITY RISK. COMMENT OUT FOR PRODUCTION
app.use(cors({ origin: '*' }))
// MIDDLEWARE END // Enable CORS for testing
// THIS IS A SECURITY RISK. COMMENT OUT FOR PRODUCTION
app.use(cors({ origin: '*' }))
console.log(`Running server in environment: ${config.env}`) // MIDDLEWARE END
wlogger.info(`Running server in environment: ${config.env}`)
await app.listen(config.port) console.log(`Running server in environment: ${config.env}`)
console.log(`Server started on ${config.port}`) wlogger.info(`Running server in environment: ${config.env}`)
// Create the system admin user. await app.listen(config.port)
const success = await adminLib.createSystemUser() console.log(`Server started on ${config.port}`)
if (success) console.log('System admin user created.')
return app // Create the system admin user.
const success = await this.adminLib.createSystemUser()
if (success) console.log('System admin user created.')
return app
} catch (err) {
console.error('Could not start server. Error: ', err)
}
}
} }
// startServer()
// export default app module.exports = Server
// module.exports = app
module.exports = {
startServer
}
+2 -1
View File
@@ -1,3 +1,4 @@
const server = require('./bin/server.js') const Server = require('./bin/server.js')
const server = new Server()
server.startServer() server.startServer()
+3 -1
View File
@@ -10,7 +10,7 @@ const axios = require('axios').default
// Local support libraries // Local support libraries
const config = require('../../../config') const config = require('../../../config')
const app = require('../../../bin/server') const Server = require('../../../bin/server')
const testUtils = require('../../utils/test-utils') const testUtils = require('../../utils/test-utils')
const AdminLib = require('../../../src/adapters/admin') const AdminLib = require('../../../src/adapters/admin')
const adminLib = new AdminLib() const adminLib = new AdminLib()
@@ -22,6 +22,8 @@ const LOCALHOST = `http://localhost:${config.port}`
describe('Auth', () => { describe('Auth', () => {
before(async () => { before(async () => {
const app = new Server()
// This should be the first instruction. It starts the REST API server. // This should be the first instruction. It starts the REST API server.
await app.startServer() await app.startServer()