2023-05-01 17:57:21 +02:00
|
|
|
const express = require('express')
|
|
|
|
|
const Path = require('path')
|
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
|
|
|
|
class ClientRouter {
|
2023-05-02 11:21:49 +02:00
|
|
|
constructor(appRoot, routerBasePath = '') {
|
2023-05-01 17:57:21 +02:00
|
|
|
this.appRoot = appRoot
|
|
|
|
|
this.routerBasePath = routerBasePath
|
|
|
|
|
|
|
|
|
|
this.client = null
|
|
|
|
|
this.router = express()
|
|
|
|
|
this.router.disable('x-powered-by')
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 10:44:32 +02:00
|
|
|
async init () {
|
2023-05-01 17:57:21 +02:00
|
|
|
const clientDir = Path.join(this.appRoot, '/client')
|
2023-05-02 10:44:32 +02:00
|
|
|
const { loadNuxt } = require(Path.resolve(clientDir, 'node_modules/nuxt'))
|
|
|
|
|
this.client = await loadNuxt({ rootDir: clientDir, for: 'start' })
|
|
|
|
|
}
|
2023-05-01 17:57:21 +02:00
|
|
|
|
2023-05-02 10:44:32 +02:00
|
|
|
async start () {
|
|
|
|
|
Logger.info('[Client] Starting')
|
|
|
|
|
await this.init()
|
2023-05-01 17:57:21 +02:00
|
|
|
|
2023-05-02 10:44:32 +02:00
|
|
|
this.router.use(this.client.render)
|
2023-05-01 17:57:21 +02:00
|
|
|
|
2023-05-02 10:44:32 +02:00
|
|
|
this.client.hook('error', (err) => Logger.error('[Client]', err))
|
|
|
|
|
this.client.ready().then(() => Logger.info('[Client] Ready'))
|
2023-05-01 17:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-02 10:44:32 +02:00
|
|
|
async stop () {
|
2023-05-01 17:57:21 +02:00
|
|
|
if (this.client) {
|
2023-05-02 10:44:32 +02:00
|
|
|
await this.client.close()
|
|
|
|
|
Logger.info('[Client] Stopped')
|
2023-05-01 17:57:21 +02:00
|
|
|
} else {
|
|
|
|
|
Logger.error('Client not running')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = ClientRouter
|