self update and serve static/dist

This commit is contained in:
peter
2019-08-28 00:24:30 +02:00
parent fc0bae15ed
commit 997ae629b6
5 changed files with 15 additions and 116 deletions
+3 -103
View File
@@ -4,16 +4,11 @@ import * as Logger from 'log4js'
import * as Knex from 'knex'
import { FrontblockApiClient, FrontblockApiConf } from 'frontblock';
import { AdminBase } from 'frontblock-generic/Admin';
import express = require('express');
import http = require('http');
import bsock = require('bsock');
import { promises as fs } from "fs"
import * as path from "path"
import { FrontblockApi } from 'frontblock-generic/Api';
import { Plugin } from 'frontblock-generic/Plugin';
import { socketioRPC } from 'frontblock-generic/RPC';
import { GitUpdater } from './GitUpdater';
var exec = require('child-process-promise').exec;
import { UpdateManager } from './UpdateManger';
Logger.configure({
appenders:
@@ -33,14 +28,9 @@ export type AdminConf = { httpPort: number}
export class FrontblockAdmin extends AdminBase<AdminConf>{
private express
private httpServer
private io = bsock.createServer()
private wsServer = http.createServer()
constructor(runningPlugins: Plugin[] = []){
super(runningPlugins)
this.initialize()
this.addPlugin(new UpdateManager(this))
}
getDefaultConfig(): { apiConf: FrontblockApiConf; } & AdminConf & { dbConf:Knex.Config; } {
@@ -72,16 +62,6 @@ export class FrontblockAdmin extends AdminBase<AdminConf>{
return this.apiClient
}
private initialize(){
this.startWebserver()
this.startWebsocket()
}
private destroy(){
this.wsServer.close()
this.stopWebserver()
}
exportRPCs():socketioRPC[]{
return [
...super.exportRPCs(),
@@ -107,90 +87,10 @@ export class FrontblockAdmin extends AdminBase<AdminConf>{
}
return status
}
private startWebserver(){
if(this.httpServer != null || this.express != null){
logger.warn("Webserver is already running")
return
}
let port:number = this.getConfig().httpPort
this.express = express()
this.express.use('/', express.static('static'))
/**
* get the compiled FrontendPlugins.js
*/
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)
})
/**
* 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 to the main entry point from where
* a webpacked and rolled up index.html is serverd
* and angular takes over routing
*/
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")
return
}
this.httpServer.close()
this.httpServer = null
this.express = null
logger.info("Webserver stopped")
}
private startWebsocket(){
try{
this.io.attach(this.wsServer)
this.io.on('socket', (socket) => {
logger.info("New Websocket connection on port", socket.port)
const handleError = (e: any) => {
logger.info("Websocket closing", String(e))
socket.close()
}
socket.on('error', handleError)
this.initApis(socket)
})
logger.info('Admin websocket listening on *20000')
this.wsServer.listen(20000)
}catch(e){
logger.error(String(e))
}
}
}
process.on( 'SIGINT', function() {
logger.info( "Gracefully shutting down from SIGINT (Ctrl-C)" );
// some other closing procedures go here
process.exit( );
})
})
+4 -4
View File
@@ -21,11 +21,11 @@ export type NPMVersion = string
export const install = (plugins: Plugin[] = []) => {
const npmPkgs = [['sqlite3', '4.1.0'], ['knex', '0.19.2']]
const deps = npmPkgs.map((pkg) => {return pkg[0] + '@' + pkg[1]} )
logger.info("Checking npm dependencies..." + deps)
const npmPkgs:[NPMPkgName, NPMVersion][] = [['sqlite3', '4.1.0'], ['knex', '0.19.2']]
const deps = npmPkgs.map(tuple => tuple.join('@') ).join(" ")
logger.info("Installing plaform dependencies: "+deps)
exec("npm i --prefix ./plugins " + deps.join(' ')).then(process => {
exec("npm i --prefix ./plugins " + deps).then(process => {
logger.debug(process.stdout)
const Admin = require("./Admin").FrontblockAdmin