-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
107 lines (95 loc) · 2.44 KB
/
routes.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
const express = require('express');
const bodyParser = require('body-parser').json();
const auth = require('./apis/auth');
const patch = require('./apis/apply-patch');
const thumbnail = require('./apis/generate-thumbnail');
const errorlog = require('./logger').errorlog;
const successlog = require('./logger').successlog;
const app = express.Router();
/**
* URL: http://localhost:3000/api/v1/login
* Content-Type: application/x-www-form-urlencoded
* Params:
* username: string
* password: password
*/
app.post('/login',(req,res)=>{
try {
let result = auth.login(req.body);
res.send(result);
}
catch(err) {
errorlog.error(`Error <Method(login)>: ${err}`);
}
});
/**
* URL: http://localhost:3000/api/v1/validate
* Content-Type: application/x-www-form-urlencoded
* Params:
* token: token
*/
app.post('/validate',(req,res)=>{
try {
let bool = auth.validate(req.body.token);
res.send(bool);
}
catch(err) {
errorlog.error(`Error <Method(validate)>: ${err}`);
}
});
/**
* URL: http://localhost:3000/api/v1/applyPatch
* Content-Type: JSON (application/json)
* Params:
* json: json object
* patch: json patch object
* token: token
*/
app.post('/applyPatch', bodyParser, (req,res)=>{
if (auth.validate(req.body.token)) {
jsondata = patch.apply(req.body);
res.send(jsondata);
}
else {
errorlog.error(`Error <Method(applyPatch)>: Invalid Token`);
res.send(
JSON.stringify({
success: 'false',
message: 'Invalid Token',
})
);
}
});
/**
* URL: http://localhost:3000/api/v1/getThumbnail
* Content-Type: application/x-www-form-urlencoded
* Params:
* image: string (Image URL)
* token: token
*/
app.post('/getThumbnail',(req,res)=>{
if (auth.validate(req.body.token)) {
thumbnail.generate(req.body.image).then((thumbnail)=>{
res.send(thumbnail);
}).catch((err)=>{
errorlog.error(`Error <Method(getThumbnail)>: ${err}`);
res.send(
JSON.stringify({
success: 'false',
message: 'Error',
error: err
})
);
});
}
else {
errorlog.error(`Error <Method(getThumbnail)>: Invalid Token`);
res.send(
JSON.stringify({
success: 'false',
message: 'Invalid Token',
})
);
}
});
module.exports = app;