-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.ts
More file actions
55 lines (49 loc) · 1.79 KB
/
server.ts
File metadata and controls
55 lines (49 loc) · 1.79 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
import express from 'express';
import ViteExpress from 'vite-express';
import mapRouter from './routes/map';
import fetch from 'node-fetch';
import { loadEnvFile } from 'node:process';
import * as fs from 'node:fs';
const app = express();
const PORT = 3001;
loadEnvFile();
app.use('/map', mapRouter);
// Get local DEM tiles
app.get('/dem/:z/:x/:y.png', async(req, res) => {
try {
const regExp=/^\d+$/;
if(regExp.exec(req.params.x) && regExp.exec(req.params.y) && regExp.exec(req.params.z)) {
const filename = `${process.env.TERRARIUM_TILES}/${req.params.z}/${req.params.x}/${req.params.y}.png`;
fs.createReadStream(filename)
.on('error', (e: any) => {
const notFound = e.code == 'ENOENT';
res.status(notFound ? 404 : 500)
.json({
"error": notFound ? "Can't find file": "Unknown error loading tile"
})
})
.pipe(res);
} else {
res.status(400).json({error:"x, y and z must be integers"});
}
} catch(e) {
res.status(500).json({error: e});
}
});
// Get DEM tiles from AWS
app.get('/dem/aws/:z/:x/:y.png', async(req, res) => {
try {
const resp = await fetch(`https://s3.amazonaws.com/elevation-tiles-prod/terrarium/${req.params.z}/${req.params.x}/${req.params.y}.png`);
if(resp.status == 200) {
res.set('Content-Type', 'image/png');
resp.body?.pipe(res);
} else {
res.status(resp.status).json({error: 'Could not retrieve DEM'});
}
} catch(e) {
res.status(500).json({error: e});
}
});
ViteExpress.listen(app, PORT, () => {
console.log(`App listening on port ${PORT}.`);
});