drogon鍏ラ棬鏁欑▼涓?鎺у埗鍣ㄥ拰鏁版嵁搴撳鎴风浣跨敤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了drogon鍏ラ棬鏁欑▼涓?鎺у埗鍣ㄥ拰鏁版嵁搴撳鎴风浣跨敤相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/stc' title='stc'>stc
buffer adb tls postgre rip his std document2020骞?1鏈?6鏃?1:36:59
powershell
鍦╲scode cmake缂栬瘧锛屾敞鎰? drogon_ctl -v 鏌ョ湅浣犵殑搴撴槸鍚﹁繛鎺ユ垚鍔?Libraries: postgresql: no (batch mode: no) mariadb: yes sqlite3: yes openssl: yes brotli: yes boost: no c-ares: yes 鍥犱负瑕佷娇鐢ㄥ埌mysql 鎺у埗鍣? drogon_ctl create controller -h <[namespace::]class_name> drogon_ctl create controller -h demo::v1::User 鎺у埗鍣ㄥご鏂囦欢 TestCtrl.h #pragma once #include <drogon/HttpController.h> using namespace drogon; namespace web { class TestCtrl:public drogon::HttpController<TestCtrl> { public: METHOD_LIST_BEGIN // METHOD_ADD(TestCtrl::tt,"/tt",Get); ADD_METHOD_TO(TestCtrl::name,"/name",Get); ADD_METHOD_TO(TestCtrl::tt,"/tt",Get); METHOD_LIST_END void tt(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback); void name(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback) const; }; } 娉ㄦ剰锛欰DD_METHOD_TO鍜孧ETHOD_ADD鏂规硶鐨勫尯鍒?METHOD_ADD鏄寜鐓х┖闂村悕绉?/span>+鏂囦欢鍚嶇О+鏂规硶鍚嶇О http://127.0.0.1:888/web/TestCtrl/name ADD_METHOD_TO 灏辨槸鐩存帴 http://127.0.0.1:888/name 涓€鑸紶缁熺殑mvc鎴戜滑浼氬垎鏂囦欢澶癸紝姣斿web锛宎dmin锛宎pi涔嬬被鐨勬潵鍖哄垎鎺у埗鍣ㄦ枃浠跺叆鍙o紝杩欎釜灏遍渶瑕佸幓淇敼CMakeLists.txt aux_source_directory(controllers CTL_SRC) aux_source_directory(filters FILTER_SRC) aux_source_directory(plugins PLUGIN_SRC) aux_source_directory(models MODEL_SRC) HttpSimpleController鍜孒ttpController娉ㄦ剰鍖哄垎涓€涓?閰嶇疆鏁版嵁搴擄紝杩欎釜鏈変袱涓厤缃枃浠朵竴涓槸config.json锛岃繕鏈変竴涓猰odels涓嬮潰鐨刴odel.json main.cc #include <drogon/drogon.h> int main() { //Set HTTP listener address and port // drogon::app().addListener("0.0.0.0",888); //Load config file drogon::app().loadConfigFile("../../config.json"); //Run HTTP framework,the method will block in the internal event loop drogon::app().run(); return 0; } 鎴戠紪璇戠殑鏄湪build/debug娴嬭瘯锛屾敞鎰忎綘鐨刢onfig.json鏂囦欢鐩稿搴旂殑浣嶇疆 /* This is a JSON format configuration file */ { //ssl:The global ssl files setting // "ssl": { // "cert": "../../trantor/trantor/tests/server.pem", // "key": "../../trantor/trantor/tests/server.pem" // }, "listeners": [ { //address: Ip address,0.0.0.0 by default "address": "0.0.0.0", //port: Port number "port": 888, //https: If true, use https for security,false by default "https": false } // , // { // "address": "0.0.0.0", // "port": 443, // "https": true, // //cert,key: Cert file path and key file path, empty by default, // //if empty, use the global setting // "cert": "", // "key": "", // //use_old_tls: enable the TLS1.0/1.1, false by default // "use_old_tls": false // } ], "db_clients": [ { //name: Name of the client,鈥榙efault鈥?nbsp;by default //"name":"", //rdbms: Server type, postgresql,mysql or sqlite3, "postgresql" by default "rdbms": "mysql", //filename: Sqlite3 db file name //"filename":"", //host: Server address,localhost by default "host": "172.18.1.9", //port: Server port, 5432 by default "port": 3306, //dbname: Database name "dbname": "v2", //user: 鈥榩ostgres鈥?nbsp;by default "user": "zx", //passwd: 鈥樷€?nbsp;by default "passwd": "zx", //is_fast: false by default, if it is true, the client is faster but user can鈥榯 call //any synchronous interface of it. "is_fast": false, //client_encoding: The character set used by the client. it is empty string by default which //means use the default character set. //"client_encoding": "", //connection_number: 1 by default, if the 鈥榠s_fast鈥?nbsp;is true, the number is the number of //connections per IO thread, otherwise it is the total number of all connections. "connection_number": 1 } ], "app": { //threads_num: The number of IO threads, 1 by default, if the value is set to 0, the number of threads //is the number of CPU cores "threads_num": 1, //enable_session: False by default "enable_session": false, "session_timeout": 0, //document_root: Root path of HTTP document, defaut path is ./ "document_root": "./", //home_page: Set the html file of the home page, the default value is "index.html" //If there isn鈥榯 any handler registered to the path "/", the home page file in the "document_root" is send to clients as a response //to the request for "/". "home_page": "index.html", //static_file_headers: Headers for static files /*"static_file_headers": [ { "name": "field-name", "value": "field-value" } ],*/ //upload_path: The path to save the uploaded file. "uploads" by default. //If the path isn鈥榯 prefixed with /, ./ or ../, //it is relative path of document_root path "upload_path": "uploads", /* file_types: * HTTP download file types,The file types supported by drogon * by default are "html", "js", "css", "xml", "xsl", "txt", "svg", * "ttf", "otf", "woff2", "woff" , "eot", "png", "jpg", "jpeg", * "gif", "bmp", "ico", "icns", etc. */ "file_types": [ "gif", "png", "jpg", "js", "css", "html", "ico", "swf", "xap", "apk", "cur", "xml" ], //locations: An array of locations of static files for GET requests. "locations": [{ //uri_prefix: The URI prefix of the location prefixed with "/", the default value is "" that disables the location. //"uri_prefix": "/.well-known/acme-challenge/", //default_content_type: The default content type of the static files without //an extension. empty string by default. "default_content_type": "text/plain", //alias: The location in file system, if it is prefixed with "/", it //presents an absolute path, otherwise it presents a relative path to //the document_root path. //The default value is "" which means use the document root path as the location base path. "alias": "", //is_case_sensitive: indicates whether the URI prefix is case sensitive. "is_case_sensitive": false, //allow_all: true by default. If it is set to false, only static files with a valid extension can be accessed. "allow_all": true, //is_recursive: true by default. If it is set to false, files in sub directories can鈥榯 be accessed. "is_recursive": true, //filters: string array, the filters applied to the location. "filters": [] }], //max_connections: maximum connections number,100000 by default "max_connections": 100000, //max_connections_per_ip: maximum connections number per clinet,0 by default which means no limit "max_connections_per_ip": 0, //Load_dynamic_views: False by default, when set to true, drogon //compiles and loads dynamically "CSP View Files" in directories defined //by "dynamic_views_path" "load_dynamic_views": false, //dynamic_views_path: If the path isn鈥榯 prefixed with /, ./ or ../, //it is relative path of document_root path "dynamic_views_path": [ "./views" ], //dynamic_views_output_path: Default by an empty string which means the output path of source //files is the path where the csp files locate. If the path isn鈥榯 prefixed with /, it is relative //path of the current working directory. "dynamic_views_output_path": "", //enable_unicode_escaping_in_json: true by default, enable unicode escaping in json. "enable_unicode_escaping_in_json": true, //log: Set log output, drogon output logs to stdout by default "log": { //log_path: Log file path,empty by default,in which case,logs are output to the stdout //"log_path": "./", //logfile_base_name: Log file base name,empty by default which means drogon names logfile as //drogon.log ... "logfile_base_name": "", //log_size_limit: 100000000 bytes by default, //When the log file size reaches "log_size_limit", the log file is switched. "log_size_limit": 100000000, //log_level: "DEBUG" by default,options:"TRACE","DEBUG","INFO","WARN" //The TRACE level is only valid when built in DEBUG mode. "log_level": "DEBUG" }, //run_as_daemon: False by default "run_as_daemon": false, //relaunch_on_error: False by default, if true, the program will be restart by the parent after exiting; "relaunch_on_error": false, //use_sendfile: True by default, if true, the program //uses sendfile() system-call to send static files to clients; "use_sendfile": true, //use_gzip: True by default, use gzip to compress the response body鈥榮 content; "use_gzip": true, //use_brotli: False by default, use brotli to compress the response body鈥榮 content; "use_brotli": false, //static_files_cache_time: 5 (seconds) by default, the time in which the static file response is cached, //0 means cache forever, the negative value means no cache "static_files_cache_time": 5, //simple_controllers_map: Used to configure mapping from path to simple controller "simple_controllers_map": [{ "path": "/path/name", "controller": "controllerClassName", "http_methods": [ "get", "post" ], "filters": [ ] }], //idle_connection_timeout: Defaults to 60 seconds, the lifetime //of the connection without read or write "idle_connection_timeout": 60, //server_header_field: Set the 鈥楽erver鈥?nbsp;header field in each response sent by drogon, //empty string by default with which the 鈥楽erver鈥?nbsp;header field is set to "Server: drogon/version string " "server_header_field": "", //enable_server_header: Set true to force drogon to add a 鈥楽erver鈥?nbsp;header to each HTTP response. The default //value is true. "enable_server_header": true, //enable_date_header: Set true to force drogon to add a 鈥楧ate鈥?nbsp;header to each HTTP response. The default //value is true. "enable_date_header": true, //keepalive_requests: Set the maximum number of requests that can be served through one keep-alive connection. //After the maximum number of requests are made, the connection is closed. //The default value of 0 means no limit. "keepalive_requests": 0, //pipelining_requests: Set the maximum number of unhandled requests that can be cached in pipelining buffer. //After the maximum number of requests are made, the connection is closed. //The default value of 0 means no limit. "pipelining_requests": 0, //gzip_static: If it is set to true, when the client requests a static file, drogon first finds the compressed //file with the extension ".gz" in the same path and send the compressed file to the client. //The default value of gzip_static is true. "gzip_static": true, //br_static: If it is set to true, when the client requests a static file, drogon first finds the compressed //file with the extension ".br" in the same path and send the compressed file to the client. //The default value of br_static is true. "br_static": true, //client_max_body_size: Set the maximum body size of HTTP requests received by drogon. The default value is "1M". //One can set it to "1024", "1k", "10M", "1G", etc. Setting it to "" means no limit. "client_max_body_size": "1M", //max_memory_body_size: Set the maximum body size in memory of HTTP requests received by drogon. The default value is "64K" bytes. //If the body size of a HTTP request exceeds this limit, the body is stored to a temporary file for processing. //Setting it to "" means no limit. "client_max_memory_body_size": "64K", //client_max_websocket_message_size: Set the maximum size of messages sent by WebSocket client. The default value is "128K". //One can set it to "1024", "1k", "10M", "1G", etc. Setting it to "" means no limit. "client_max_websocket_message_size": "128K" }, //plugins: Define all plugins running in the application "plugins": [{ //name: The class name of the plugin //"name": "drogon::plugin::SecureSSLRedirector", //dependencies: Plugins that the plugin depends on. It can be commented out "dependencies": [], //config: The configuration of the plugin. This json object is the parameter to initialize the plugin. //It can be commented out "config": { "ssl_redirect_exempt": [".*\.jpg"], "secure_ssl_host": "localhost:8849" } }], //custom_config: custom configuration for users. This object can be get by the app().getCustomConfig() method. "custom_config": {} } model.json { //rdbms: server type, postgresql,mysql or sqlite3 "rdbms": "mysql", //filename: sqlite3 db file name //"filename":"", //host: server address,localhost by default; "host": "172.18.1.9", //port: server port, 5432 by default; "port": 3306, //dbname: Database name; "dbname": "v2", //schema: valid for postgreSQL, "public" by default; "schema": "public", //user: User name "user": "zx", //password or passwd: Password "password": "zx", //client_encoding: The character set used by drogon_ctl. it is empty string by default which //means use the default character set. //"client_encoding": "", //table: An array of tables to be modelized. if the array is empty, all revealed tables are modelized. "tables": [], "restful_api_controllers": { "enabled": false, // resource_uri: The URI to access the resource, the default value // is 鈥?*鈥?nbsp;in which the asterisk represents the table name. // If this option is set to a empty string, the URI is composed of the namespaces and the class name. "resource_uri": "/*", // class_name: "Restful*Ctrl" by default, the asterisk represents the table name. // This option can contain namespaces. "class_name": "Restful*Ctrl", // filters: an array of filter names. "filters": [], // db_client: the database client used by the controller. this option must be consistent with // the configuration of the application. "db_client": { //name: Name of the client,鈥榙efault鈥?nbsp;by default "name": "default", //is_fast: "is_fast": false }, // directory: The directory where the controller source files are stored. "directory": "controllers", // generate_base_only: false by default. Set to true to avoid overwriting custom subclasses. "generate_base_only": false } } TestCtrl.cc #include "TestCtrl.h" using namespace web; void TestCtrl::tt(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { Json::Value ret; ret["result"]="ok11111"; ret["token"]=drogon::utils::getUuid(); ret["name"] = "zx"; auto resp=HttpResponse::newHttpJsonResponse(ret); callback(resp); } void TestCtrl::name(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) const { auto clientPtr = drogon::app().getDbClient(); auto f = clientPtr->execSqlAsyncFuture("select * from admin"); auto r = f.get(); // Block until we get the result or catch the exception; std::cout << r.size() << " rows selected!" << std::endl; int i = 0; for (auto row : r) { std::cout << i++ << ": user name is " << row["real_name"].as<std::string>() << std::endl; } Json::Value ret; ret["result"]="ok"; ret["user_name"]="Jack"; ret["gender"]=1; auto resp=HttpResponse::newHttpJsonResponse(ret); callback(resp); } 杩愯 web.exe锛岃闂甴ttp://127.0.0.1:888/name 鍦ㄦ帶鍒跺彴灏辨墦鍗板嚭浣犺緭鍑?std::cout << i++ << ": user name is " << row["real_name"].as<std::string>() << std::endl; drogon_ctl create model <model_path> drogon_ctl create model models 浣犵殑model.json閰嶇疆鐨勬病闂锛屼絾鏄垜鐩墠娴嬭瘯鍙戠幇鐢熸垚鐨勬暟鎹簱琛ㄥ疄浣撲細鏈変簺鐢熸垚鏈変簺鏈夐棶棰橈紝std::string 浣嗘槸鏈変簺浼氱敓鎴?ustd::string锛岀紪璇戝嚭閿?br>
宸茬粡鍙嶉缁欎綔鑰呬簡
娉ㄦ剰鏂扮敓鎴愮殑model鏂囦欢锛岄渶瑕侀噸鏂癱amke锛屽缓璁垹闄uild鏂囦欢澶逛笅鐨勬枃浠跺湪cmake
cmake -DCMAKE_TOOLCHAIN_FILE=D:/cpp/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows -G"Visual Studio 16 2019" ..
以上是关于drogon鍏ラ棬鏁欑▼涓?鎺у埗鍣ㄥ拰鏁版嵁搴撳鎴风浣跨敤的主要内容,如果未能解决你的问题,请参考以下文章