-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
222 lines (158 loc) · 5.37 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
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
// var mongoOp = require("./models/mongo");
var mongoose = require("mongoose");
var router = express.Router();
var AutoComplete = require("./models/autocomplete")
// var site_vhosts = []
// var vhost
var connection = mongoose.connect('mongodb://localhost:27017/roastmeister001')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({"extended" : true}));
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
// 추후 trace를 하게 위해서 err.stack 을 사용하여 logging하시기 바랍니다.
// Published story에서 beautifule logging winston 참조
});
router.get("/",function(req,res){
res.json({"error" : false,"message" : "Wooooooooooooooooooooooooooo"});
});
// check require params
var checkRequireParams = function(target, requirments) {
if (requirments == null) {return null}
var returnMessage = ""
for (var i=0; i<requirments.length; i++) {
if (target == null || target[requirments[i]] == null) {
if (i==0) {returnMessage += requirments[i]
} else {returnMessage += ", " + requirments[i]}
}
}
if (returnMessage.length == 0) {
return null
} else {
return returnMessage
}
}
//
// Authorization part
var authorizationKey = null
var mongoSchema = mongoose.Schema;
var authorizationSchema = new mongoSchema({
key : {type: String, require: true, unique: true},
created_at: Date
})
var AuthorizationModel = connection.model("Authorization", authorizationSchema);
if (process.argv && process.argv.length>2) {
AuthorizationModel.count({}, function(err, count) {
authorizationKey = process.argv[2]
if (count>0) {
// error
console.log("ERROR : Here was aleady set authenticaiton key.")
process.exit(1);
} else {
// set key
var keydocument = new AuthorizationModel({
key: authorizationKey,
created_at: new Date()
})
keydocument.save(function(err) {
console.log("Success regist authorization key.")
})
}
})
} else {
AuthorizationModel.count({}, function(err, count) {
if (count>0) {
// ok
AuthorizationModel.find({}, function(err, data) {
authorizationKey = data[0].key
})
} else {
// error
console.log("ERROR : You must authenticaiton key at third argv.")
console.log("ex) $ node server.js authenticaiton_key")
process.exit(1);
}
})
}
//
//
app.use('/roastmeister001', function (req, res, next) {
// console.log('Time: %d', Date.now());
if (req.header('Authorization') && req.header('Authorization') == authorizationKey) {
next();
} else {
response = {"error" : true, "message" : "Invalid Authorization Key."};
res.json(response);
}
});
var autocompleteTitles = ["nation", "farm", "beankind", "drykind", "machine", "faul"]
var autocompltetDBs = {}
for (var i=0; i<autocompleteTitles.length; i++) {
var title = autocompleteTitles[i]
autocompltetDBs[title] = new AutoComplete(mongoose, connection, title)
}
// POST autocomplete
router.route("/roastmeister001/autocomplete")
.post(function(req,res){
var checkedRequireParams = checkRequireParams(req.body, ["deviceid"])
var deviceid = req.body.deviceid
if (deviceid != null) {
upsertAutocompleteWithCallback(req.body, 0, {upsert: 0, error: 0}, function(err, response) {
var response = {"error" : false, "message": "success", "data" : response};
res.json(response)
})
} else {
var response = {"error" : true, "err_code" : 501, "message" : "Invalid params. Require [" + checkedRequireParams + "] params."};
res.json(response)
}
});
function upsertAutocompleteWithCallback (params, i, response, callback) {
if (i==autocompleteTitles.length) {
callback(null, response)
return
}
var title = autocompleteTitles[i]
if (params[title] == null) {
upsertAutocompleteWithCallback(params, i+1, response, callback)
return
}
// 재귀 호출
autocompltetDBs[autocompleteTitles[i]].upsert({name: params[autocompleteTitles[i]], deviceid: params.deviceid}, function(err, data) {
if (err) {
response.error += 1
} else {
response.upsert += 1
}
upsertAutocompleteWithCallback(params, i+1, response, callback)
})
}
// GET autocompletes
router.route("/roastmeister001/autocomplete/:title")
.get(function(req,res){
for (var i=0; i<autocompleteTitles.length; i++) {
if (autocompleteTitles[i] == req.params.title) {
var title = autocompleteTitles[i]
var param = {}
if (req.params.q) {param.name = req.params.q}
autocompltetDBs[title].search(req.query, function(err, data) {
var response
if(err) {
response = {"error" : true, "message" : "Error fetching data"};
} else {
response = {"error" : false, "message": "success", "data" : data};
}
res.json(response);
})
return
}
}
var response = {"error" : true, "message" : "you cannnot use '" + req.params.title + "'db."}
res.json(response)
})
app.use('/',router);
// site_vhosts.push(express.vhost('rm.canapio.com', app));
// vhost = express().apply(this, site_vhosts);
app.listen(3000);
console.log("Listening to PORT 3000");