-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (43 loc) · 1.34 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
const debug = require("debug")("gc:rest:index");
const express = require("express");
const mongo = require("mongodb");
const compression = require("compression");
const cors = require("cors");
const morgan = require("morgan");
const { find } = require("./lib/find");
const gpx = require("./lib/gpx");
const graphql = require("./lib/graphql");
const metrics = require("./lib/metrics");
async function main() {
const url = process.env["GC_DB_URI"] || "mongodb://localhost:27017";
debug("Connecting to mongodb at %s", url);
const client = await mongo.MongoClient.connect(url);
const db = client.db("gc");
const gcs = db.collection("gcs");
const app = express();
app.use(metrics.expressjs());
app.use(compression());
app.use(cors());
app.use(morgan("tiny"));
const apollo = graphql(db);
apollo.applyMiddleware({ app, path: "/api/graphql" });
app.get("/api/poi.gpx", async (req, res) => {
let cursor;
try {
const cursor = find(gcs, req.query).maxTimeMS(10 * 60 * 1000);
res.type("application/gpx+xml");
await gpx(cursor, data => res.write(data));
res.end("");
} catch (err) {
console.error(err);
res.status(500).send("Something went wrong");
} finally {
if (cursor) cursor.close();
}
});
app.listen(8080);
}
main().catch(err => {
console.log(err);
process.exit(1);
});