-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
143 lines (106 loc) · 4.08 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/***
*
* Postgres Query Sandbox
*
* Notes
*
* npm install express
* npm install sequelize
*
* Version History
*
* Version Date Author Notes
* 0.8.x 01/30/2023 Barry Solomon Add Promise
*
*/
const { response } = require('express');
const express = require('express');
const os = require('os');
const HOSTNAME = os.hostname();
const PORT = process.env.PORT || 8080
const SERVER_VERSION = "1.0.101";
var thequery = "SELECT username, email, fullname FROM public.users;";
var username = process.env.POSTGRES_USERNAME || "postgresUser";
var userpassword = process.env.POSTGRES_USERPASS || "postgresPW";
var dbname = "postgresDB";
var host = process.env.POSTGRES_SERVER || "postgres"; // "192.168.1.57"; // "postgres"; // "127.0.0.1"; // "AuggieTheDoggie"; //
var port = process.env.POSTGRES_PORT || 5432;
const sequelize = require('sequelize');
const _sequelize = new sequelize(dbname, username, userpassword, {
host: host,
port: port,
dialect: 'postgres',
});
express()
.get('/', (req, res) => {
res.setHeader('Content-Type', 'text/plain');
res.statusCode = 200;
let ordered = Object.keys(process.env).sort().reduce(
(obj, key) => {
obj[key] = process.env[key];
return obj;
},
{}
);
let myArgs = process.argv;
let newDate = new Date();
// Access the provided 'page' and 'limt' query parameters
let the_query = req.query?.query ?? thequery;
let page = req.query?.page;
let limit = req.query?.limit;
res.write('Environment Variables\n\n' + JSON.stringify(ordered, null, 4));
res.write('\n\nArguments:\n\n' + myArgs);
res.write(`\n\nWelcome to CPLN(${HOSTNAME})! The time is: ` + newDate.toUTCString());
// Execute postgres query and echo results back to user
//
return new Promise((resolve, reject) => {
//resolve();
res.write("\nDEBUG: ==>var _sequelize = new sequelize\n\n ");
try {
res.write("\nDEBUG: ==> call return _sequelize:\n");
return _sequelize
.query(the_query, {
type: sequelize.QueryTypes.SELECT,
})
.then(myTableRows => {
res.write("\nDEBUG: ==> .then(myTableRows => {\n");
const result = myTableRows && JSON.stringify(myTableRows);
res.write("\n\nQuery:\n\n " + thequery + "\n");
res.write("\nResults:\n\n " + result);
console.log("myTableRows", JSON.stringify(myTableRows));
console.log("Query result", result);
if (!myTableRows) {
res.write("\nFailed to find raw:\n\n " + result);
reject("Failed to find raw");
}
else {
res.write("\n\nSuccess:\n\n " + result);
resolve();
}
})
}
catch (e) {
console.log("------------------------------\n");
console.log(`stack: ${e.stack}`);
console.log(`error: ${e}`);
console.log("------------------------------\n");
}
})
.then(() => {
res.end("\n\nDone");
})
.catch((e) => {
console.log("------------------------------\n");
console.log(`stack: ${e.stack}`);
console.log(`error: ${e}`);
console.log("------------------------------\n");
})
})
.get('/health', (req, res) => {
res.setHeader('Content-Type', 'text/plain');
res.statusCode = 200;
res.end("We're all good here");
})
.listen(PORT, '0.0.0.0', () => {
console.log(`Server version ${SERVER_VERSION} running at http://${HOSTNAME}:${PORT}/`)
});