-
Notifications
You must be signed in to change notification settings - Fork 10
/
express.js
113 lines (97 loc) · 2.88 KB
/
express.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
require('dotenv').config()
const express = require('express');
const app = express();
const port = process.env.PORT ? parseInt(process.env.PORT) : 80;
const API_VERSION = 1;
const driveAPI = new (require('./driveAPI'))();
app.listen(port, () => console.log(`DriveConnector backend listening on port ${port}`));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
const router = express.Router();
router.get('/', (req, res) => {
res.sendStatus(200);
});
router.route('/getSheet')
.get((req, res) => {
console.log('GET /getSheet', req.query.id, req.query.range);
driveAPI.getSheet(req.query.id, req.query.range)
.then((data)=>{
res.json(data);
})
.catch(error => {
console.error(error);
res.sendStatus(500);
});
});
router.route('/getDoc')
.get((req, res) => {
console.log('GET /getDoc', req.query.id);
driveAPI.getDoc(req.query.id)
.then(data => res.json(data))
.catch(error => {
console.error(error);
res.sendStatus(500);
});
});
router.route('/listFiles')
.get((req, res) => {
console.log('GET /listFiles', req.query.folder);
driveAPI.listFiles(req.query.folder)
.then(data => res.json(data))
.catch(error => {
console.error(error);
res.sendStatus(500);
});
});
router.route('/getAll')
.get((req, res) => {
console.log('GET /getAll', req.query.driveId);
driveAPI.getAll(req.query.driveId)
.then(data => res.json(data))
.catch(error => {
console.error(error);
res.sendStatus(500);
});
});
router.route('/getImage')
.get((req, res) => {
// console.log('GET /getImage', req.query.id);
try {
res.sendFile('cache/' + req.query.id, { root: './'});
} catch (e) {
console.error(e);
res.sendStatus(404);
}
});
app.use(`/api/v${API_VERSION}`, router);
if (process.env.SERVE_SAMPLES) {
app.use('/samples', express.static('samples'));
app.use('/', (req, res, next) => res.redirect('/samples'));
}
// https://stackoverflow.com/a/21947851
function onExit(callback) {
// attach user callback to the process event emitter
// if no callback, it will still exit gracefully on Ctrl-C
process.on('cleanup', callback);
// do app specific cleaning before exiting
process.on('exit', function () {
// @ts-ignore
process.emit('cleanup');
});
// catch ctrl+c event and exit normally
process.on('SIGINT', function () {
console.log('Ctrl-C...');
process.exit(2);
});
//catch uncaught exceptions, trace, then exit normally
process.on('uncaughtException', function(e) {
console.log('Uncaught Exception...');
console.log(e.stack);
process.exit(99);
});
};
const exitHandler = () => driveAPI.writeCache();
onExit(exitHandler);