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
+22 -13
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
const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
@@ -12,16 +21,17 @@ const cors = require('kcors')
// Local libraries
const config = require('../config') // this first.
// const IPFSLib = require('../src/lib/ipfs')
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 { wlogger } = require('../src/adapters/wlogger')
async function startServer () {
class Server {
constructor () {
this.adminLib = new AdminLib()
}
async startServer () {
try {
// Create a Koa instance.
const app = new Koa()
app.keys = [config.session]
@@ -72,15 +82,14 @@ async function startServer () {
console.log(`Server started on ${config.port}`)
// Create the system admin user.
const success = await adminLib.createSystemUser()
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 = app
module.exports = {
startServer
}
module.exports = Server
+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()
+3 -1
View File
@@ -10,7 +10,7 @@ const axios = require('axios').default
// Local support libraries
const config = require('../../../config')
const app = require('../../../bin/server')
const Server = require('../../../bin/server')
const testUtils = require('../../utils/test-utils')
const AdminLib = require('../../../src/adapters/admin')
const adminLib = new AdminLib()
@@ -22,6 +22,8 @@ const LOCALHOST = `http://localhost:${config.port}`
describe('Auth', () => {
before(async () => {
const app = new Server()
// This should be the first instruction. It starts the REST API server.
await app.startServer()