Drogon

【Drogon】框架是如何帮我们读取配置文件的


本篇文章简单解读一下drogon的默认配置文件及其加载过程。

使用配置文件

我们可以使用如下命令创建一个新的项目:

  1. drogon_ctl create project xxx

默认的main.cc文件内只是监听了一个80端口号,并在主函数的最后使用app().run();运行起整个框架。

  1. #include <drogon/drogon.h>
  2. int main() {
  3. //Set HTTP listener address and port
  4. drogon::app().addListener("0.0.0.0",80);
  5. //Load config file
  6. //drogon::app().loadConfigFile("../config.json");
  7. //Run HTTP framework,the method will block in the internal event loop
  8. drogon::app().run();
  9. return 0;
  10. }

main.cc文件做出如下修改:

  1. #include <drogon/drogon.h>
  2. int main() {
  3. drogon::app()
  4. .loadConfigFile("../config.json")
  5. .run();
  6. return 0;
  7. }

从官方的wiki文档以及生成的默认代码的注释中我们都可以得知app().loadConfigFile()函数可以加载一个json格式的文件并进行相应的配置。同时修改后的代码也使用了链式调用,这一点并不重要,只是让代码看起来更简洁一些。

app()的调用链如下:app()HttpAppFramework::instance()HttpAppFrameworkImpl::instance()。其中HttpAppFramework类是一个抽象类,Impl是它的一个子类。最终会返回给我们一个Impl类的实例。

加载配置文件

loadConfigFile的实现如下:

  1. HttpAppFramework &HttpAppFrameworkImpl::loadConfigFile(
  2. const std::string &fileName)
  3. {
  4. ConfigLoader loader(fileName);//构造函数会读取文件内容并存储到成员变量中
  5. loader.load();//重头戏
  6. jsonConfig_ = loader.jsonValue();//框架内缓存一下文件内容,后续如getCustomConfig, run等函数会读取这里的值
  7. return *this;
  8. }

ConfigLoader的构造函数:

  1. ConfigLoader::ConfigLoader(const std::string &configFile)
  2. {
  3. //判断文件是否存在……
  4. //判断是否有读取文件的权限……
  5. configFile_ = configFile;//文件名存到成员变量中
  6. std::ifstream infile(drogon::utils::toNativePath(configFile).c_str(),
  7. std::ifstream::in);
  8. if (infile)
  9. {
  10. //这里省略掉try-catch块
  11. infile >> configJsonRoot_;//文件内容按照JsonValue的格式存储到成员变量中
  12. //如果有错误,会自动输出错误位置和可能的错误原因并结束程序的运行
  13. }
  14. }

ConfigLoader::load()函数内容如下:

  1. void ConfigLoader::load()
  2. {
  3. // std::cout<<configJsonRoot_<<std::endl;
  4. loadApp(configJsonRoot_["app"]);
  5. loadSSL(configJsonRoot_["ssl"]);
  6. loadListeners(configJsonRoot_["listeners"]);
  7. loadDbClients(configJsonRoot_["db_clients"]);
  8. loadRedisClients(configJsonRoot_["redis_clients"]);
  9. }

其中的每一个函数都会从构造函数里读取完的内容取出一条子项并进行逐条分析,其中的重头戏是loadApp

  1. static void loadApp(const Json::Value &app)
  2. {
  3. // ...
  4. // session
  5. auto enableSession = app.get("enable_session", false).asBool();
  6. auto timeout = app.get("session_timeout", 0).asUInt64();
  7. if (enableSession)
  8. drogon::app().enableSession(timeout);
  9. else
  10. drogon::app().disableSession();
  11. //...
  12. // document root
  13. auto documentRoot = app.get("document_root", "").asString();
  14. if (documentRoot != "")
  15. {
  16. drogon::app().setDocumentRoot(documentRoot);
  17. }
  18. //...
  19. }

loadApp里读取了43条app的子项(默认生成的config.json文件中只有39条),调用了35个不同的配置函数完成了自动配置。

比如enable_session以及session_timeout两条配置项决定调用enableSession还是disableSession函数,以及在调用enableSession的同时设置过期时间。

app().setDocumentRoot()函数为例:

  1. HttpAppFramework &setDocumentRoot(const std::string &rootPath) override
  2. {
  3. rootPath_ = rootPath;
  4. return *this;
  5. }

它做的事情很简单,就是把传进来的参数存储到了成员变量中。

后续在调用app().run()的时候会读取这些成员变量的值来决定具体的操作:

比如在run()→setupFileLogger()函数中,就会读取logfileBaseName_成员变量。

这一个成员变量是在app().setLogPath()函数中设置的,我们当然可以直接调用这个函数来进行设置,但是在ConfigLoader::loadLogSetting()函数中会自动帮我们调用,而ConfigLoader::loadapp()在调用这个函数时会自动传递进来app["log"]

所以我们既可以直接调用app().setLogPath(),又可以通过在配置文件中编写app.log,来实现设置日志文件的存储位置。

源码之前,了无秘密。

而drogon源码的阅读,loadConfigFile()是一个不错的入手点。

评论

{{ comment.user.username }}
{{ comment.content }}
{{ subComment.user.username }}
{{ subComment.content }}