From 3a9aae3f7bdb11019ed20a4749ca3a8fa05465f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=BCbleitner?= Date: Tue, 27 Aug 2019 21:06:44 +0200 Subject: [PATCH] renamed some stuff --- src/backend/FrontblockAdmin.ts | 179 --------------------------------- src/backend/Main.ts | 10 +- src/backend/UpdateManger.ts | 2 +- 3 files changed, 6 insertions(+), 185 deletions(-) delete mode 100644 src/backend/FrontblockAdmin.ts diff --git a/src/backend/FrontblockAdmin.ts b/src/backend/FrontblockAdmin.ts deleted file mode 100644 index 8a79d36..0000000 --- a/src/backend/FrontblockAdmin.ts +++ /dev/null @@ -1,179 +0,0 @@ -'use strict' - -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 { UpdateManager } from './UpdateManger'; -import { FrontblockApi } from 'frontblock-generic/Api'; -import { Plugin } from 'frontblock-generic/Plugin'; -var exec = require('child-process-promise').exec; - -Logger.configure({ - appenders: - { - "admin": { type: 'stdout' }, - //app: { type: 'file', filename: 'application.log' } - }, - categories: - { - default: { appenders: [ 'admin' ], level: 'debug' } - } -}) -const logger = Logger.getLogger("admin") - - -export type AdminConf = { httpPort: number} - -/** - * FrontblockAdmin - * - * The customer-facing dynamic component of the customer backend - * Supports (un)loading plugins which will be communicated to its library component (See FrontblockLib.ts and the info() RPC) - * - * The list of available plugins is published via the frontblock API and downloaded via gitea-releases - */ -export class FrontblockAdmin extends AdminBase{ - - private express - private httpServer - private io = bsock.createServer() - private wsServer = http.createServer() - private updateManager:UpdateManager = new UpdateManager(this) - - constructor(runningPlugins: Plugin[] = []){ - super(runningPlugins) - this.initialize() - } - - getDefaultConfig(): { apiConf: FrontblockApiConf; } & AdminConf & { dbConf:Knex.Config; } { - return { - httpPort: 8080, - apiConf: { - apiHost: "api.testnet.frontblock.me", - apiKey: "", - apiPort: 10001, - tls: false - }, - dbConf: { - client: 'sqlite3', - connection: { - filename: "./data/ApiClient.sqlite" - }, - useNullAsDefault: true - } - } - } - - protected makeApiClient(conf: FrontblockApiConf): FrontblockApi { - // @ts-ignore - if(this.apiClient) this.apiClient.disconnect() - this.apiClient = new FrontblockApiClient(conf) - - // @ts-ignore - this.apiClient.connect() - return this.apiClient - } - - private async initialize(){ - this.addPlugin(this.updateManager) - await this.updateManager.updateAdmin() - await this.updateManager.updatePlatformDependencies() - await this.updateManager.updateDashboard() - await this.updateManager.updateFrontblockLib() - - this.startWebsocket() - this.startWebserver() - } - - 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( ); - }) \ No newline at end of file diff --git a/src/backend/Main.ts b/src/backend/Main.ts index 539c7fc..908f757 100644 --- a/src/backend/Main.ts +++ b/src/backend/Main.ts @@ -5,21 +5,21 @@ var exec = require('child-process-promise').exec; Logger.configure({ appenders: { - "admin": { type: 'stdout' }, + "main": { type: 'stdout' }, //app: { type: 'file', filename: 'application.log' } }, categories: { - default: { appenders: [ 'admin' ], level: 'debug' } + default: { appenders: [ 'main' ], level: 'debug' } } }) -const logger = Logger.getLogger("admin") +const logger = Logger.getLogger("main") logger.info("Checking npm dependencies...") exec("npm i --prefix ./plugins knex@0.19.2 sqlite3@4.1.0").then(process => { logger.debug(process.stdout) - const admin = require("./FrontblockAdmin").FrontblockAdmin - new admin() + const Admin = require("./Admin").FrontblockAdmin + new Admin() }) diff --git a/src/backend/UpdateManger.ts b/src/backend/UpdateManger.ts index 67c7c86..5b9d426 100644 --- a/src/backend/UpdateManger.ts +++ b/src/backend/UpdateManger.ts @@ -3,7 +3,7 @@ import { socketioRPC } from "frontblock-generic/RPC"; import { GitUpdater, RepoFolderStatus } from "./GitUpdater"; import { FrontblockCherryPicker } from "git-cherrypicker"; import * as Logger from 'log4js' -import { FrontblockAdmin } from "./FrontblockAdmin"; +import { FrontblockAdmin } from "./Admin"; import { TableDefiniton } from "frontblock-generic/Admin"; var exec = require('child-process-promise').exec;