File tree Expand file tree Collapse file tree 3 files changed +32
-14
lines changed
Expand file tree Collapse file tree 3 files changed +32
-14
lines changed Original file line number Diff line number Diff line change 2020
2121############ TABS vs. SPACES App for Cloud Functions ############
2222
23- # initiate a connection pool to a Cloud SQL database
24- db = init_connection_pool ()
25- # creates required 'votes' table in database (if it does not exist)
26- migrate_db (db )
23+ # lazy global initialization
24+ # db connection must be established within request context
25+ db = None
2726
2827
2928@functions_framework .http
@@ -36,6 +35,13 @@ def votes(request: Request) -> Response:
3635 Returns:
3736 Flask HTTP Response to the client.
3837 """
38+ global db
39+ # initialize db within request context
40+ if not db :
41+ # initiate a connection pool to a Cloud SQL database
42+ db = init_connection_pool ()
43+ # creates required 'votes' table in database (if it does not exist)
44+ migrate_db (db )
3945 if request .method == "GET" :
4046 context = get_index_context (db )
4147 return render_template ("index.html" , ** context )
Original file line number Diff line number Diff line change 2020
2121############ TABS vs. SPACES App for Cloud Functions ############
2222
23- # initiate a connection pool to a Cloud SQL database
24- db = init_connection_pool ()
25- # creates required 'votes' table in database (if it does not exist)
26- migrate_db (db )
23+ # lazy global initialization
24+ # db connection must be established within request context
25+ db = None
2726
2827
2928@functions_framework .http
3029def votes (request : Request ) -> Response :
30+ global db
31+ # initialize db within request context
32+ if not db :
33+ # initiate a connection pool to a Cloud SQL database
34+ db = init_connection_pool ()
35+ # creates required 'votes' table in database (if it does not exist)
36+ migrate_db (db )
3137 if request .method == "GET" :
3238 context = get_index_context (db )
3339 return render_template ("index.html" , ** context )
Original file line number Diff line number Diff line change 1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- from flask import render_template , Response
15+ from flask import render_template , Request , Response
1616
1717import functions_framework
1818
1919from app import get_index_context , init_connection_pool , migrate_db , save_vote
2020
2121############ TABS vs. SPACES App for Cloud Functions ############
2222
23- # initiate a connection pool to a Cloud SQL database
24- db = init_connection_pool ()
25- # creates required 'votes' table in database (if it does not exist)
26- migrate_db (db )
23+ # lazy global initialization
24+ # db connection must be established within request context
25+ db = None
2726
2827
2928@functions_framework .http
30- def votes (request ):
29+ def votes (request : Request ) -> Response :
30+ global db
31+ # initialize db within request context
32+ if not db :
33+ # initiate a connection pool to a Cloud SQL database
34+ db = init_connection_pool ()
35+ # creates required 'votes' table in database (if it does not exist)
36+ migrate_db (db )
3137 if request .method == "GET" :
3238 context = get_index_context (db )
3339 return render_template ("index.html" , ** context )
You can’t perform that action at this time.
0 commit comments