-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
74 lines (61 loc) · 2.1 KB
/
index.ts
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
import express from 'express'
import Rand from 'rand-seed';
import { fileHandler } from "./src/fileHandler";
import { User } from "./src/types";
import { randomElementSeed } from "./src/util";
import { getCoordinates } from "./src/coordinates";
const app = express();
const PORT = 3000;
function setHeaders(res : express.Response) {
res.header("Content-Type",'application/json')
res.header("Access-Control-Allow-Origin", "*")
res.header("Cache-Control", "private, max-age=0")
}
app.get("/", (req, res) => {
res.sendStatus(200)
})
app.get('/me', async (req, res) => {
const json = await fileHandler("./data/users.json")
setHeaders(res)
const users: User[] = JSON.parse(json)
const seed = new Rand(req.headers['seed'] as string || 'drWho')
res.send(randomElementSeed(seed, users))
})
app.get('/users', async (req, res) => {
const users = await fileHandler("./data/users.json")
setHeaders(res)
res.send(users)
})
app.get('/activities', async (req, res) => {
const activities = await fileHandler("./data/activities.json")
setHeaders(res)
res.send(activities)
})
app.get('/fromFlights', async (req, res) => {
const fromFlights = await fileHandler("./data/fromFlights.json")
setHeaders(res)
res.send(fromFlights)
})
app.get('/toFlights', async (req, res) => {
const toFlights = await fileHandler("./data/toFlights.json")
setHeaders(res)
res.send(toFlights)
})
app.get('/coordinates', async (req, res) => {
const userJson = await fileHandler("./data/users.json")
setHeaders(res)
const users = JSON.parse(userJson).map(u => ({ userId: u.id, coordinates: getCoordinates() }))
res.send(JSON.stringify(users))
})
app.options('/me', async (req, res) => {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Methods", "GET, OPTIONS")
res.header("Access-Control-Allow-Headers", "seed")
res.sendStatus(204)
})
app.get('/robots.txt', async (req, res) => {
res.header("Content-Type",'text/plain');
res.send('User-agent: *\nDisallow: /')
})
app.listen(PORT)
console.log(`App listening on ${PORT}`)