1
-
2
1
/*
3
2
# CloudBoost - Core Engine that powers Bakend as a Service
4
3
# (c) 2014 HackerBay, Inc.
8
7
module . exports = function ( ) {
9
8
10
9
//create a new app.
11
- global . app . post ( '/app/:appId' , function ( req , res ) {
10
+ global . app . post ( '/app/:appId' , function ( req , res ) {
12
11
13
12
console . log ( "++++ Create App API ++++++" ) ;
14
13
15
- try {
16
- console . log ( "SecureKey to create app:" + req . body . secureKey ) ;
14
+ try {
15
+ console . log ( "SecureKey to create app:" + req . body . secureKey ) ;
17
16
18
17
var appId = req . params . appId ;
19
- console . log ( "App ID : " + appId ) ;
18
+ console . log ( "App ID : " + appId ) ;
20
19
21
20
var sdk = req . body . sdk || "REST" ;
22
21
23
22
if ( global . keys . secureKey === req . body . secureKey ) {
24
23
console . log ( "Secure Key Valid. Creating app..." ) ;
25
- global . appService . createApp ( appId ) . then ( function ( app ) {
24
+ global . appService . createApp ( appId ) . then ( function ( app ) {
26
25
27
- global . appService . createDefaultTables ( appId ) . then ( function ( ) {
26
+ global . appService . createDefaultTables ( appId ) . then ( function ( ) {
28
27
console . log ( "Success : App Successfully Created." ) ;
29
28
res . status ( 200 ) . send ( app ) ;
30
- } , function ( err ) {
29
+ } , function ( err ) {
31
30
console . log ( "Error : Cannot create an app." ) ;
32
31
console . log ( err ) ;
33
32
res . status ( 500 ) . send ( err ) ;
34
33
} )
35
34
36
- } , function ( err ) {
35
+ } , function ( err ) {
37
36
console . log ( "Error : Cannot create an app." ) ;
38
37
console . log ( err ) ;
39
38
res . status ( 500 ) . send ( err ) ;
@@ -43,45 +42,45 @@ module.exports = function() {
43
42
res . status ( 401 ) . send ( "Unauthorized" ) ;
44
43
}
45
44
46
- global . apiTracker . log ( appId , "App / Create" , req . url , sdk ) ;
45
+ global . apiTracker . log ( appId , "App / Create" , req . url , sdk ) ;
47
46
48
- } catch ( e ) {
47
+ } catch ( e ) {
49
48
console . log ( e ) ;
50
49
}
51
50
} ) ;
52
51
53
52
//delete app.
54
- global . app . delete ( '/app/:appId' , _deleteApp ) ;
53
+ global . app . delete ( '/app/:appId' , _deleteApp ) ;
55
54
global . app . put ( '/app/:appId' , _deleteApp ) ;
56
55
57
- function _deleteApp ( req , res ) { //delete the app and all of its data.
56
+ function _deleteApp ( req , res ) { //delete the app and all of its data.
58
57
console . log ( '+++++++++++++ APP DELETE HANDLER +++++++++++' ) ;
59
58
var appId = req . params . appId ;
60
59
var sdk = req . body . sdk || "REST" ;
61
60
62
61
var body = req . body || { } ;
63
-
64
- if ( global . keys . secureKey === body . secureKey ) {
62
+ var deleteReason = body . deleteReason ;
63
+ if ( global . keys . secureKey === body . secureKey ) {
65
64
console . log ( "Authorized" ) ;
66
65
//delete all code here.
67
- global . appService . deleteApp ( appId ) . then ( function ( ) {
66
+ global . appService . deleteApp ( appId , deleteReason ) . then ( function ( ) {
68
67
console . log ( "App deleted" ) ;
69
- return res . status ( 200 ) . send ( { status : 'Success' } ) ;
70
- } , function ( ) {
68
+ return res . status ( 200 ) . send ( { status : 'Success' } ) ;
69
+ } , function ( ) {
71
70
console . log ( "Internal Server Error" ) ;
72
- return res . status ( 500 ) . send ( { status : 'Error' } ) ;
71
+ return res . status ( 500 ) . send ( { status : 'Error' } ) ;
73
72
} ) ;
74
- } else {
73
+ } else {
75
74
console . log ( "Unauthorized" ) ;
76
- return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
75
+ return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
77
76
}
78
77
79
78
global . apiTracker . log ( appId , "App / Delete" , req . url , sdk ) ;
80
79
81
80
}
82
81
83
82
//delete a table.
84
- global . app . delete ( '/app/:appId/:tableName' , _deleteTable ) ;
83
+ global . app . delete ( '/app/:appId/:tableName' , _deleteTable ) ;
85
84
86
85
function _deleteTable ( req , res ) { //delete the app and all of its data.
87
86
@@ -96,23 +95,23 @@ module.exports = function() {
96
95
97
96
var appKey = req . body . key || req . params . key ;
98
97
99
- global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
98
+ global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
100
99
if ( isMasterKey ) {
101
100
//delete all code here.
102
- global . appService . deleteTable ( appId , tableName ) . then ( function ( table ) {
101
+ global . appService . deleteTable ( appId , tableName ) . then ( function ( table ) {
103
102
res . status ( 200 ) . send ( table ) ;
104
- } , function ( error ) {
103
+ } , function ( error ) {
105
104
console . log ( "Table Delete Error" ) ;
106
105
console . log ( error ) ;
107
106
res . status ( 500 ) . send ( 'Cannot delete table at this point in time. Please try again later.' ) ;
108
107
} ) ;
109
108
} else {
110
- res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
109
+ res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
111
110
}
112
- } , function ( error ) {
111
+ } , function ( error ) {
113
112
return res . status ( 500 ) . send ( 'Cannot retrieve security keys.' ) ;
114
113
} ) ;
115
- } catch ( e ) {
114
+ } catch ( e ) {
116
115
console . log ( "Delete Table Error" ) ;
117
116
console . log ( e ) ;
118
117
return res . status ( 500 ) . send ( 'Cannot delete table.' ) ;
@@ -122,15 +121,15 @@ module.exports = function() {
122
121
}
123
122
124
123
//create a table.
125
- global . app . put ( '/app/:appId/:tableName' , function ( req , res ) {
124
+ global . app . put ( '/app/:appId/:tableName' , function ( req , res ) {
126
125
127
126
console . log ( "Create or Delete table Api..." ) ;
128
127
129
- if ( req . body && req . body . method == "DELETE" ) {
128
+ if ( req . body && req . body . method == "DELETE" ) {
130
129
/***************************DELETE******************************/
131
- _deleteTable ( req , res ) ;
130
+ _deleteTable ( req , res ) ;
132
131
/***************************DELETE******************************/
133
- } else {
132
+ } else {
134
133
135
134
/***************************UPDATE******************************/
136
135
console . log ( '++++++++ UPDATE TABLE API +++++++++' ) ;
@@ -141,37 +140,37 @@ module.exports = function() {
141
140
var sdk = req . body . sdk || "REST" ;
142
141
var appKey = req . body . key || req . params . key ;
143
142
144
- global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
145
- if ( isMasterKey ) {
143
+ global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
144
+ if ( isMasterKey ) {
146
145
//delete all code here.
147
146
148
- if ( global . mongoDisconnected ) {
147
+ if ( global . mongoDisconnected ) {
149
148
return res . status ( 500 ) . send ( 'Storage / Cache Backend are temporarily down.' ) ;
150
149
}
151
150
152
- global . appService . upsertTable ( appId , tableName , body . data . columns ) . then ( function ( table ) {
151
+ global . appService . upsertTable ( appId , tableName , body . data . columns ) . then ( function ( table ) {
153
152
return res . status ( 200 ) . send ( table ) ;
153
+
154
154
} , function ( err ) {
155
155
return res . status ( 500 ) . send ( err ) ;
156
156
} ) ;
157
- } else {
158
- return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
157
+ } else {
158
+ return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
159
159
}
160
- } , function ( error ) {
160
+ } , function ( error ) {
161
161
return res . status ( 500 ) . send ( 'Cannot retrieve security keys.' ) ;
162
162
} ) ;
163
-
164
163
global . apiTracker . log ( appId , "App / Table / Create" , req . url , sdk ) ;
165
164
166
165
}
167
166
168
167
} ) ;
169
168
170
169
//get a table.
171
- global . app . post ( '/app/:appId/:tableName' , _getTable ) ;
172
- global . app . get ( '/app/:appId/:tableName' , _getTable ) ;
170
+ global . app . post ( '/app/:appId/:tableName' , _getTable ) ;
171
+ global . app . get ( '/app/:appId/:tableName' , _getTable ) ;
173
172
174
- function _getTable ( req , res ) {
173
+ function _getTable ( req , res ) {
175
174
console . log ( '++++++++ GET TABLE API +++++++++' ) ;
176
175
177
176
var appId = req . params . appId ;
@@ -180,98 +179,97 @@ module.exports = function() {
180
179
var sdk = req . body . sdk || "REST" ;
181
180
var appKey = req . body . key || req . params . key ;
182
181
183
- global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
184
- if ( isMasterKey ) {
182
+ global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
183
+ if ( isMasterKey ) {
185
184
//delete all code here.
186
- if ( tableName === "_getAll" ) {
187
- global . appService . getAllTables ( appId ) . then ( function ( tables ) {
185
+ if ( tableName === "_getAll" ) {
186
+ global . appService . getAllTables ( appId ) . then ( function ( tables ) {
188
187
return res . status ( 200 ) . send ( tables ) ;
189
- } , function ( err ) {
188
+ } , function ( err ) {
190
189
return res . status ( 500 ) . send ( 'Error' ) ;
191
190
} ) ;
192
- } else {
193
- global . appService . getTable ( appId , tableName ) . then ( function ( table ) {
191
+ } else {
192
+ global . appService . getTable ( appId , tableName ) . then ( function ( table ) {
194
193
return res . status ( 200 ) . send ( table ) ;
195
- } , function ( err ) {
194
+ } , function ( err ) {
196
195
return res . status ( 500 ) . send ( 'Error' ) ;
197
196
} ) ;
198
197
}
199
198
200
- } else {
201
- return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
199
+ } else {
200
+ return res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
202
201
}
203
- } , function ( error ) {
202
+ } , function ( error ) {
204
203
return res . status ( 500 ) . send ( 'Cannot retrieve security keys.' ) ;
205
204
} ) ;
206
205
207
- global . apiTracker . log ( appId , "App / Table / Get" , req . url , sdk ) ;
206
+ global . apiTracker . log ( appId , "App / Table / Get" , req . url , sdk ) ;
208
207
}
209
208
210
-
211
209
//Export Database for :appID
212
- global . app . post ( '/backup/:appId/exportdb' , function ( req , res ) {
210
+ global . app . post ( '/backup/:appId/exportdb' , function ( req , res ) {
213
211
console . log ( "++++ Export Database ++++++" ) ;
214
- try {
212
+ try {
215
213
var appKey = req . body . key ;
216
214
var appId = req . params . appId ;
217
215
218
- global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
216
+ global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
219
217
220
218
if ( isMasterKey ) {
221
- global . appService . exportDatabase ( appId ) . then ( function ( data ) {
219
+ global . appService . exportDatabase ( appId ) . then ( function ( data ) {
222
220
res . writeHead ( 200 , {
223
- "Content-Type" : "application/octet-stream" ,
224
- "Content-Disposition" : "attachment; filename=dump" + ( new Date ( ) ) + ".json"
221
+ "Content-Type" : "application/octet-stream" ,
222
+ "Content-Disposition" : "attachment; filename=dump" + ( new Date ( ) ) + ".json"
225
223
} ) ;
226
224
res . end ( JSON . stringify ( data ) )
227
- } , function ( err ) {
225
+ } , function ( err ) {
228
226
console . log ( "Error : Exporting Database." ) ;
229
227
console . log ( err ) ;
230
228
res . status ( 500 ) . send ( "Error" ) ;
231
229
} )
232
230
} else {
233
- res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
231
+ res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
234
232
}
235
- } , function ( error ) {
233
+ } , function ( error ) {
236
234
return res . status ( 500 ) . send ( 'Cannot retrieve security keys.' ) ;
237
235
} ) ;
238
- } catch ( e ) {
236
+ } catch ( e ) {
239
237
console . log ( e ) ;
240
238
}
241
239
} ) ;
242
240
243
241
//Import Database for :appID
244
- global . app . post ( '/backup/:appId/importdb' , function ( req , res ) {
242
+ global . app . post ( '/backup/:appId/importdb' , function ( req , res ) {
245
243
console . log ( "++++ Import Database ++++++" ) ;
246
244
try {
247
245
var appKey = req . body . key
248
246
var appId = req . params . appId ;
249
- global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
247
+ global . appService . isMasterKey ( appId , appKey ) . then ( function ( isMasterKey ) {
250
248
if ( isMasterKey ) {
251
249
var file ;
252
- if ( req . files && req . files . file ) {
253
- file = req . files . file . data
250
+ if ( req . files && req . files . file ) {
251
+ file = req . files . file . data
254
252
}
255
- if ( file ) {
256
- global . appService . importDatabase ( appId , file ) . then ( function ( data ) {
257
- if ( data ) {
258
- res . status ( 200 ) . json ( { Success :true } )
253
+ if ( file ) {
254
+ global . appService . importDatabase ( appId , file ) . then ( function ( data ) {
255
+ if ( data ) {
256
+ res . status ( 200 ) . json ( { Success : true } )
259
257
} else {
260
- res . status ( 500 ) . json ( { success :false } )
258
+ res . status ( 500 ) . json ( { success : false } )
261
259
}
262
- } , function ( err ) {
260
+ } , function ( err ) {
263
261
console . log ( "Error : Exporting Database." ) ;
264
262
console . log ( err ) ;
265
263
res . status ( 500 ) . send ( "Error" ) ;
266
264
} )
267
265
}
268
266
} else {
269
- res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
267
+ res . status ( 401 ) . send ( { status : 'Unauthorized' } ) ;
270
268
}
271
- } , function ( error ) {
269
+ } , function ( error ) {
272
270
return res . status ( 500 ) . send ( 'Cannot retrieve security keys.' ) ;
273
271
} ) ;
274
- } catch ( e ) {
272
+ } catch ( e ) {
275
273
console . log ( e ) ;
276
274
}
277
275
} ) ;
0 commit comments