-
Notifications
You must be signed in to change notification settings - Fork 0
/
appfinder.js
166 lines (133 loc) · 5.13 KB
/
appfinder.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var request = require('request');
var utils = require('./utils.js');
var path = require('path');
var fs = require('fs');
var external_apps = {};
var express = require('express');
var evh = require('express-vhost');
var appInventory = {};
var hostile = require('hostile');
function AppFinder () {
this.apps = {};
this.installedApps = [];
var evh = require('express-vhost');
var server = utils.createApp();
this.server = server;
this.api = utils.createApp();
this.evh = evh;
}
AppFinder.prototype.registerApp = function (appDir) {
var manifestFilePath = appDir + path.sep + 'manifest.json';
// check if app is already existing
// check if directory exists
var appURL = '';
console.log(appDir);
if (fs.existsSync(appDir) && fs.existsSync(manifestFilePath)) {
console.log(manifestFilePath);
var manifest = JSON.parse(fs.readFileSync(manifestFilePath));
appName = manifest.BundleIdentifier;
appURL = __dirname + path.sep + 'apps/' + appName + '/';
console.log(manifest);
this.installedApps.push(manifest);
/*hostile.remove('127.0.0.1', appName + '.bungalowapp.com', function (err) {
if (err) {
console.error(err)
} else {
console.log('set /etc/hosts successfully!')
}
})
console.log("Registering app");
hostile.set('127.0.0.1', appName + '.bungalowapp.com', function (err) {
if (err) {
} else {
evh.register(appName + '.bungalowapp.com', www);
console.log('set /etc/hosts successfully!')
}
})*/
}
}
AppFinder.prototype.registerApps = function () {
var appDir = __dirname + path.sep + 'apps';
this.registerDirectory(appDir);
var ownApps = '~' + path.sep + 'Bungalow';
if (fs.existsSync(ownApps)) {
this.registerDirectory(ownApps);
}
}
AppFinder.prototype.registerDirectory = function (appDir) {
var directories = fs.readdirSync(appDir).filter(function(file) {
return fs.statSync(path.join(appDir, file)).isDirectory();
});
for(var i = 0; i < directories.length; i++) {
var directory = directories[i];
this.registerApp(appDir + path.sep + directory);
}
}
AppFinder.prototype.listen = function () {
var self = this;
this.server.get('/api/v1/apps', function (req, res) {
console.log(self.installedApps);
res.json(self.installedApps);
res.end();
});
this.server.get('/*', function (req, res) {
console.log("A");
var host = req.host;
var app_path = req.params[0].split('/');
var appId = app_path[0];
console.log("A");
console.log("APPID", appId);
if (!(appId in external_apps)) {
var address = app_path.slice(1).join('/');
var appDir = __dirname + path.sep + path.sep + 'apps' + path.sep + appId + path.sep;
if (!fs.existsSync(appDir)) {
appDir = '~/Bungalow' + path.sep + appId + path.sep;
}
var manifestFilePath = appDir + path.sep + 'manifest.json';
// check if app is already existing
// check if directory exists
var appURL = '';
if (fs.existsSync(appDir) && fs.existsSync(manifestFilePath)) {
var manifest = JSON.parse(fs.readFileSync(manifestFilePath));
appName = appId;
appURL = __dirname + path.sep + 'apps/' + appName + '/';
var file = appDir + app_path.slice(1).join('/');
self.apps[appName] = manifest;
var data = fs.readFileSync(file);
res.write(data);
res.end();
} else {
// Check if app is available on App Finder
if (false)
request('http://appfinder.aleros.webfactional.com/api/index.php?id=' + appId, function (error, response, body) {
var app = JSON.parse(body);
external_apps[appId] = app.app_url;
var url = external_apps[appId] + app_path.slice(1).join('/');
request(url, function (error, response, body) {
if (!body) body = "";
res.write(body);
res.end();
})
});
var notfound = fs.readFileSync(__dirname + path.sep + 'apps/notfound/index.html');
res.write(notfound);
res.end();
}
} else {
request(external_apps[appId] + '/' + app_path.slice(1).join('/'), function (error, response, body) {
if (error) {
var notfound = fs.readFileSync(__dirname + path.sep + 'apps/notfound/index.html');
res.write(notfound);
res.end();
return;
}
res.write(body);
res.end();
})
}
});
}
var appFinder = new AppFinder();
appFinder.registerApps();
appFinder.listen();
module.exports = appFinder.server;