forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
225 lines (199 loc) · 7.48 KB
/
server.js
File metadata and controls
225 lines (199 loc) · 7.48 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
// Require process, so we can mock environment variables.
const process = require('process');
const express = require('express');
const bodyParser = require('body-parser');
const Knex = require('knex');
const app = express();
app.set('view engine', 'pug');
app.enable('trust proxy');
// Automatically parse request body as form data.
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
// Set Content-Type for all responses for these routes.
app.use((req, res, next) => {
res.set('Content-Type', 'text/html');
next();
});
// Create a Winston logger that streams to Stackdriver Logging.
const winston = require('winston');
const {LoggingWinston} = require('@google-cloud/logging-winston');
const loggingWinston = new LoggingWinston();
const logger = winston.createLogger({
level: 'info',
transports: [new winston.transports.Console(), loggingWinston],
});
// [START cloud_sql_postgres_knex_create]
// Initialize Knex, a Node.js SQL query builder library with built-in connection pooling.
const connect = () => {
// Configure which instance and what database user to connect with.
// Remember - storing secrets in plaintext is potentially unsafe. Consider using
// something like https://cloud.google.com/kms/ to help keep secrets secret.
const config = {
user: process.env.DB_USER, // e.g. 'my-user'
password: process.env.DB_PASS, // e.g. 'my-user-password'
database: process.env.DB_NAME, // e.g. 'my-database'
};
config.host = `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`;
// Establish a connection to the database
const knex = Knex({
client: 'pg',
connection: config,
});
// ... Specify additional properties here.
// [START_EXCLUDE]
// [START cloud_sql_postgres_knex_limit]
// 'max' limits the total number of concurrent connections this pool will keep. Ideal
// values for this setting are highly variable on app design, infrastructure, and database.
knex.client.pool.max = 5;
// 'min' is the minimum number of idle connections Knex maintains in the pool.
// Additional connections will be established to meet this value unless the pool is full.
knex.client.pool.min = 5;
// [END cloud_sql_postgres_knex_limit]
// [START cloud_sql_postgres_knex_timeout]
// 'acquireTimeoutMillis' is the number of milliseconds before a timeout occurs when acquiring a
// connection from the pool. This is slightly different from connectionTimeout, because acquiring
// a pool connection does not always involve making a new connection, and may include multiple retries.
// when making a connection
knex.client.pool.acquireTimeoutMillis = 60000; // 60 seconds
// 'createTimeoutMillis` is the maximum number of milliseconds to wait trying to establish an
// initial connection before retrying.
// After acquireTimeoutMillis has passed, a timeout exception will be thrown.
knex.client.pool.createTimeoutMillis = 30000; // 30 seconds
// 'idleTimeoutMillis' is the number of milliseconds a connection must sit idle in the pool
// and not be checked out before it is automatically closed.
knex.client.pool.idleTimeoutMillis = 600000; // 10 minutes
// [END cloud_sql_postgres_knex_timeout]
// [START cloud_sql_postgres_knex_backoff]
// 'knex' uses a built-in retry strategy which does not implement backoff.
// 'createRetryIntervalMillis' is how long to idle after failed connection creation before trying again
knex.client.pool.createRetryIntervalMillis = 200; // 0.2 seconds
// [END cloud_sql_postgres_knex_backoff]
// [END_EXCLUDE]
return knex;
};
const knex = connect();
// [END cloud_sql_postgres_knex_create]
// [START cloud_sql_postgres_knex_connection]
/**
* Insert a vote record into the database.
*
* @param {object} knex The Knex connection object.
* @param {object} vote The vote record to insert.
* @returns {Promise}
*/
const insertVote = async (knex, vote) => {
try {
return await knex('votes').insert(vote);
} catch (err) {
throw Error(err);
}
};
// [END cloud_sql_postgres_knex_connection]
/**
* Retrieve the latest 5 vote records from the database.
*
* @param {object} knex The Knex connection object.
* @returns {Promise}
*/
const getVotes = async (knex) => {
return await knex
.select('candidate', 'time_cast')
.from('votes')
.orderBy('time_cast', 'desc')
.limit(5);
};
/**
* Retrieve the total count of records for a given candidate
* from the database.
*
* @param {object} knex The Knex connection object.
* @param {object} candidate The candidate for which to get the total vote count
* @returns {Promise}
*/
const getVoteCount = async (knex, candidate) => {
return await knex('votes').count('vote_id').where('candidate', candidate);
};
app.get('/', (req, res) => {
(async function () {
// Query the total count of "TABS" from the database.
const tabsResult = await getVoteCount(knex, 'TABS');
const tabsTotalVotes = parseInt(tabsResult[0].count);
// Query the total count of "SPACES" from the database.
const spacesResult = await getVoteCount(knex, 'SPACES');
const spacesTotalVotes = parseInt(spacesResult[0].count);
// Query the last 5 votes from the database.
const votes = await getVotes(knex);
// Calculate and set leader values.
let leadTeam = '';
let voteDiff = 0;
let leaderMessage = '';
if (tabsTotalVotes !== spacesTotalVotes) {
if (tabsTotalVotes > spacesTotalVotes) {
leadTeam = 'TABS';
voteDiff = tabsTotalVotes - spacesTotalVotes;
} else {
leadTeam = 'SPACES';
voteDiff = spacesTotalVotes - tabsTotalVotes;
}
leaderMessage = `${leadTeam} are winning by ${voteDiff} vote${
voteDiff > 1 ? 's' : ''
}.`;
} else {
leaderMessage = 'TABS and SPACES are evenly matched!';
}
res.render('index.pug', {
votes: votes,
tabsCount: tabsTotalVotes,
spacesCount: spacesTotalVotes,
leadTeam: leadTeam,
voteDiff: voteDiff,
leaderMessage: leaderMessage,
});
})();
});
app.post('/', async (req, res) => {
// Get the team from the request and record the time of the vote.
const {team} = req.body;
const timestamp = new Date();
if (!team || (team !== 'TABS' && team !== 'SPACES')) {
res.status(400).send('Invalid team specified.').end();
return;
}
// Create a vote record to be stored in the database.
const vote = {
candidate: team,
time_cast: timestamp,
};
// Save the data to the database.
try {
await insertVote(knex, vote);
} catch (err) {
logger.error(`Error while attempting to submit vote:${err}`);
res
.status(500)
.send('Unable to cast vote; see logs for more details.')
.end();
return;
}
res.status(200).send(`Successfully voted for ${team} at ${timestamp}`).end();
});
const PORT = process.env.PORT || 8080;
const server = app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
module.exports = server;