SSR App
This commit is contained in:
+28
-69
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
import { getLogger } from 'frontblock-generic/Types';
|
||||
import { getLogger, Logger, } from "log4js";
|
||||
import { promises as fs, mkdirSync } from "fs"
|
||||
import { RPCServer } from 'rpclibrary'
|
||||
import * as Path from 'path'
|
||||
@@ -22,12 +22,7 @@ import { Injector } from '../Injector/Injector';
|
||||
import { Shoutbox } from '../Components/Shoutbox/Shoutbox';
|
||||
import { PubSub } from '../Components/PubSub/PubSub';
|
||||
|
||||
//import { ngExpressEngine } from '../../frontend/node_modules/@nguniversal/express-engine';
|
||||
//import { APP_BASE_HREF } from '../../frontend/';
|
||||
//import { AppServerModule } from './app.server.module';
|
||||
|
||||
|
||||
const logger = getLogger("admin", 'debug')
|
||||
getLogger().level = 'debug'
|
||||
|
||||
@RootComponent({
|
||||
injectable: IAdmin,
|
||||
@@ -73,15 +68,15 @@ implements TableDefinitionExporter, IAdmin {
|
||||
await this.makeKnex()
|
||||
this.startWebsocket()
|
||||
await Promise.all( this.frontworkComponents.map(c => c.initialize?c.initialize():undefined ))
|
||||
logger.debug(this.frontworkComponents.length+" components initialized")
|
||||
this.startWebserver()
|
||||
getLogger('Admin#start').debug(this.frontworkComponents.length+" components initialized")
|
||||
await this.startWebserver()
|
||||
}
|
||||
|
||||
stop(){
|
||||
Promise.all([
|
||||
...this.frontworkComponents.map(c => c.stop?c.stop():undefined ),
|
||||
])
|
||||
.catch(e => logger.warn(e))
|
||||
.catch(e => getLogger('Admin#stop').warn(e))
|
||||
.finally(() => {
|
||||
this.rpcServer.destroy()
|
||||
process.exit(0)
|
||||
@@ -123,92 +118,56 @@ implements TableDefinitionExporter, IAdmin {
|
||||
], {
|
||||
visibility: '0.0.0.0'
|
||||
})
|
||||
logger.debug("Websocket up on", 20000)
|
||||
getLogger('Admin#startWebsocket').debug("Websocket up on", 20000)
|
||||
}
|
||||
|
||||
private startWebserver(){
|
||||
private async startWebserver(){
|
||||
if(this.httpServer != null || this.express != null){
|
||||
logger.warn("Webserver is already running")
|
||||
getLogger('Admin#startWebserver').warn("Webserver is already running")
|
||||
return
|
||||
}
|
||||
|
||||
let port:number = this.config.getConfig().httpPort
|
||||
this.express = express()
|
||||
|
||||
/*
|
||||
this.express.engine('html', ngExpressEngine({
|
||||
bootstrap: AppServerModule,
|
||||
}));
|
||||
|
||||
this.express.set('view engine', 'html');
|
||||
this.express.set('views', 'static');
|
||||
|
||||
// Serve static files from /browser
|
||||
this.express.get('*.*', express.static('static', {
|
||||
maxAge: '1y'
|
||||
}));
|
||||
const distFolder = "../../../../dist"
|
||||
const ngExpressServer = Path.join(__dirname, distFolder, 'server.js')
|
||||
|
||||
this.express.get("/", (request, response) => {
|
||||
response.status(200)
|
||||
response.sendFile('index.html');
|
||||
this.express.get('*.*', (req, res)=>{
|
||||
//console.log('*.*', req.path);
|
||||
res.sendFile(Path.join(__dirname, distFolder,'browser',decodeURIComponent(req.path)))
|
||||
})
|
||||
|
||||
// All regular routes use the Universal engine
|
||||
this.express.get('*', (req, res) => {
|
||||
res.status(301)
|
||||
res.redirect('/')
|
||||
//res.render('index', { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
|
||||
});
|
||||
*/
|
||||
/* */
|
||||
// get the compiled FrontendPlugins.js
|
||||
this.express.use('/', express.static('static'))
|
||||
try{
|
||||
const req = require(distFolder+"/server.js")
|
||||
await req.attachExpress(this.express)
|
||||
getLogger('Admin#startWebserver').info('Frontend from '+ngExpressServer+" loaded")
|
||||
}catch(e){
|
||||
getLogger('Admin#startWebserver').error(e)
|
||||
getLogger('Admin#startWebserver').warn("No angular SSR module was provided in "+ngExpressServer)
|
||||
getLogger('Admin#startWebserver').warn("This is not fatal, but your page will not render on *"+port)
|
||||
}
|
||||
|
||||
this.express.get('/plugins/:id'+".js", async (request, response) => {
|
||||
const pth = Path.resolve("plugins/"+request.params.id, "FrontendPlugin.js");
|
||||
const file = await fs.readFile(pth)
|
||||
const frontend = file.toString()
|
||||
|
||||
response.status(200)
|
||||
response.set('Content-Type', 'application/javascript')
|
||||
response.send(frontend)
|
||||
this.express.listen(port, () => {
|
||||
getLogger('Admin#startWebserver').info('Admin panel listening for HTTP on *'+port)
|
||||
})
|
||||
|
||||
// serve the index.html from the static folder
|
||||
this.express.get("/", (request, response) => {
|
||||
response.status(200)
|
||||
response.sendFile('index.html');
|
||||
})
|
||||
|
||||
// redirect all the other traffic to the single page app
|
||||
this.express.get("*", (request, response) => {
|
||||
response.status(301)
|
||||
response.redirect('/')
|
||||
})
|
||||
|
||||
this.httpServer = new http.Server(this.express)
|
||||
this.httpServer.listen(port, () => {
|
||||
logger.info('Admin panel listening for HTTP on *'+port)
|
||||
})
|
||||
|
||||
/* */
|
||||
}
|
||||
|
||||
private stopWebserver(){
|
||||
if(this.httpServer == null || this.express == null){
|
||||
logger.warn("Webserver is not running")
|
||||
getLogger('Admin#stopWebserver').warn("Webserver is not running")
|
||||
return
|
||||
}
|
||||
this.httpServer.close()
|
||||
this.httpServer = null
|
||||
this.express = null
|
||||
logger.info("Webserver stopped")
|
||||
getLogger('Admin#stopWebserver').info("Webserver stopped")
|
||||
}
|
||||
|
||||
async makeKnex():Promise<Knex>{
|
||||
const conf:Knex.Config = this.config.getConfigKey("dbConf")
|
||||
|
||||
logger.debug("Making new knex:", conf)
|
||||
getLogger('Admin#makeKnex').debug("Making new knex:", conf)
|
||||
if(conf.client === 'sqlite3'){
|
||||
mkdirSync(Path.dirname((<any>conf.connection).filename), {recursive: true})
|
||||
}
|
||||
@@ -237,6 +196,6 @@ implements TableDefinitionExporter, IAdmin {
|
||||
}
|
||||
|
||||
process.on( 'SIGINT', function() {
|
||||
logger.info("Shutting down from SIGINT (Ctrl-C)" );
|
||||
getLogger('process#SIGINT').info("Shutting down from SIGINT (Ctrl-C)" );
|
||||
Injector.resolve<FrontworkAdmin>(FrontworkAdmin).stop()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user