-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
166 lines (153 loc) · 4.04 KB
/
application.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
'use strict'
var fs = require('fs');
var mkdirp = require('mkdirp');
var Promise = require('promise');
var AppConfig = require('./config');
var EveService = require('./service');
var Application = {
create : function(appid, app_conf, callback) {
var app = {};
app.appid = appid;
app.app_src = "/app_src/";
app.appname = app_conf["appname"];
app.mDb = {};
app.mResources = [],
app.mComponents= [],
app.getComponent= function(acpid, callback) {
if (this.mComponents[acpid]) {
callback(this.mComponents[acpid]);
} else {
var filename = AppConfig.http.root + "/appid_" + this.appid + app.app_src + acpid + ".js";
fs.exists(filename, function(exists) {
if (!exists) {
app.loadResource(app.app_src + acpid+".js", function(rslt, filename) {
if (rslt) {
var moudule = require("./"+filename);
app.mComponents[acpid] = moudule;
callback(moudle);
} else {
callback();
}
});
} else {
var moudule = require("./" + filename);
app.mComponents[acpid] = moudule;
callback(moudule);
}
});
}
};
app.loadResource = function(url, callback){
var path = "/appid_" + this.appid + "/";
if (url.indexOf(path) == 0) {
url = url.substring(path.length-1);
}
if (url == "/") url = "/index.html"
//console.log("SELECT uri, contents from res_t where uri='" +
// url + "' and app_id='" + this.appid + "'");
EveService.sysdb.query("SELECT uri, contents from res_t where uri='" +
url + "' and app_id='" + this.appid + "'", function(err, rows, fields) {
if (err) throw err;
if (rows.length != 1) {
callback(false, "")
return;
}
var filename = AppConfig.http.root + "/appid_" + app.appid + rows[0].uri;
var dirname = filename.substring(0, filename.lastIndexOf('/'));
if (!fs.existsSync(dirname)) {
mkdirp.sync(dirname);
}
fs.writeFile(filename, rows[0].contents, function(err) {
if (err) throw err;
//console.log("write file: " + filename);
callback(true, "/appid_" + app.appid + rows[0].uri);
});
});
},
app.doAction = function(acpid, req, res, callback) {
app.getComponent(acpid, function(cmp) {
if (cmp) {
cmp.doAction(req, res, callback);
} else {
callback(false);
}
});
}
app.status = app_conf["status"];
if (app.status) {
callback(app);
return;
}
function setAppStatus() {
return new Promise(function(resolve, reject){
EveService.sysdb.query("update apps_t set status=1 where app_id=" + app.appid,
function(err, rows, fields) {
if (err) {
reject(err);
}else {
resolve();
}
});
});
};
function doAppInit(cmp) {
return new Promise(function(resolve, reject){
cmp.doAction("oninit", cmp, function(){
resolve(1,2);
});
});
};
function createDefDb() {
return new Promise(function(resolve, reject){
if(app.status = 1) {
reject();
} else {
var dbname = "appid_" + app.appid;
EveService.sysdb.query("create database if not exists appid_" + app.appid + ";" +
"create table if not exisst setting_t( name varcahr(255) ,value varchar(255))",
function(err, rows, fields) {
if (err) {
reject(err);
}else {
app.mDb = mysql.createConnection({
host : AppConfig.sysdb.ip,
port : AppConfig.sysdb.port,
user : AppConfig.sysdb.user,
password : AppConfig.sysdb.passowrd,
database : dbname
});
app.mDb.connect();
resolve();
}
});
}
});
};
function getAppInitCmp() {
return new Promise(function(success, error) {
app.getComponent("app", function(cmp){
if(cmp) {
success(cmp);
} else {
error();
}
});
});
};
createDefDb()
.then(getAppInitCmp)
.then(doAppInit)
.then(setAppStatus)
.then(function(){
callback(app)
})
.catch(function(err){
if (err) {
console.log("Error" + err)
}
callback(app)
});
app.status = 1;
}
};
module.exports = Application;