一個好用且方便的FastCgi C++庫 - FastCgi++


不知道你是不是曾經發愁過使用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++的使用方法。 :)  工作愉快

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM