diff --git a/config.example.json b/config.example.json index d359752c..6e23743e 100644 --- a/config.example.json +++ b/config.example.json @@ -26,6 +26,26 @@ "key": "" } ],*/ + "db_clients":[ + { + //name:Name of the client,'default' by default + //"name":"", + //rdbms:server type, "postgreSQL" by default + "rdbms": "postgreSQL", + //host:server address,localhost by default + "host": "127.0.0.1", + //port:server port, 5432 by default + "port": 5432, + //dbname:Database name + "dbname": "test", + //user:'postgres' by default + "user": "", + //passwd:'' by default + "passwd": "", + //connection_number:1 by default + "connection_number":1 + } + ], "app": { //threads_num:num of threads,1 by default "threads_num": 16, diff --git a/drogon_ctl/templates/config.csp b/drogon_ctl/templates/config.csp index 2080fec6..4f18bbd4 100644 --- a/drogon_ctl/templates/config.csp +++ b/drogon_ctl/templates/config.csp @@ -26,6 +26,27 @@ "key": "" } ],*/ + /* + "db_clients": [ + { + //name:Name of the client,'default' by default + //"name":"", + //rdbms:server type, "postgreSQL" by default + "rdbms": "postgreSQL", + //host:server address,localhost by default + "host": "127.0.0.1", + //port:server port, 5432 by default + "port": 5432, + //dbname:Database name + "dbname": "test", + //user:'postgres' by default + "user": "", + //passwd:'' by default + "passwd": "", + //connection_number:1 by default + "connection_number": 1 + } + ],*/ "app": { //threads_num:num of threads,1 by default "threads_num": 16, @@ -89,18 +110,23 @@ "use_gzip": true, //static_files_cache_time:5 (seconds) by default,the time in which static file response is cached, //0 means cache forever,the negative value means no cache - "static_files_cache_time":5, + "static_files_cache_time": 5, //simple_controllers_map:Configuring mapping from path to simple controller "simple_controllers_map": [ { "path": "/path/name", "controller": "controllerClassName", - "http_methods": ["get","post"], - "filters": ["FilterClassName"] + "http_methods": [ + "get", + "post" + ], + "filters": [ + "FilterClassName" + ] } ], //idle_connection_timeout: defaults to 60 seconds, the lifetime //of the connection without read or write - "idle_connection_timeout":60 + "idle_connection_timeout": 60 } -} +} \ No newline at end of file diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 16abedd6..c3829529 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -15,6 +15,9 @@ #pragma once #include <drogon/config.h> +#if USE_POSTGRESQL +#include <drogon/orm/DbClient.h> +#endif #include <drogon/utils/Utilities.h> #include <drogon/HttpApiBinder.h> #include <trantor/utils/NonCopyable.h> @@ -137,6 +140,17 @@ class HttpAppFramework : public trantor::NonCopyable virtual void setStaticFilesCacheTime(int cacheTime) = 0; virtual int staticFilesCacheTime() const = 0; virtual void setIdleConnectionTimeout(size_t timeout) = 0; +#if USE_POSTGRESQL + virtual orm::DbClientPtr getDbClient(const std::string &name = "default") = 0; + virtual void createDbClient(const std::string &dbType, + const std::string &host, + const u_short port, + const std::string &databaseName, + const std::string &userName, + const std::string &password, + const size_t connectionNum = 1, + const std::string &name = "default") = 0; +#endif private: virtual void registerHttpApiController(const std::string &pathPattern, diff --git a/lib/src/ConfigLoader.cc b/lib/src/ConfigLoader.cc index f894f502..c8d85cb9 100644 --- a/lib/src/ConfigLoader.cc +++ b/lib/src/ConfigLoader.cc @@ -216,6 +216,28 @@ static void loadApp(const Json::Value &app) auto kickOffTimeout = app.get("idle_connection_timeout", 60).asUInt64(); HttpAppFramework::instance().setIdleConnectionTimeout(kickOffTimeout); } +static void loadDbClients(const Json::Value &dbClients) +{ + if (!dbClients) + return; + for (auto &client : dbClients) + { + auto type = client.get("rdbms", "postgreSQL").asString(); + auto host = client.get("host", "127.0.0.1").asString(); + auto port = client.get("port", 5432).asUInt(); + auto dbname = client.get("dbname", "").asString(); + if (dbname == "") + { + std::cerr << "Please configure dbname in the configuration file" << std::endl; + exit(1); + } + auto user = client.get("user", "postgres").asString(); + auto password = client.get("passwd", "").asString(); + auto connNum = client.get("connection_number", 1).asUInt(); + auto name = client.get("name", "default").asString(); + HttpAppFramework::instance().createDbClient(type, host, (u_short)port, dbname, user, password, connNum, name); + } +} static void loadListeners(const Json::Value &listeners) { if (!listeners) @@ -246,4 +268,5 @@ void ConfigLoader::load() loadApp(_configJsonRoot["app"]); loadSSL(_configJsonRoot["ssl"]); loadListeners(_configJsonRoot["listeners"]); + loadDbClients(_configJsonRoot["db_clients"]); } diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 62b2ce13..4a8c77d9 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -1215,3 +1215,32 @@ HttpAppFramework &HttpAppFramework::instance() HttpAppFramework::~HttpAppFramework() { } + +#if USE_POSTGRESQL +orm::DbClientPtr HttpAppFrameworkImpl::getDbClient(const std::string &name) +{ + return _dbClientsMap[name]; +} + +void HttpAppFrameworkImpl::createDbClient(const std::string &dbType, + const std::string &host, + const u_short port, + const std::string &databaseName, + const std::string &userName, + const std::string &password, + const size_t connectionNum, + const std::string &name) +{ + if (dbType == "postgreSQL") + { + auto connStr = formattedString("host=%s port=%u dbname=%s user=%s", host.c_str(), port, databaseName.c_str(), userName.c_str()); + if (!password.empty()) + { + connStr += " password="; + connStr += password; + } + auto client = drogon::orm::DbClient::newPgClient(connStr, connectionNum); + _dbClientsMap[name] = client; + } +} +#endif diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index bb8e17a0..638825b5 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -85,7 +85,17 @@ class HttpAppFrameworkImpl : public HttpAppFramework } trantor::EventLoop *loop(); - +#if USE_POSTGRESQL + virtual orm::DbClientPtr getDbClient(const std::string &name = "default") override; + virtual void createDbClient(const std::string &dbType, + const std::string &host, + const u_short port, + const std::string &databaseName, + const std::string &userName, + const std::string &password, + const size_t connectionNum = 1, + const std::string &name = "default") override; +#endif private: virtual void registerHttpApiController(const std::string &pathPattern, const HttpApiBinderBasePtr &binder, @@ -203,5 +213,9 @@ class HttpAppFrameworkImpl : public HttpAppFramework int _staticFilesCacheTime = 5; std::unordered_map<std::string, std::weak_ptr<HttpResponse>> _staticFilesCache; std::mutex _staticFilesCacheMutex; +#if USE_POSTGRESQL + std::map<std::string, orm::DbClientPtr> _dbClientsMap; +#endif }; + } // namespace drogon