1515'use strict' ;
1616
1717const express = require ( 'express' ) ;
18+ const createTcpPool = require ( './connect-tcp.js' ) ;
1819const mssql = require ( 'mssql' ) ;
1920
2021const app = express ( ) ;
@@ -50,32 +51,8 @@ async function accessSecretVersion(secretName) {
5051 return version . payload . data ;
5152}
5253
53- // [START cloud_sql_sqlserver_mssql_create]
5454const createPool = async ( ) => {
55- const config = { pool : { } , options : { } } ;
56-
57- // Check if a Secret Manager secret version is defined
58- // If a version is defined, retrieve the secret from Secret Manager and set as the DB_PASS
59- const { CLOUD_SQL_CREDENTIALS_SECRET } = process . env ;
60- if ( CLOUD_SQL_CREDENTIALS_SECRET ) {
61- const secrets = await accessSecretVersion ( CLOUD_SQL_CREDENTIALS_SECRET ) ;
62- try {
63- process . env . DB_PASS = secrets . toString ( ) ;
64- } catch ( err ) {
65- err . message = `Unable to parse secret from Secret Manager. Make sure that the secret is JSON formatted: \n ${ err . message } ` ;
66- throw err ;
67- }
68- }
69-
70- config . user = process . env . DB_USER ; // e.g. 'my-db-user'
71- config . password = process . env . DB_PASS ; // e.g. 'my-db-password'
72- config . database = process . env . DB_NAME ; // e.g. 'my-database'
73- // set the server to '172.17.0.1' when connecting from App Engine Flex
74- config . server = process . env . DEPLOYED ? '172.17.0.1' : '127.0.0.1' ;
75- config . port = 1433 ;
76-
77- // [START_EXCLUDE]
78-
55+ const config = { pool : { } } ;
7956 // [START cloud_sql_sqlserver_mssql_timeout]
8057 // 'connectionTimeout` is the maximum number of milliseconds to wait trying to establish an
8158 // initial connection. After the specified amount of time, an exception will be thrown.
@@ -87,7 +64,6 @@ const createPool = async () => {
8764 // and not be checked out before it is automatically closed
8865 ( config . pool . idleTimeoutMillis = 600000 ) ,
8966 // [END cloud_sql_sqlserver_mssql_timeout]
90-
9167 // [START cloud_sql_sqlserver_mssql_limit]
9268 // 'max' limits the total number of concurrent connections this pool will keep. Ideal
9369 // values for this setting are highly variable on app design, infrastructure, and database.
@@ -103,11 +79,21 @@ const createPool = async () => {
10379 config . pool . createRetryIntervalMillis = 200 ;
10480 // [END cloud_sql_sqlserver_mssql_backoff]
10581
106- // [END_EXCLUDE]
107- config . options . trustServerCertificate = true ;
108- return await mssql . connect ( config ) ;
82+ // Check if a Secret Manager secret version is defined
83+ // If a version is defined, retrieve the secret from Secret Manager and set as the DB_PASS
84+ const { CLOUD_SQL_CREDENTIALS_SECRET } = process . env ;
85+ if ( CLOUD_SQL_CREDENTIALS_SECRET ) {
86+ const secrets = await accessSecretVersion ( CLOUD_SQL_CREDENTIALS_SECRET ) ;
87+ try {
88+ process . env . DB_PASS = secrets . toString ( ) ;
89+ } catch ( err ) {
90+ err . message = `Unable to parse secret from Secret Manager. Make sure that the secret is JSON formatted: \n ${ err . message } ` ;
91+ throw err ;
92+ }
93+ }
94+
95+ return createTcpPool ( config ) ;
10996} ;
110- // [END cloud_sql_sqlserver_mssql_create]
11197
11298const ensureSchema = async pool => {
11399 // Wait for tables to be created (if they don't already exist).
@@ -148,7 +134,7 @@ app.use(async (req, res, next) => {
148134} ) ;
149135
150136// Serve the index page, showing vote tallies.
151- app . get ( '/' , async ( req , res ) => {
137+ const httpGet = async ( req , res ) => {
152138 try {
153139 // Get the 5 most recent votes.
154140 const recentVotesQuery = pool
@@ -192,10 +178,12 @@ app.get('/', async (req, res) => {
192178 )
193179 . end ( ) ;
194180 }
195- } ) ;
181+ } ;
182+
183+ app . get ( '/' , httpGet ) ;
196184
197185// Handle incoming vote requests and inserting them into the database.
198- app . post ( '/' , async ( req , res ) => {
186+ const httpPost = async ( req , res ) => {
199187 const { team} = req . body ;
200188 const timestamp = new Date ( ) ;
201189
@@ -236,20 +224,28 @@ app.post('/', async (req, res) => {
236224 // [END cloud_sql_sqlserver_mssql_connection]
237225
238226 res . status ( 200 ) . send ( `Successfully voted for ${ team } at ${ timestamp } ` ) . end ( ) ;
239- } ) ;
240-
241- const PORT = parseInt ( process . env . PORT ) || 8080 ;
242- const server = app . listen ( PORT , ( ) => {
243- console . log ( `App listening on port ${ PORT } ` ) ;
244- console . log ( 'Press Ctrl+C to quit.' ) ;
245- } ) ;
227+ } ;
246228
247- const environment = process . env . NODE_ENV || 'development' ;
248- if ( environment === 'development' ) {
249- process . on ( 'unhandledRejection' , err => {
250- console . error ( err ) ;
251- throw err ;
252- } ) ;
253- }
229+ app . post ( '*' , httpPost ) ;
230+
231+ /**
232+ * Responds to GET and POST requests for TABS vs SPACES sample app.
233+ *
234+ * @param {Object } req Cloud Function request context.
235+ * @param {Object } res Cloud Function response context.
236+ */
237+ exports . votes = ( req , res ) => {
238+ switch ( req . method ) {
239+ case 'GET' :
240+ httpGet ( req , res ) ;
241+ break ;
242+ case 'POST' :
243+ httpPost ( req , res ) ;
244+ break ;
245+ default :
246+ res . status ( 405 ) . send ( { error : 'Something blew up!' } ) ;
247+ break ;
248+ }
249+ } ;
254250
255- module . exports = server ;
251+ module . exports = app ;
0 commit comments