您的位置:首页 > 编程语言 > C语言/C++

一个好用且方便的FastCgi C++库 - FastCgi++

2015-07-31 10:08 405 查看
不知道你是不是曾经发愁过使用FastCgi库来使用C++开发Fastcgi程序繁琐而且会与C++ STL代码产生冲突的地方,或者你还是习惯了cout而不是pringf,那这篇文章就可以了解到一个使用的比较少的FastCgi 的C++库 —— FastCgi++ / FastCgipp。

开发环境:

  OS: Centos 7

  编译器:gcc 4.8.3

准备:

  1. 我的yum源中没有找到Fastcgi++,而且,正如我以往的习惯来说,我还是比较喜欢源码编译 : )

    FastCgi++ 官方介绍: http://www.nongnu.org/fastcgipp/

     下载 源码:最新版本为2.1版本 http://download.savannah.nongnu.org/releases/fastcgipp/fastcgi++-2.1.tar.bz2

  2.  国外大多数开源库中都会使用Boost库,所以,免不了需要安装libboost-devel。参考Mongodb中对Boost库的安装。或者直接yum install boost-devel,也就仅仅是依赖这一个非标准库,所以不需要安装其他。

  3.  由于是FastCgi Application ,选一个一个WebServer来验证他的执行成果。选择Nginx,我的Nginx版本为1.6.0

编译:

  1.  这一段确实也没什么好说的

      tar -xvjf fastcgi++-2.1.tar.bz2

      ./configure --disable-shared --enable-static

      make && make install

  再不指定prefix的路径的情况下,GCC编译也就会到默认的路径去寻找头文件与库文件。这里,我选择编译成了静态库的形式,不需要数据库有关的操作,不需要编译进去.

基本使用:

开始使用:

   

/**
* Fastcgi++ Test by kk
* main.cpp
*/

#include <boost/date_time/posix_time/posix_time.hpp>
#include <fstream>
#include <fastcgi++/request.hpp>
#include <fastcgi++/manager.hpp>
void error_log(const char* msg)
{
using namespace std;
using namespace boost;
static ofstream error;
if(!error.is_open())
{
error.open("/tmp/errlog", ios_base::out | ios_base::app);
error.imbue(locale(error.getloc(), new posix_time::time_facet()));
}
error << '[' << posix_time::second_clock::local_time() << "] " << msg << endl;
}
class HelloWorld: public Fastcgipp::Request<wchar_t>
{
bool response()
{
wchar_t russian[]={ 0x041f, 0x0440, 0x0438, 0x0432, 0x0435, 0x0442, 0x0020, 0x043c, 0x0438, 0x0440, 0x0000 };
wchar_t chinese[]={ 0x4e16, 0x754c, 0x60a8, 0x597d, 0x0000 };
wchar_t greek[]={ 0x0393, 0x03b5, 0x03b9, 0x03b1, 0x0020, 0x03c3, 0x03b1, 0x03c2, 0x0020, 0x03ba, 0x03cc, 0x03c3, 0x03bc, 0x03bf, 0x0000 };
wchar_t japanese[]={ 0x4eca, 0x65e5, 0x306f, 0x4e16, 0x754c, 0x0000 };
wchar_t runic[]={ 0x16ba, 0x16d6, 0x16da, 0x16df, 0x0020, 0x16b9, 0x16df, 0x16c9, 0x16da, 0x16de, 0x0000 };
out << "Content-Type: text/html; charset=utf-8\r\n\r\n";
out << "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
out << "<title>fastcgi++: Hello World in UTF-8</title></head><body>";
out << "English: Hello World<br />";
out << "Russian: " << russian << "<br />";
out << "Greek: " << greek << "<br />";
out << "Chinese: " << chinese << "<br />";
out << "Japanese: " << japanese << "<br />";
out << "Runic English?: " << runic << "<br />";
out << "</body></html>";
err << "Hello apache error log";
return true;
}
};
int main()
{
try
{
Fastcgipp::Manager<HelloWorld> fcgi;
fcgi.handler();
}
catch(std::exception& e)
{
error_log(e.what());
}
}


  使用GCC将其编译:g++ -o main -lboost_system-mt -lboost_thread-mt -lfastcgipp main.cpp

验证:

  由于是一个FastCgi程序,所以就需要一个lighthttpd项目中一个fastcgi启动器spawn-fastcgi(好吧,暂且先启动器这么叫,我想在下面的文章源码具体探索一下spawn-fastcgi的执行过程)

  网上关于spawn-fastcgi的教程很多,编译起来也不难。贴上源码下载路径就好:http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz

  更改Nginx配置文件 nginx.conf 在server中加入fastcgi配置:

  

location ~\.fcgi$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.fcgi;
fastcgi_param SCRIPT_FILENAME /$fastcgi_script_name;
include fastcgi_params;
}


  重启Nginx重新加载配置文件。

  运行spawn-fastcgi程序:  

    spawn-fastcgi -a 127.0.0.1 -C 20 -p 9000 main

  成功提示:spawn-fcgi: child spawned successfully: PID: 13404

  现在打开浏览器,输入localhost/test.fcgi就能看到C++代码输出的值了。

后记:

  以前一直使用的FastCgi库,但是这是一个C语言的库,用C++来开发使用极其不方便,而且会出现一些不知名的错误。FastCgi++使用OOP设计,能完美的使用C++来开发FastCgi程序。在后面的文章会详细的介绍FastCgi++的使用方法。 :) 工作愉快
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: