-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (62 loc) · 1.86 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
const snowflake = require('snowflake-sdk')
const express = require('express')
require('dotenv').config()
const account = process.env.SNOWFLAKE_ACCOUNT
const username = process.env.SNOWFLAKE_USERNAME
const password = process.env.SNOWFLAKE_PASSWORD
const database = process.env.SNOWFLAKE_DATABASE
const connectionPool = snowflake.createPool(
{
account,
username,
password,
authenticator: 'SNOWFLAKE',
database
},
{
max: 10,
min: 0
}
);
const sqlText = `select * from serhant_share.serhant.luxury_presence_property_slugs WHERE mls_property_id ILIKE '%' || :1 LIMIT 1`
const listing = (req, res) => {
const mlsnumber = req.params.mlsnumber?.split('-').pop()
if (mlsnumber.length < 4) {
res.status(400)
res.end()
return
}
connectionPool.use(async (clientConnection) => {
await clientConnection.execute({
sqlText,
binds:[mlsnumber],
complete: function (err, stmt, rows)
{
const stream = stmt.streamRows();
let url = false
stream.on('data', function (row)
{
console.log('Found', mlsnumber, row.SLUG)
url = 'https://serhant.com/properties/' + row.SLUG
});
stream.on('end', function (row)
{
if (!url) {
console.log('Not Found', mlsnumber)
url = 'https://serhant.com/properties/' + mlsnumber
}
res.redirect(url)
res.end()
});
}
});
});
}
const health = (req, res) => {
res.status(200)
res.end()
}
const app = express()
app.get('/listing/:mlsnumber', listing)
app.get('/health', health)
app.listen(process.env.PORT || 8080)