-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (68 loc) · 2.23 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require('express')
const fs = require('fs').promises
const path = require('path')
const { parseXYZ } = require('./src/utils')
const renderPool = require('./src/pool')
const config = require('./config')
const logger = require("log4js").getLogger("tile_server")
const app = express()
const port = 3000
app.get('/', async (req, res) => {
try {
const page = await fs.readFile(path.resolve(__dirname, 'assets', 'pages', 'index.html'))
res.writeHead(500, {'Content-Type': 'text/html'})
return res.end(page)
} catch(e) {
logger.error(e)
res.writeHead(500, {'Content-Type': 'text/plain'})
if (config.isDebug) return res.end(e.message)
else return res.end('Server error 500')
}
})
app.get('/:x/:y/:z', async (req, res) => {
const [x, y, z] = parseXYZ(req)
if ([x, y, z].includes(undefined)) {
logger.debug('Error parse x, y, z in ' + req.url)
res.writeHead(500, {'Content-Type': 'text/plain'})
return res.end('Error parse x, y, z')
}
if (config.cache.enable) {
try {
const tile = await fs.readFile(path.resolve(config.cache.directory, [z, x, y].join('/') + '.png'))
res.writeHead(200, {'Content-Type': 'image/png'})
return res.end(tile)
} catch(e) {
logger.debug('Not find cache file ' + path.resolve(config.cache.directory, [z, x, y].join('/') + '.png'))
}
}
try {
logger.debug(`Request to render tile x:${x} y:${y} z:${z}`)
const tile = await renderPool.push(x, y, z)
res.writeHead(200, {'Content-Type': 'image/png'})
return res.end(tile)
} catch(e) {
logger.debug(`Failed render tile x:${x} y:${y} z:${z}`)
logger.error(e)
res.writeHead(500, {'Content-Type': 'text/plain'})
if (config.isDebug) return res.end(e.message)
else return res.end('Server error 500')
}
})
async function main() {
logger.info('Loading stylesheet to mapnik...')
try {
await renderPool.loading()
logger.debug('Loading stylesheet to mapnik succes')
} catch(e) {
logger.fatal(e)
process.exit(1)
}
}
main().then(() => {
app.listen(config.server.port, config.server.address,
() => logger.info(`Tile server start on http://${config.server.address}:${config.server.port}`)
)
}).catch((e) => {
logger.fatal(e)
process.exit(1)
})