-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.ts
157 lines (131 loc) · 4.92 KB
/
Server.ts
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
/**
* This is the REST entry point for the project.
* Restify is configured here.
*/
import restify = require('restify');
import Log from "../Util";
import {IInsightFacade, InsightResponse, QueryRequest} from "../controller/IInsightFacade";
import InsightFacade from "../controller/InsightFacade";
/**
* This configures the REST endpoints for the server.
*/
export default class Server {
private port: number;
private rest: restify.Server;
constructor(port: number) {
Log.info("Server::<init>( " + port + " )");
this.port = port;
}
/**
* Stops the server. Again returns a promise so we know when the connections have
* actually been fully closed and the port has been released.
*
* @returns {Promise<boolean>}
*/
public stop(): Promise<boolean> {
Log.info('Server::close()');
let that = this;
return new Promise(function (fulfill) {
that.rest.close(function () {
fulfill(true);
});
});
}
/**
* Starts the server. Returns a promise with a boolean value. Promises are used
* here because starting the server takes some time and we want to know when it
* is done (and if it worked).
*
* @returns {Promise<boolean>}
*/
public start(): Promise<boolean> {
let that = this;
return new Promise(function (fulfill, reject) {
try {
Log.info('Server::start() - start');
that.rest = restify.createServer({
name: 'insightUBC'
});
that.rest.use(restify.bodyParser({mapParams: true, mapFiles: true}));
that.rest.get("/public/.*", restify.serveStatic({
directory: __dirname
}));
// serve default page
that.rest.get('/:name', restify.serveStatic({
directory: __dirname + '/view',
default: 'index.html'
}));
// get query interface for UI
that.rest.get('/', Server.getQueryInterface);
// put dataset
// put(path, send)
that.rest.put('/dataset/:id', Server.putDataset);
// delete dataset
// del(path, rm)
that.rest.del('/dataset/:id', Server.deleteDataset);
// post query
// put(path, send)
that.rest.post('/query', Server.postQuery);
that.rest.listen(that.port, function () {
Log.info('Server::start() - restify listening: ' + that.rest.url);
fulfill(true);
});
that.rest.on('error', function (err: string) {
reject(err);
});
} catch (err) {
reject(err);
}
});
}
public static getQueryInterface(req: restify.Request, res: restify.Response, next: restify.Next) {
res.send(200);
return next();
}
public static putDataset(req: restify.Request, res: restify.Response, next: restify.Next) {
try {
let insightfacade = new InsightFacade();
let dataStr = new Buffer(req.params.body).toString('base64');
insightfacade.addDataset(req.params.id, dataStr)
.then(function (insightresponse: InsightResponse){
res.json(insightresponse.code, insightresponse.body);
})
.catch(function (err: any){
res.json(err.code, err.body);
});
} catch (err) {
res.json(400, {error: err.message});
}
return next();
}
public static deleteDataset(req: restify.Request, res: restify.Response, next: restify.Next) {
try {
let insightfacade = new InsightFacade();
insightfacade.removeDataset(req.params.id)
.then(function (insightresponse: InsightResponse){
res.json(insightresponse.code, insightresponse.body);
})
.catch(function (err: any){
res.json(err.code, err.body);
});
} catch (err) {
res.json(404, {error: err.message});
}
return next();
}
public static postQuery(req: restify.Request, res: restify.Response, next: restify.Next) {
try {
let insightfacade = new InsightFacade();
insightfacade.performQuery(req.body)
.then(function (insightresponse: InsightResponse){
res.json(insightresponse.code, insightresponse.body);
})
.catch(function (err: any){
res.json(err.code, err.body);
});
} catch (err) {
res.json(400, {error: err.message});
}
return next();
}
}