fastcgi++应用初探

来源:互联网 发布:北京凶宅数据库名单 编辑:程序博客网 时间:2024/06/04 19:51

fastcgi++作为一个完全C++编写的fastcgi应用开发包,封装了很多功能,比如参数提取,session,mysql数据库连接管理等最大限度的简化cgi编程。
编写一个简单的helloworld的fastcgi应用。

  1. #include <fstream>
  2. #include <boost/date_time/posix_time/posix_time.hpp>
  3. #include <fastcgi++/request.hpp>
  4. #include <fastcgi++/manager.hpp>
  5. void error_log(constchar* msg)
  6. {
  7.         using namespace std;
  8.         using namespace boost;
  9.         static ofstream error;
  10.         if(!error.is_open())
  11.         {
  12.                 error.open("/tmp/errlog", ios_base::out | ios_base::app);
  13.                 error.imbue(locale(error.getloc(), new posix_time::time_facet()));
  14.         }
  15.         error << '[' << posix_time::second_clock::local_time() <<"] " << msg << endl;
  16. }
  17. class Helloworld: public Fastcgipp::Request<char>{
  18. public:
  19.         bool response()
  20.         {
  21.                 out << "<html><body>";
  22.                 out << "Hello world!";
  23.                 out << "</body></html>";
  24.                 return true;
  25.         } 
  26. }
  27. main(){
  28.         try
  29.         {
  30.                 Fastcgipp::Manager<Helloworld> fcgi;
  31.                 fcgi.handler();
  32.         }
  33.         catch(std::exception& e)
  34.         {
  35.                 error_log(e.what());
  36.         }
  37. }

可以看到基本的只要从 Fastcgipp::Request派生一个类,实现其中的 bool response() 函数就可以完成一个fastcgi 应用。

0 0
原创粉丝点击