From 48fbdff79ff4fc743cf25b51466da6f579a58dbb Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sun, 26 Feb 2023 16:41:26 +0000 Subject: [PATCH] establish db connection on app start, not on route --- server.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/server.cpp b/server.cpp index fef030d..49f9f53 100644 --- a/server.cpp +++ b/server.cpp @@ -6,19 +6,29 @@ using namespace sql; using namespace std; +Driver *driver; +Connection *con; +Statement *stmt; + +void exiting(){ + delete con; + delete stmt; +} + int main() { crow::SimpleApp app; - CROW_ROUTE(app, "/seconds")([](){ - Driver *driver = get_driver_instance(); - Connection *con = driver->connect(getenv("DBURL"),getenv("DBUSER"),getenv("DBPWD")); - con->setSchema("timer_backend"); - Statement *stmt = con->createStatement(); - ResultSet *res = stmt->executeQuery("SELECT * FROM sessions;"); + driver = get_driver_instance(); + con = driver->connect(getenv("DBURL"),getenv("DBUSER"),getenv("DBPWD")); + con->setSchema("timer_backend"); + stmt = con->createStatement(); + CROW_ROUTE(app, "/seconds")([](){ crow::json::wvalue entries; + ResultSet *res = stmt->executeQuery("SELECT * FROM sessions;"); + int i = 0; while (res->next()) { int id = res->getInt("id"); @@ -29,12 +39,11 @@ int main() } delete res; - delete stmt; - delete con; - return entries; }); app.port(18080).run(); + + atexit(exiting); }