forked from olegabu/fabric-starter-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-app-manager.js
77 lines (65 loc) · 2.29 KB
/
web-app-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const fs = require('fs');
const path = require('path');
const express = require('express');
const unzip = require('unzipper');
const _ = require('lodash');
const cfg = require('./config.js');
const logger = cfg.log4js.getLogger('FabricStarterClient');
function unlinkFile(path) {
try {
fs.unlinkSync(path);
} catch (e) {
logger.warn(`Error deleting file ${path}`, e);
}
}
class WebAppManager {
async provisionWebAppFromPackage(fileObj) {
const readStream = fs.createReadStream(fileObj.path);
const extractPath = cfg.WEBAPPS_DIR;
if (!fs.existsSync(cfg.WEBAPPS_DIR)){
fs.mkdirSync(cfg.WEBAPPS_DIR);
}
return new Promise((resolve, reject)=>{
const pipe = readStream.pipe(unzip.Extract({path: extractPath})); //TODO: check if folder inside zip have different name
pipe.on('close', async function () {
unlinkFile(fileObj.path);
resolve(extractPath);
});
pipe.on('error', async function () {
unlinkFile(fileObj.path);
reject();
})
})
}
async getWebAppsList() {
return new Promise((resolve, reject) => {
fs.readdir(cfg.WEBAPPS_DIR, (err, fileList) => {
return err ? resolve([]) : resolve(fileList);
})
})
}
async provisionMiddleware(fileObj) {
const readStream = fs.createReadStream(fileObj.path);
const extractPath = cfg.MIDDLWARE_DIR;
if (!fs.existsSync(cfg.MIDDLWARE_DIR)){
fs.mkdir(cfg.MIDDLWARE_DIR);
}
return new Promise((resolve, reject)=> {
const destFile = path.resolve(cfg.MIDDLWARE_DIR, fileObj.originalname);
fs.copyFile(fileObj.path, destFile, err => {
return err ? reject(err): resolve(destFile);
});
});
}
async getMiddlewareList() {
return new Promise((resolve, reject) => {
fs.readdir(cfg.MIDDLWARE_DIR, (err, fileList) => {
return err ? resolve([]) : resolve(fileList);
})
})
}
redeployWebapp(app, appContext, appFolder) {
app.use(`/${cfg.WEBAPPS_DIR}/${appContext}`, express.static(appFolder));
}
}
module.exports = new WebAppManager();