Skip to content

Commit 9f28fcf

Browse files
committed
Backport netlify function deployment from master.
1 parent 58cf3d8 commit 9f28fcf

File tree

20 files changed

+98
-57
lines changed

20 files changed

+98
-57
lines changed

frontend/mock-api/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const version = require("../../package.json").version;
2-
const config = require("./config.json");
2+
const config = require("./functions/api/config.json");
33

44
config.version = version;
55

frontend/mock-api/autocomplete/countries.json renamed to frontend/mock-api/functions/api/autocomplete/countries.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[
1+
module.exports = [
22
"Afghanistan",
33
"Albania",
44
"Algeria",
@@ -209,4 +209,4 @@
209209
"Yemen",
210210
"Zambia",
211211
"Zimbabwe"
212-
]
212+
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as serverless from "serverless-http";
2+
import * as createApi from "../../index";
3+
import * as express from "express";
4+
const app = createApi();
5+
const wrapper = express();
6+
7+
module.exports.handler = serverless(
8+
wrapper.use("/.netlify/functions/api", app)
9+
);

frontend/mock-api/index.js

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
const path = require("path");
22
const version = require("../package.json").version;
3-
const EXPORT_FORM_CONFIG = require("./forms/export-form.json");
3+
const EXPORT_FORM_CONFIG = require("./functions/api/forms/export-form.json");
44
const mockAuthMiddleware = require("./mockAuthMiddleware");
5-
5+
const express = require("express");
6+
const bodyParser = require("body-parser");
7+
const cors = require("cors");
68
// Taken from:
79
// http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
810
function shuffleArray(array) {
9-
for (var i = array.length - 1; i > 0; i--) {
10-
var j = Math.floor(Math.random() * (i + 1));
11-
var temp = array[i];
11+
for (let i = array.length - 1; i > 0; i--) {
12+
const j = Math.floor(Math.random() * (i + 1));
13+
const temp = array[i];
1214
array[i] = array[j];
1315
array[j] = temp;
1416
}
1517
return array;
1618
}
1719

1820
const ERROR = JSON.stringify({
19-
message: "Could not process the request",
21+
message: "Could not process the request"
2022
});
2123

2224
const LONG_DELAY = 500;
2325
const SHORT_DELAY = 300;
2426
const NO_DELAY = 10;
2527

2628
// Simulate API
27-
module.exports = function (app, port) {
29+
function mountApi(app) {
2830
/*
2931
QUERIES
3032
*/
@@ -78,7 +80,7 @@ module.exports = function (app, port) {
7880
id: 1,
7981
status: "DONE",
8082
numberOfResults: 5,
81-
resultUrl: `/api/results/results.csv`,
83+
resultUrl: `/api/results/results.csv`
8284
})
8385
);
8486
}, LONG_DELAY);
@@ -97,8 +99,8 @@ module.exports = function (app, port) {
9799
{ id: "empty-set", label: "Empty Dataset" },
98100
{
99101
id: "another-empty-set",
100-
label: "Another empty dataset with a long name",
101-
},
102+
label: "Another empty dataset with a long name"
103+
}
102104
])
103105
);
104106
});
@@ -110,7 +112,9 @@ module.exports = function (app, port) {
110112
req,
111113
res
112114
) {
113-
res.sendFile(path.join(__dirname, `./results/${req.params.filename}`));
115+
res.sendFile(
116+
path.join(__dirname, "functions/api", `./results/${req.params.filename}`)
117+
);
114118
});
115119

116120
/*
@@ -120,14 +124,20 @@ module.exports = function (app, port) {
120124
req,
121125
res
122126
) {
123-
res.sendFile(path.join(__dirname, "./concepts.json"));
127+
res.sendFile(path.join(__dirname, "functions/api", "./concepts.json"));
124128
});
125129

126130
app.get(
127131
"/api/datasets/:datasetId/concepts/:id",
128132
mockAuthMiddleware,
129133
function response(req, res) {
130-
res.sendFile(path.join(__dirname, `./concepts/${req.params.id}.json`));
134+
res.sendFile(
135+
path.join(
136+
__dirname,
137+
"functions/api",
138+
`./concepts/${req.params.id}.json`
139+
)
140+
);
131141
}
132142
);
133143

@@ -149,10 +159,10 @@ module.exports = function (app, port) {
149159
"group 1",
150160
"important",
151161
"jk",
152-
"interesting",
162+
"interesting"
153163
];
154164

155-
for (var i = 25600; i < 35600; i++) {
165+
for (let i = 25600; i < 35600; i++) {
156166
const notExecuted = Math.random() < 0.1;
157167

158168
ids.push({
@@ -168,7 +178,7 @@ module.exports = function (app, port) {
168178
own: Math.random() < 0.1,
169179
shared: Math.random() < 0.8,
170180
resultUrl: notExecuted ? null : `/api/results/results.csv`,
171-
ownerName: "System",
181+
ownerName: "System"
172182
});
173183
}
174184

@@ -182,7 +192,9 @@ module.exports = function (app, port) {
182192
mockAuthMiddleware,
183193
function response(req, res) {
184194
setTimeout(() => {
185-
res.sendFile(path.join(__dirname, "./stored-queries/25.json"));
195+
res.sendFile(
196+
path.join(__dirname, "functions/api", "./stored-queries/25.json")
197+
);
186198
}, LONG_DELAY);
187199
}
188200
);
@@ -229,7 +241,7 @@ module.exports = function (app, port) {
229241
res.send(
230242
JSON.stringify({
231243
successful: 1 + Math.floor(Math.random() * 200),
232-
unsuccessful: 586,
244+
unsuccessful: 586
233245
})
234246
);
235247
}, LONG_DELAY);
@@ -247,7 +259,7 @@ module.exports = function (app, port) {
247259
const countriesRequested = req.params.filterId === "production_country";
248260

249261
const storedValues = countriesRequested
250-
? require("./autocomplete/countries")
262+
? require("./functions/api/autocomplete/countries")
251263
: [
252264
"1008508208",
253265
"1015841172",
@@ -258,16 +270,16 @@ module.exports = function (app, port) {
258270
"1000326535",
259271
"1014150881",
260272
"1017126347",
261-
"1008445564",
273+
"1008445564"
262274
];
263275

264276
const suggestions = storedValues
265277
.map((v, id) => ({
266278
label: v,
267279
value: id,
268-
templateValues: { company: "Columbia Pictures Corporation" },
280+
templateValues: { company: "Columbia Pictures Corporation" }
269281
}))
270-
.filter((v) => v.label.toLowerCase().startsWith(text));
282+
.filter(v => v.label.toLowerCase().startsWith(text));
271283

272284
res.send(JSON.stringify(suggestions));
273285
}, LONG_DELAY);
@@ -285,7 +297,7 @@ module.exports = function (app, port) {
285297

286298
res.send({
287299
unknownCodes: concepts.slice(5),
288-
resolvedConcepts: concepts.slice(1),
300+
resolvedConcepts: concepts.slice(1)
289301
});
290302
}, LONG_DELAY);
291303
}
@@ -299,7 +311,7 @@ module.exports = function (app, port) {
299311

300312
res.send({
301313
version: version,
302-
isDevelopment: process.env.NODE_ENV !== "production",
314+
isDevelopment: process.env.NODE_ENV !== "production"
303315
});
304316
});
305317

@@ -317,17 +329,17 @@ module.exports = function (app, port) {
317329

318330
if (req.params.filterId !== "production_country") return null;
319331

320-
const countries = require("./autocomplete/countries");
321-
const unknownCodes = values.filter((val) => !countries.includes(val));
322-
const resolvedValues = values.filter((val) => countries.includes(val));
332+
const countries = require("./functions/api/autocomplete/countries");
333+
const unknownCodes = values.filter(val => !countries.includes(val));
334+
const resolvedValues = values.filter(val => countries.includes(val));
323335

324336
res.send({
325337
unknownCodes: unknownCodes,
326338
resolvedFilter: {
327339
tableId: req.params.tableId,
328340
filterId: req.params.filterId,
329-
value: resolvedValues.map((val) => ({ label: val, value: val })),
330-
},
341+
value: resolvedValues.map(val => ({ label: val, value: val }))
342+
}
331343
});
332344
}, LONG_DELAY);
333345
}
@@ -336,7 +348,7 @@ module.exports = function (app, port) {
336348
app.get("/api/config/frontend", mockAuthMiddleware, (req, res) => {
337349
res.setHeader("Content-Type", "application/json");
338350

339-
const config = require("./config.json");
351+
const config = require("./functions/api/config.json");
340352

341353
config.version = version;
342354

@@ -351,13 +363,13 @@ module.exports = function (app, port) {
351363

352364
if (user === "test" && password === "test") {
353365
res.send({
354-
access_token: "VALID",
366+
access_token: "VALID"
355367
});
356368
} else {
357369
res.status(422);
358370
res.send(
359371
JSON.stringify({
360-
message: "Login failed",
372+
message: "Login failed"
361373
})
362374
);
363375
}
@@ -374,10 +386,10 @@ module.exports = function (app, port) {
374386
domains: ["datasets"],
375387
abilities: ["read", "download", "preserve_id"],
376388
targets: ["imdb"],
377-
creationTime: "2020-01-23T09:52:31.3318485",
378-
},
389+
creationTime: "2020-01-23T09:52:31.3318485"
390+
}
379391
],
380-
groups: [],
392+
groups: []
381393
});
382394
});
383395

@@ -390,7 +402,7 @@ module.exports = function (app, port) {
390402
res.status(201);
391403
res.send(
392404
JSON.stringify({
393-
id: 56000 + Math.floor(Math.random() * 200),
405+
id: 56000 + Math.floor(Math.random() * 200)
394406
})
395407
);
396408
}, LONG_DELAY);
@@ -409,12 +421,12 @@ module.exports = function (app, port) {
409421
if (dice < 0.5) {
410422
return {
411423
formType: "EXPORT_FORM",
412-
values: {},
424+
values: {}
413425
};
414426
} else {
415427
return {
416428
formType: "Other form",
417-
values: {},
429+
values: {}
418430
};
419431
}
420432
}
@@ -428,10 +440,10 @@ module.exports = function (app, port) {
428440
"group 1",
429441
"important",
430442
"jk",
431-
"interesting",
443+
"interesting"
432444
];
433445

434-
for (var i = 55600; i < 85600; i++) {
446+
for (let i = 55600; i < 58600; i++) {
435447
configs.push({
436448
id: i,
437449
label: "Saved Config",
@@ -442,7 +454,7 @@ module.exports = function (app, port) {
442454
own: Math.random() < 0.1,
443455
shared: Math.random() < 0.8,
444456
ownerName: "System",
445-
...getFormConfigAttributes(),
457+
...getFormConfigAttributes()
446458
});
447459
}
448460

@@ -456,7 +468,9 @@ module.exports = function (app, port) {
456468
mockAuthMiddleware,
457469
function response(req, res) {
458470
setTimeout(() => {
459-
res.sendFile(path.join(__dirname, "./form-configs/testconf.json"));
471+
res.sendFile(
472+
path.join(__dirname, "functions/api", "./form-configs/testconf.json")
473+
);
460474
}, LONG_DELAY);
461475
}
462476
);
@@ -481,4 +495,16 @@ module.exports = function (app, port) {
481495
}, LONG_DELAY);
482496
}
483497
);
498+
}
499+
500+
const createApi = () => {
501+
const app = express();
502+
app.use(cors());
503+
// body parser must be set up before routes are attached
504+
app.use(bodyParser.json());
505+
506+
mountApi(app);
507+
return app;
484508
};
509+
510+
module.exports = createApi;

frontend/mock-api/server.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,12 @@
22
// EXPRESS SETUP
33
// -----------
44
const path = require("path");
5-
const express = require("express");
6-
const bodyParser = require("body-parser");
7-
const cors = require("cors");
8-
const mountApi = require(".");
5+
const createApi = require(".");
96

107
const isDeveloping = process.env.NODE_ENV !== "production";
118
const port = process.env.PORT || 8001;
12-
const app = express();
13-
const lang = process.env.APP_LANG || "en";
149

15-
app.use(cors());
16-
// body parser must be set up before routes are attached
17-
app.use(bodyParser.json());
18-
19-
mountApi(app, port);
10+
const app = createApi()
2011

2112
if (!isDeveloping) {
2213
app.use("/app/static", express.static(__dirname + "/build"));

frontend/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
},
1111
"scripts": {
1212
"start": "react-app-rewired start",
13-
"build": "react-app-rewired build",
13+
"build-frontend": "react-app-rewired build",
14+
"build-mock-backend": "tsc mock-api/functions/*/**.ts",
15+
"build": "yarn build-frontend && yarn build-mock-backend",
1416
"test": "react-app-rewired test",
1517
"eject": "react-scripts eject",
1618
"heroku-postbuild": "yarn run build",
@@ -94,6 +96,7 @@
9496
"react-scripts": "^3.4.1",
9597
"redux-logger": "^3.0.6",
9698
"redux-mock-store": "^1.5.1",
99+
"serverless-http": "^2.5.0",
97100
"timekeeper": "^2.0.0",
98101
"ts-loader": "^6.2.2",
99102
"ts-node": "^8.8.1",

0 commit comments

Comments
 (0)