-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (58 loc) · 1.9 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
const dbconfig = require('./config.json');
const pg = require('pg');
const express = require('express');
const logger = require('morgan');
const app = express();
const pool = new pg.Pool(dbconfig);
app.use(logger(':date[iso] ":method :url"'));
const port = 8306;
/*
const DirCache = require('./dircache.js')
const cache = new DirCache(`./cache/${dbconfig.database?dbconfig.database:process.env.PGDATABASE?process.env.PGDATABASE:''}`);
let cacheMiddleware = async(req, res, next) => {
if (!cache) {
next();
return;
}
const cacheDir = `${req.params.table}/bbox`
const key = ((req.query.geom_column?req.query.geom_column:'geom')
+ (req.query.columns?'_'+req.query.columns:'')
+ (req.query.filter?'_'+req.query.filter:''))
.replace(/[\W]+/g, '_');
const bbox = await cache.getCachedFile(cacheDir, key);
if (bbox) {
console.log(`bbox cache hit for ${cacheDir}?${key}`);
res.header('Content-Type', 'application/json').send(bbox);
return;
} else {
res.sendResponse = res.send;
res.send = (body) => {
if (res.statusCode >= 200 && res.statusCode < 300) {
cache.setCachedFile(cacheDir, key, body);
}
res.sendResponse(body);
}
next();
}
}
*/
app.get('/api/query/:z/:x/:y', async (request, response)=>{
let sql = `select *
from mytable
where
z = ${request.params.z}
and x = ${request.params.x}
and y = ${request.params.y}
and ok = '${request.query.a}'
and text = '${request.query.b}'`;
//console.log(sql);
try {
let queryResult = await pool.query(sql);
response.json(queryResult.rows);
} catch(err) {
response.status(500).send(err.message);
}
})
const server = app.listen(port);
server.setTimeout(600000);
console.log(`pgserver listening on port ${port}`);