-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
74 lines (61 loc) · 2.15 KB
/
index.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
/* eslint no-console: 0 */
const http = require('http');
const express = require('express');
const compression = require('compression');
const moment = require('moment');
const config = require('./config');
const moedoo = require('./lib/moedoo')(process.env.DATABASE_URL || {
DB_USER: config.DB_USER,
DB_PASSWORD: config.DB_PASSWORD,
DB_HOST: config.DB_HOST,
DB_PORT: config.DB_PORT,
DB_NAME: config.DB_NAME,
});
const api = require('./routes/api');
const poster = require('./routes/poster');
moedoo.query(`
CREATE TABLE IF NOT EXISTS rtdb(
title character varying(128) NOT NULL,
detail jsonb,
created_at Date DEFAULT now(),
CONSTRAINT movie_pk PRIMARY KEY (title)
);
CREATE TABLE IF NOT EXISTS poster(
url character varying(1024) NOT NULL,
poster text,
created_at Date DEFAULT now(),
CONSTRAINT poster_pk PRIMARY KEY (url)
);
-- the following two can be one
DELETE FROM rtdb WHERE created_at < '${moment().subtract(5, 'days').format('YYYY-MM-DD')}';
DELETE FROM poster WHERE created_at < '${moment().subtract(30, 'days').format('YYYY-MM-DD')}';`).then(() => {
const app = express();
app.set('port', process.env.PORT || config.APP_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');
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Max-Age', '86400');
if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}
next();
});
app.use(compression());
// API...
app.get('/api', api(moedoo));
// Poster...
app.get('/poster', poster(moedoo));
// fall-back handler...
app.all('*', (request, response) => {
response.status(400).end();
});
const server = http.createServer(app); // creating server which express will piggy back on
server.listen(app.get('port'), config.APP_HOST, () => {
console.log(`Server running on ${config.APP_HOST}:${process.env.PORT || config.APP_PORT}...`);
});
}, (err) => {
console.error(err);
});