-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
224 lines (183 loc) · 6.88 KB
/
server.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var config = require('./config.json');
var AWS = config.AWS;
var MYSQL = config.MYSQL;
var TINIFY = config.TINIFY;
var express = require('express');
var app = express();
var http = require('http').Server(app);
var mysql = require('mysql');
var bodyParser = require("body-parser");
var compression = require('compression')
var tinify = require("tinify");
var connection = mysql.createConnection({
host : MYSQL.HOST,
user : MYSQL.USER,
password : MYSQL.PASSWORD,
database : MYSQL.DATABASE
});
/////////////////////////////////////////////////////////////////////////////////////
/// APP INIT ////////////////////////////////////////////////////////////////////////
/// APP INIT ////////////////////////////////////////////////////////////////////////
appInit();
function appInit()
{
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(compression());
app.use(express.static(__dirname + '/www'));
app.set('port', process.env.PORT || 8100);
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
app.get('/', function (request, response) {
response.sendFile(__dirname + "/www/index.html");
setTimerFlush(response);
});
}
///////////////////////////////////////////////////////////////////////////////////
/// TINIFY ////////////////////////////////////////////////////////////////////////
/// TINIFY ////////////////////////////////////////////////////////////////////////
function tinifyInit(){
console.log("Initialize TinyPNG")
tinify.key = TINIFY.KEY;
}
function tinifyStoreAWS(fileUrl, fileBaseName,product_id,APIResponse){
if(product_id == null || product_id < 0)
{
return queryError("Undefined product id",APIResponse);
}
if(typeof fileUrl != "string")
{
return queryError("Params file_url is not type of string",APIResponse);
}
tinify.fromUrl(fileUrl).store({
service : "s3",
aws_access_key_id : AWS.ACCESS_KEY_ID,
aws_secret_access_key : AWS.SECRECT_ACCESS_KEY,
region : AWS.REGION,
path : AWS.BUCKET_NAME+"/Images/"+fileBaseName
}).then(function(result){
var location = result.headers.location
if(location!= null && location != '')
{
var query = "UPDATE products SET picture_tinify = '"+location+"' WHERE id LIKE "+product_id;
querySuccess(location,APIResponse);
}
});
}
//////////////////////////////////////////////////////////////////////////////////////////////
/// CALLBACK FUNCTION ////////////////////////////////////////////////////////////////////////
/// CALLBACK FUNCTION ////////////////////////////////////////////////////////////////////////
var APICallbackFunction = function(error,results,APIResponse){
if (error)
{
APIResponse.status(500);
return APIResponse.json(error);
}
return APIResponse.json(results);
}
var queryError = function(errorMessage, APIResponse){
console.log("Error");
return APICallbackFunction(errorMessage,null,APIResponse);
}
var querySuccess = function(data,APIResponse){
console.log("Sucess");
return APICallbackFunction(null,data,APIResponse);
}
//the basic call to rest api
//only return one object
var basicQueryCallbackFunction = function(err, rows, fields, APIResponse){
if(err)
return queryError(err,APIResponse);
if(rows == null)
return queryError("Row return null", APIResponse);
if(rows.length == 0)
return queryError("Row return empty", APIResponse);
else
return querySuccess(rows[0],APIResponse);
}
var advancedQueryCallbackFunction = function(err, rows, fields, APIResponse){
if(err)
return queryError(err,APIResponse);
if(rows == null)
return queryError("Row return null", APIResponse);
if(rows.length == 0)
return queryError("Row return empty", APIResponse);
else
return querySuccess(rows,APIResponse);
}
function setTimerFlush (res){
var timer = setInterval(function () {
res.flush()
}, 2000)
res.on('close', function () {
clearInterval(timer)
})
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/// BASIC REST API QUERY ////////////////////////////////////////////////////////////////////////
/// BASIC REST API QUERY ////////////////////////////////////////////////////////////////////////
app.get('/v1/:object/:id', function(request, response, next) {
var id = request.params.id;
var object = request.params.object;
var query = "SELECT * from "+object+" WHERE id LIKE "+id;
runBasicQuery(query,APICallbackFunction, response)
setTimerFlush(response);
});
app.put('/v1/:object/:id', function(request, response, next) {
var id = request.params.id;
var object = request.params.object;
var data = request.body;
for(key in data)
{
console.log(key+"="+data[key]);
}
//var query = "SELECT * from "+object+" WHERE id LIKE "+id;
//runBasicQuery(query,APICallbackFunction, response)
//setTimerFlush(response);
});
function runBasicQuery(query,callback,APIResponse){
connection.query(query,
function(err, rows, fields){
basicQueryCallbackFunction(err, rows, fields, APIResponse);
}
);
}
///////////////////////////////////////////////////////////////////////////////////////////
/// ADVANCED QUERY ////////////////////////////////////////////////////////////////////////
/// ADVANCED QUERY ////////////////////////////////////////////////////////////////////////
app.get('/v1/query/data/:queryName', function(request, response, next) {
var queryName = request.params.queryName;
console.log(request.query.parameters);
var params = JSON.parse(request.query.parameters);
switch(queryName)
{
case "getAllProductByUserId":
var user_id = params.user_id;
var start_from = params.start_from;
var offset = params.offset;
var query = "SELECT * from products"+
" WHERE user_id LIKE "+user_id+
" ORDER BY updated_at DESC"+
" LIMIT "+start_from+", "+offset+";";
runAdvancedQuery(query,APICallbackFunction,response);
break;
case "tinifyStoreAWS":
var file_url = params.file_url;
var file_basename = params.file_basename;
var product_id = params.product_id;
tinifyStoreAWS(file_url,file_basename,product_id,response);
break;
default :
return queryError("Invalid query name",response);
break;
}
setTimerFlush(response);
});
function runAdvancedQuery(query,callback,APIResponse){
connection.query(query,
function(err, rows, fields){
advancedQueryCallbackFunction(err, rows, fields, APIResponse);
}
);
}