generated from DP6/template-default-initiative-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (53 loc) · 1.95 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
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors');
const { insertRowsAsStream, loadData } = require('./bigquery');
const { hubMessage } = require('./message');
const app = express();
const port = 8000;
const env = process.env;
const DATASET_ID = env.DATASET_ID || 'raft_suite',
TABLE_ID = env.TABLE_ID || 'hub',
INSERT_TYPE = env.INSERT_TYPE || 'batch';
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Acces-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
app
.route('/')
.get(function (req, res) {
let message = hubMessage();
message = JSON.stringify(message, null, 2);
const response = `This is a example message:\n${message}`;
res.status(200).send(response);
console.log(message);
})
.post(function (req, res) {
try {
if (!req.body) throw new Error('No body found on the request');
console.log(req.body);
let body = JSON.parse(req.body);
let required_keys = ['module', 'spec', 'deploy'];
let keyNotInObject = (key) => !body.hasOwnProperty(key);
required_keys = required_keys.filter(keyNotInObject);
if (required_keys.length > 0) throw new Error(`Missing required keys: ${required_keys.join(', ')}`);
let data;
if (body instanceof Array) data = body.map((i) => hubMessage(i));
else data = hubMessage(body);
res.status(200).send(data.jobId);
console.log(JSON.stringify(data));
if (INSERT_TYPE == 'stream') insertRowsAsStream(data, DATASET_ID, TABLE_ID);
else loadData(data, DATASET_ID, TABLE_ID);
} catch (error) {
res.status(400).send(error.toString());
console.error(error);
}
});
app.listen(port, () => {
console.log(`App listening on port ${port}!`);
});
exports.app = functions.https.onRequest(app);