1. Add the getCustomCOnfig() method.
2. Add some comments.
This commit is contained in:
parent
56eaca1998
commit
66456eae1a
27
README.md
27
README.md
|
@ -67,21 +67,20 @@ int main()
|
|||
Drogon provides some interfaces for adding controller logic directly in the main() function, for example, user can register a handler like this in Drogon:
|
||||
|
||||
```c++
|
||||
app.registerHttpMethod("/test?username={1}",
|
||||
[](const HttpRequestPtr& req,
|
||||
const std::function<void (const HttpResponsePtr &)> & callback,
|
||||
const std::string &name)
|
||||
{
|
||||
Json::Value json;
|
||||
json["result"]="ok";
|
||||
json["message"]=std::string("hello,")+name;
|
||||
auto resp=HttpResponse::newHttpJsonResponse(json);
|
||||
callback(resp);
|
||||
},
|
||||
{Get,"LoginFilter"});
|
||||
app.registerHandler("/test?username={1}",
|
||||
[](const HttpRequestPtr& req,
|
||||
const std::function<void (const HttpResponsePtr &)> & callback,
|
||||
const std::string &name)
|
||||
{
|
||||
Json::Value json;
|
||||
json["result"]="ok";
|
||||
json["message"]=std::string("hello,")+name;
|
||||
auto resp=HttpResponse::newHttpJsonResponse(json);
|
||||
callback(resp);
|
||||
},
|
||||
{Get,"LoginFilter"});
|
||||
```
|
||||
|
||||
|
||||
|
||||
While such interfaces look intuitive, they are not suitable for complex business logic scenarios. Assuming there are tens or even hundreds of handlers that need to be registered in the framework, isn't it a better practice to implement them separately in their respective classes? So unless your logic is very simple, we don't recommend using above interfaces. Instead, we can create an HttpSimpleController as follows:
|
||||
|
||||
```c++
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* This is a JSON format configuration file
|
||||
*/
|
||||
*/
|
||||
{
|
||||
/*
|
||||
//ssl:The global ssl files setting
|
||||
|
@ -52,23 +52,23 @@
|
|||
}
|
||||
],*/
|
||||
"app": {
|
||||
//threads_num: The number of IO threads,1 by default, if the value is set to 0, the number of threads
|
||||
//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 processors.
|
||||
"threads_num": 1,
|
||||
//enable_session: False by default
|
||||
"enable_session": true,
|
||||
"session_timeout": 0,
|
||||
//document_root: Root path of HTTP document,defaut path is ./
|
||||
//document_root: Root path of HTTP document, defaut path is ./
|
||||
"document_root": "./",
|
||||
//upload_path: The path to save the uploaded file. "uploads" by default.
|
||||
//If the path isn't 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. */
|
||||
* 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",
|
||||
|
@ -119,23 +119,21 @@
|
|||
"use_sendfile": true,
|
||||
//use_gzip: True by default, use gzip to compress the response body's content;
|
||||
"use_gzip": true,
|
||||
//static_files_cache_time: 5 (seconds) by default,the time in which static file response is cached,
|
||||
//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": [
|
||||
"FilterClassName"
|
||||
]
|
||||
}
|
||||
],
|
||||
"simple_controllers_map": [{
|
||||
"path": "/path/name",
|
||||
"controller": "controllerClassName",
|
||||
"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,
|
||||
|
@ -150,5 +148,7 @@
|
|||
//After the maximum number of requests are made, the connection is closed.
|
||||
//The default value of 0 means no limit.
|
||||
"pipeline_requests": 0
|
||||
}
|
||||
},
|
||||
//custom_config: custom configuration for users. This object can be get by the app().getCustomConfig() method.
|
||||
"custom_config": {}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/* This is a JSON format configuration file
|
||||
*/
|
||||
*/
|
||||
{
|
||||
/*
|
||||
//ssl:The global ssl files setting
|
||||
|
@ -52,23 +52,23 @@
|
|||
}
|
||||
],*/
|
||||
"app": {
|
||||
//threads_num: The number of IO threads,1 by default, if the value is set to 0, the number of threads
|
||||
//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 processors.
|
||||
"threads_num": 1,
|
||||
//enable_session: False by default
|
||||
"enable_session": true,
|
||||
"session_timeout": 0,
|
||||
//document_root: Root path of HTTP document,defaut path is ./
|
||||
//document_root: Root path of HTTP document, defaut path is ./
|
||||
"document_root": "./",
|
||||
//upload_path: The path to save the uploaded file. "uploads" by default.
|
||||
//If the path isn't 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. */
|
||||
* 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",
|
||||
|
@ -119,23 +119,21 @@
|
|||
"use_sendfile": true,
|
||||
//use_gzip: True by default, use gzip to compress the response body's content;
|
||||
"use_gzip": true,
|
||||
//static_files_cache_time: 5 (seconds) by default,the time in which static file response is cached,
|
||||
//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": [
|
||||
"FilterClassName"
|
||||
]
|
||||
}
|
||||
],
|
||||
"simple_controllers_map": [{
|
||||
"path": "/path/name",
|
||||
"controller": "controllerClassName",
|
||||
"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,
|
||||
|
@ -150,5 +148,7 @@
|
|||
//After the maximum number of requests are made, the connection is closed.
|
||||
//The default value of 0 means no limit.
|
||||
"pipeline_requests": 0
|
||||
}
|
||||
},
|
||||
//custom_config: custom configuration for users. This object can be get by the app().getCustomConfig() method.
|
||||
"custom_config": {}
|
||||
}
|
|
@ -109,10 +109,10 @@ int main()
|
|||
//app().setThreadNum(4);
|
||||
// trantor::Logger::setLogLevel(trantor::Logger::TRACE);
|
||||
//class function
|
||||
app().registerHttpMethod("/api/v1/handle1/{1}/{2}/?p3={3}&p4={4}", &A::handle);
|
||||
app().registerHttpMethod("/api/v1/handle11/{1}/{2}/?p3={3}&p4={4}", &A::staticHandle);
|
||||
app().registerHandler("/api/v1/handle1/{1}/{2}/?p3={3}&p4={4}", &A::handle);
|
||||
app().registerHandler("/api/v1/handle11/{1}/{2}/?p3={3}&p4={4}", &A::staticHandle);
|
||||
//lambda example
|
||||
app().registerHttpMethod("/api/v1/handle2/{1}/{2}", [](const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, int a, float b) {
|
||||
app().registerHandler("/api/v1/handle2/{1}/{2}", [](const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, int a, float b) {
|
||||
// LOG_DEBUG << "int a=" << a;
|
||||
// LOG_DEBUG << "float b=" << b;
|
||||
HttpViewData data;
|
||||
|
@ -127,18 +127,23 @@ int main()
|
|||
|
||||
B b;
|
||||
//functor example
|
||||
app().registerHttpMethod("/api/v1/handle3/{1}/{2}", b);
|
||||
app().registerHandler("/api/v1/handle3/{1}/{2}", b);
|
||||
|
||||
A tmp;
|
||||
std::function<void(const HttpRequestPtr &, const std::function<void(const HttpResponsePtr &)> &, int, const std::string &, const std::string &, int)>
|
||||
func = std::bind(&A::handle, &tmp, _1, _2, _3, _4, _5, _6);
|
||||
//api example for std::function
|
||||
app().registerHttpMethod("/api/v1/handle4/{4}/{3}/{1}", func);
|
||||
app().registerHandler("/api/v1/handle4/{4}/{3}/{1}", func);
|
||||
|
||||
app().setDocumentRoot("./");
|
||||
app().enableSession(60);
|
||||
//start app framework
|
||||
//drogon::HttpAppFramework::instance().enableDynamicViewsLoading({"/tmp/views"});
|
||||
app().loadConfigFile("config.example.json");
|
||||
auto &json = app().getCustomConfig();
|
||||
if(json.empty())
|
||||
{
|
||||
std::cout << "empty custom config!" << std::endl;
|
||||
}
|
||||
app().run();
|
||||
}
|
||||
|
|
|
@ -58,6 +58,7 @@ inline std::string getGitCommit()
|
|||
class HttpAppFramework : public trantor::NonCopyable
|
||||
{
|
||||
public:
|
||||
virtual ~HttpAppFramework();
|
||||
///Get the instance of HttpAppFramework
|
||||
/**
|
||||
* HttpAppFramework works at singleton mode, so any calling of this
|
||||
|
@ -99,28 +100,54 @@ class HttpAppFramework : public trantor::NonCopyable
|
|||
*/
|
||||
virtual trantor::EventLoop *loop() = 0;
|
||||
|
||||
virtual void setThreadNum(size_t threadNum) = 0;
|
||||
virtual size_t getThreadNum() const = 0;
|
||||
virtual void setSSLFiles(const std::string &certPath,
|
||||
const std::string &keyPath) = 0;
|
||||
virtual void addListener(const std::string &ip,
|
||||
uint16_t port,
|
||||
bool useSSL = false,
|
||||
const std::string &certFile = "",
|
||||
const std::string &keyFile = "") = 0;
|
||||
///Load the configuration file with json format.
|
||||
virtual void loadConfigFile(const std::string &fileName) = 0;
|
||||
|
||||
virtual ~HttpAppFramework();
|
||||
virtual void registerWebSocketController(const std::string &pathName,
|
||||
const std::string &crtlName,
|
||||
const std::vector<std::string> &filters =
|
||||
std::vector<std::string>()) = 0;
|
||||
///Register a HttpSimpleController object into the framework.
|
||||
/**
|
||||
* @param pathName: When the path of a http request is equal to the @param pathName, the asyncHandleHttpRequest() method
|
||||
* of the controller is called.
|
||||
* @param ctrlName is the name of the controller. It includes the namespace to which the controller belongs.
|
||||
* @param filtersAndMethods is a vector containing Http methods or filter name constraints.
|
||||
*
|
||||
* FOR EXAMPLE:
|
||||
* app.registerHttpSimpleController("/userinfo","UserInfoCtrl",{Get,"LoginFilter"});
|
||||
*
|
||||
* NOTE:
|
||||
* Users can perform the same operation through the configuration file or a macro in the header file.
|
||||
*/
|
||||
virtual void registerHttpSimpleController(const std::string &pathName,
|
||||
const std::string &crtlName,
|
||||
const std::string &ctrlName,
|
||||
const std::vector<any> &filtersAndMethods = std::vector<any>()) = 0;
|
||||
|
||||
///Register a handler into the framework.
|
||||
/**
|
||||
* @param pathPattern: When the path of a http request matches the @param pathPattern, the handler indicated by
|
||||
* the @param function is called.
|
||||
* @param function indicates any type of callable object with a valid processing interface.
|
||||
* @param filtersAndMethods is the same as the third parameter in the above method.
|
||||
*
|
||||
* FOR EXAMPLE:
|
||||
* app.registerHandler("/hello?username={1}",
|
||||
[](const HttpRequestPtr& req,
|
||||
const std::function<void (const HttpResponsePtr &)> & callback,
|
||||
const std::string &name)
|
||||
{
|
||||
Json::Value json;
|
||||
json["result"]="ok";
|
||||
json["message"]=std::string("hello,")+name;
|
||||
auto resp=HttpResponse::newHttpJsonResponse(json);
|
||||
callback(resp);
|
||||
},
|
||||
{Get,"LoginFilter"});
|
||||
*
|
||||
* NOTE:
|
||||
* As you can see in the above example, this method supports parameters mapping.
|
||||
*/
|
||||
template <typename FUNCTION>
|
||||
void registerHttpMethod(const std::string &pathPattern,
|
||||
FUNCTION &&function,
|
||||
const std::vector<any> &filtersAndMethods = std::vector<any>())
|
||||
void registerHandler(const std::string &pathPattern,
|
||||
FUNCTION &&function,
|
||||
const std::vector<any> &filtersAndMethods = std::vector<any>())
|
||||
{
|
||||
LOG_TRACE << "pathPattern:" << pathPattern;
|
||||
internal::HttpBinderBasePtr binder;
|
||||
|
@ -151,42 +178,269 @@ class HttpAppFramework : public trantor::NonCopyable
|
|||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
registerHttpController(pathPattern, binder, validMethods, filters);
|
||||
}
|
||||
/// Register a WebSocketController into the framework.
|
||||
/// The parameters of this method are the same as those in the registerHttpSimpleController() method.
|
||||
virtual void registerWebSocketController(const std::string &pathName,
|
||||
const std::string &crtlName,
|
||||
const std::vector<std::string> &filters =
|
||||
std::vector<std::string>()) = 0;
|
||||
|
||||
///Get the custom configuration defined by users in the configuration file.
|
||||
virtual const Json::Value &getCustomConfig() const = 0;
|
||||
|
||||
///Set the number of threads for IO event loops
|
||||
/**
|
||||
* The default value is 1, if @param threadNum is 0, the number is equal to the number of CPU cores.
|
||||
*
|
||||
* NOTE:
|
||||
* This number is usually less than or equal to the number of CPU cores.
|
||||
* This number can be configured in the configuration file.
|
||||
*/
|
||||
virtual void setThreadNum(size_t threadNum) = 0;
|
||||
|
||||
///Get the number of threads for IO event loops
|
||||
virtual size_t getThreadNum() const = 0;
|
||||
|
||||
///Set the global cert file and private key file for https
|
||||
///These options can be configured in the configuration file.
|
||||
virtual void setSSLFiles(const std::string &certPath,
|
||||
const std::string &keyPath) = 0;
|
||||
|
||||
///Add a listener for http or https service
|
||||
/**
|
||||
* @param ip is the ip that the listener listens on.
|
||||
* @param port is the port that the listener listens on.
|
||||
* If @param useSSL is true, the listener is used for the https service.
|
||||
* @param certFile and @param keyFile specify the cert file and the private key file for this listener. If
|
||||
* they are empty, the global configuration set by the above method is used.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void addListener(const std::string &ip,
|
||||
uint16_t port,
|
||||
bool useSSL = false,
|
||||
const std::string &certFile = "",
|
||||
const std::string &keyFile = "") = 0;
|
||||
|
||||
///Enable sessions supporting.
|
||||
/**
|
||||
* Disabled by default.
|
||||
* If there isn't any request from a client for @param timeout(>0) seconds,
|
||||
* the session of the client is destroyed.
|
||||
* If the @param timeout is equal to 0, sessions will remain permanently
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void enableSession(const size_t timeout = 0) = 0;
|
||||
|
||||
///Disable sessions supporting.
|
||||
/**
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void disableSession() = 0;
|
||||
virtual const std::string &getDocumentRoot() const = 0;
|
||||
|
||||
///Set the root path of HTTP document, defaut path is ./
|
||||
/**
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setDocumentRoot(const std::string &rootPath) = 0;
|
||||
virtual const std::string &getUploadPath() const = 0;
|
||||
|
||||
///Get the document root directory.
|
||||
virtual const std::string &getDocumentRoot() const = 0;
|
||||
|
||||
///Set the path to store uploaded files.
|
||||
/**
|
||||
* If the @param uploadPath isn't prefixed with /, ./ or ../, it is relative path of document_root path,
|
||||
* The default value is 'uploads'.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setUploadPath(const std::string &uploadPath) = 0;
|
||||
|
||||
///Get the path to store uploaded files.
|
||||
virtual const std::string &getUploadPath() const = 0;
|
||||
|
||||
///Set types of files that can be downloaded.
|
||||
/**
|
||||
* FOR EXAMPLE:
|
||||
* app.setFileTypes({"html","txt","png","jpg"});
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setFileTypes(const std::vector<std::string> &types) = 0;
|
||||
|
||||
///Enable supporting for dynamic views loading.
|
||||
/**
|
||||
* Disabled by default.
|
||||
* The @param libPaths is a vactor that contains paths to view files.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void enableDynamicViewsLoading(const std::vector<std::string> &libPaths) = 0;
|
||||
|
||||
///Set the maximum number of all connections.
|
||||
/**
|
||||
* The default value is 100000.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setMaxConnectionNum(size_t maxConnections) = 0;
|
||||
|
||||
///Set the maximum number of connections per remote IP.
|
||||
/**
|
||||
* The default value is 0 which means no limit.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setMaxConnectionNumPerIP(size_t maxConnectionsPerIP) = 0;
|
||||
|
||||
virtual void loadConfigFile(const std::string &fileName) = 0;
|
||||
|
||||
///Make the application run as a daemon.
|
||||
/**
|
||||
* Disabled by default.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void enableRunAsDaemon() = 0;
|
||||
|
||||
///Make the application restart after crashing.
|
||||
/**
|
||||
* Disabled by default.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void enableRelaunchOnError() = 0;
|
||||
|
||||
///Set the output path of logs.
|
||||
/**
|
||||
* @param logSize indicates the maximum size of the log file.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setLogPath(const std::string &logPath,
|
||||
const std::string &logfileBaseName = "",
|
||||
size_t logSize = 100000000) = 0;
|
||||
|
||||
///Set the log level
|
||||
/**
|
||||
* The @param level is one of TRACE, DEBUG, INFO, WARN. The Default value is DEBUG.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setLogLevel(trantor::Logger::LogLevel level) = 0;
|
||||
|
||||
///If @param sendFile is true, sendfile() system-call is used to send static files to clients;
|
||||
/**
|
||||
* The default value is true.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
* Even though sendfile() is enabled, only files larger than 200k are sent this way,
|
||||
* because the advantages of sendfile() can only be reflected in sending large files.
|
||||
*/
|
||||
virtual void enableSendfile(bool sendFile) = 0;
|
||||
|
||||
///If @param useGzip is true, use gzip to compress the response body's content;
|
||||
/**
|
||||
* The default value is true.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
* After gzip is enabled, gzip is used under the following conditions:
|
||||
* 1. The content type of response is not a binary type.
|
||||
* 2. The content length is bigger than 1024 bytes.
|
||||
*/
|
||||
virtual void enableGzip(bool useGzip) = 0;
|
||||
|
||||
///Return true if gzip is enabled.
|
||||
virtual bool useGzip() const = 0;
|
||||
|
||||
///Set the time in which the static file response is cached in memory.
|
||||
/**
|
||||
* @param cacheTime: in seconds. 0 means always cached, negative means no cache
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setStaticFilesCacheTime(int cacheTime) = 0;
|
||||
|
||||
///Get the time set by the above method.
|
||||
virtual int staticFilesCacheTime() const = 0;
|
||||
|
||||
///Set the lifetime of the connection without read or write
|
||||
/**
|
||||
* @param timeout: in seconds. 60 by default.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setIdleConnectionTimeout(size_t timeout) = 0;
|
||||
|
||||
///Set the 'server' header field in each response sent by drogon.
|
||||
/**
|
||||
* @param server: empty string by default with which the 'server' header field is
|
||||
* set to "Server: drogon/version string\r\n"
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setServerHeaderField(const std::string &server) = 0;
|
||||
|
||||
///Sets 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 is
|
||||
* 0 which means no limit.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setKeepaliveRequestsNumber(const size_t number) = 0;
|
||||
|
||||
///Sets the maximum number of unhandled requests that can be cached in pipeline buffer.
|
||||
/**
|
||||
* The default value of 0 means no limit.
|
||||
* After the maximum number of requests cached in pipeline buffer are made, the connection is closed.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void setPipelineRequestsNumber(const size_t number) = 0;
|
||||
|
||||
#if USE_ORM
|
||||
///Get a database client by @param name
|
||||
virtual orm::DbClientPtr getDbClient(const std::string &name = "default") = 0;
|
||||
|
||||
///Get a 'fast' database client by @param name
|
||||
virtual orm::DbClientPtr getFastDbClient(const std::string &name = "default") = 0;
|
||||
|
||||
///Create a database client
|
||||
/**
|
||||
* @param dbType: The database type is one of "postgresql","mysql","sqlite3".
|
||||
* @param host: IP or host name.
|
||||
* @param port: The port on which the database server is listening.
|
||||
* @databaseName, @param userName, @param password: ...
|
||||
* @connectionNum: The number of connections to the database server. It's valid only if @param isFast is false.
|
||||
* @filename: The file name of sqlite3 database file.
|
||||
* @name: The client name.
|
||||
* @isFast: Indicates if the client is a fast database client.
|
||||
*
|
||||
* NOTE:
|
||||
* This operation can be performed by an option in the configuration file.
|
||||
*/
|
||||
virtual void createDbClient(const std::string &dbType,
|
||||
const std::string &host,
|
||||
const u_short port,
|
||||
|
|
|
@ -59,11 +59,11 @@ class HttpController : public DrObject<T>
|
|||
path.replace(pos, 2, "/");
|
||||
}
|
||||
if (pattern.empty() || pattern[0] == '/')
|
||||
app().registerHttpMethod(path + pattern,
|
||||
app().registerHandler(path + pattern,
|
||||
std::forward<FUNCTION>(function),
|
||||
filtersAndMethods);
|
||||
else
|
||||
app().registerHttpMethod(path + "/" + pattern,
|
||||
app().registerHandler(path + "/" + pattern,
|
||||
std::forward<FUNCTION>(function),
|
||||
filtersAndMethods);
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ class ConfigLoader : public trantor::NonCopyable
|
|||
public:
|
||||
explicit ConfigLoader(const std::string &configFile);
|
||||
~ConfigLoader();
|
||||
const Json::Value &jsonValue() const { return _configJsonRoot; }
|
||||
void load();
|
||||
|
||||
private:
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include <drogon/CacheMap.h>
|
||||
#include <drogon/Session.h>
|
||||
#include <trantor/utils/AsyncFileLogger.h>
|
||||
#include <json/json.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
@ -149,6 +150,8 @@ void HttpAppFrameworkImpl::loadConfigFile(const std::string &fileName)
|
|||
{
|
||||
ConfigLoader loader(fileName);
|
||||
loader.load();
|
||||
auto &jsonRoot = loader.jsonValue();
|
||||
_customConfig = jsonRoot.get("custom_config", Json::Value());
|
||||
}
|
||||
void HttpAppFrameworkImpl::setLogPath(const std::string &logPath,
|
||||
const std::string &logfileBaseName,
|
||||
|
|
|
@ -46,6 +46,10 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
|||
_connectionNum(0)
|
||||
{
|
||||
}
|
||||
virtual const Json::Value &getCustomConfig() const override
|
||||
{
|
||||
return _customConfig;
|
||||
}
|
||||
virtual void addListener(const std::string &ip,
|
||||
uint16_t port,
|
||||
bool useSSL = false,
|
||||
|
@ -213,6 +217,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework
|
|||
int _staticFilesCacheTime = 5;
|
||||
std::unordered_map<std::string, std::weak_ptr<HttpResponse>> _staticFilesCache;
|
||||
std::mutex _staticFilesCacheMutex;
|
||||
Json::Value _customConfig;
|
||||
#if USE_ORM
|
||||
std::map<std::string, orm::DbClientPtr> _dbClientsMap;
|
||||
struct DbInfo
|
||||
|
|
Loading…
Reference in New Issue