-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (55 loc) · 1.7 KB
/
app.js
File metadata and controls
71 lines (55 loc) · 1.7 KB
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
const express = require('express');
const app = express();
require('./lib/extensions/response');
app.use(express.json({
verify: (_, res, buf, __) => {
try {
JSON.parse(buf);
} catch {
res.badRequest('Invalid JSON in body.');
}
}
}));
const Controller = require('./lib/controllers/controller');
const VideoGamesService = require('./lib/services/video-games');
const InMemory = require('./lib/storage/in-memory');
const storage = new InMemory();
const videoGamesService = new VideoGamesService(storage);
const controller = new Controller(videoGamesService);
app.get('/ping', (_, res) => {
res.send('pong');
});
app.get('/video_games', (_, res) => {
controller.getAll(res);
});
app.get('/video_games/:id', (req, res) => {
controller.get(req, res);
});
app.post('/video_games', (req, res) => {
controller.add(req, res);
});
app.put('/video_games/:id', (req, res) => {
controller.put(req, res);
});
app.delete('/video_games/:id', (req, res) => {
controller.delete(req, res);
});
let env = process.env.API_ENV;
env = (env ? env.toUpperCase() : 'DEV');
if (env === 'DEV') {
const TestingController = require('./lib/controllers/testing-controller');
const testingController = new TestingController(storage);
app.get('/api_tests/setup', (_, res) => {
testingController.setup(res);
});
}
const Config = require('./lib/utils/config');
Config.fromEnvironment(env)
.then(config => {
const hostname = config.hostname;
const port = config.port;
app.listen(port, hostname, () => {
console.log(`API Farm listening on http://${hostname}:${port}`);
});
})
.catch(console.error);