Move apiserver.app to module
This commit is contained in:
parent
3d6eb29a93
commit
0c899f258d
83
apiserver/app.js
Normal file
83
apiserver/app.js
Normal file
@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Express app
|
||||
*/
|
||||
const express = require('express')
|
||||
|
||||
// db connection pool
|
||||
const db = require('./db')
|
||||
|
||||
// set up app
|
||||
const app = express()
|
||||
|
||||
// building geometry by lat/lon - TODO join geometry against buildings
|
||||
app.get('/buildings', function(req, res){
|
||||
const { lng, lat } = req.query
|
||||
db.query(
|
||||
`SELECT
|
||||
b.building_id as id,
|
||||
b.building_doc as doc,
|
||||
g.geometry_id as geometry_id
|
||||
FROM buildings as b, geometries as g
|
||||
WHERE
|
||||
b.geometry_id = g.geometry_id
|
||||
AND
|
||||
ST_Intersects(
|
||||
ST_Transform(
|
||||
ST_SetSRID(ST_Point($1, $2), 4326),
|
||||
3857
|
||||
),
|
||||
g.geometry_geom
|
||||
)
|
||||
LIMIT 1
|
||||
`,
|
||||
[lng, lat]
|
||||
).then(function(data){
|
||||
const rows = data.rows
|
||||
if (rows.length){
|
||||
const id = rows[0].id
|
||||
const doc = rows[0].doc
|
||||
const geometry_id = rows[0].geometry_id
|
||||
console.log(lng, lat, id)
|
||||
doc.id = id
|
||||
doc.geometry_id = geometry_id
|
||||
res.send(doc)
|
||||
} else {
|
||||
console.log(lng, lat, undefined)
|
||||
res.status(404).send({error:'Not Found'})
|
||||
}
|
||||
}).catch(function(error){
|
||||
console.error('Error:', error)
|
||||
res.status(500).send({error:'Database error'})
|
||||
})
|
||||
})
|
||||
|
||||
app.post('/user', function(req, res){
|
||||
db.query(
|
||||
`INSERT
|
||||
INTO users (
|
||||
user_id,
|
||||
username,
|
||||
email,
|
||||
pass
|
||||
) VALUES (
|
||||
gen_random_uuid(),
|
||||
%s,
|
||||
%s,
|
||||
crypt(%s, gen_salt('bf')); -- hash (max password input length is 72)
|
||||
)
|
||||
`
|
||||
)
|
||||
})
|
||||
|
||||
app.get('/user', function(req, res){
|
||||
db.query(
|
||||
`SELECT
|
||||
(
|
||||
hash = crypt(%s, pass)
|
||||
) AS authenticated
|
||||
FROM;
|
||||
`
|
||||
)
|
||||
})
|
||||
|
||||
module.exports = app
|
@ -1,54 +1,6 @@
|
||||
/**
|
||||
* Serve API
|
||||
*/
|
||||
const express = require('express')
|
||||
const app = require('./app')
|
||||
|
||||
// db connection pool
|
||||
const db = require('./db')
|
||||
|
||||
// set up app
|
||||
const app = express()
|
||||
|
||||
// building geometry by lat/lon - TODO join geometry against buildings
|
||||
app.get('/buildings', function(req, res){
|
||||
const { lng, lat } = req.query
|
||||
db.query(
|
||||
`SELECT
|
||||
b.building_id as id,
|
||||
b.building_doc as doc,
|
||||
g.geometry_id as geometry_id
|
||||
FROM buildings as b, geometries as g
|
||||
WHERE
|
||||
b.geometry_id = g.geometry_id
|
||||
AND
|
||||
ST_Intersects(
|
||||
ST_Transform(
|
||||
ST_SetSRID(ST_Point($1, $2), 4326),
|
||||
3857
|
||||
),
|
||||
g.geometry_geom
|
||||
)
|
||||
LIMIT 1
|
||||
`,
|
||||
[lng, lat]
|
||||
).then(function(data){
|
||||
const rows = data.rows
|
||||
if (rows.length){
|
||||
const id = rows[0].id
|
||||
const doc = rows[0].doc
|
||||
const geometry_id = rows[0].geometry_id
|
||||
console.log(lng, lat, id)
|
||||
doc.id = id
|
||||
doc.geometry_id = geometry_id
|
||||
res.send(doc)
|
||||
} else {
|
||||
console.log(lng, lat, undefined)
|
||||
res.status(404).send({error:'Not Found'})
|
||||
}
|
||||
}).catch(function(error){
|
||||
console.error('Error:', error)
|
||||
res.status(500).send({error:'Database error'})
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(8081, () => console.log('API server listening on port 8081'))
|
||||
app.listen(8081, () => console.log('API server listening on port 8081'))
|
||||
|
Loading…
Reference in New Issue
Block a user