Cppcms

From campisano.org
Jump to navigation Jump to search

Environment

# cat /etc/debian_version 
6.0.6
# gcc -v
gcc version 4.4.5 (Debian 4.4.5-8) 
# uname -r
3.2.0-0.bpo.3-amd64


Build and install

CppCMS

apt-get install g++ cmake python zlib1g-dev libpcre3-dev libicu-dev libgcrypt11-dev
mkdir /usr/local/cppcms
chown nobody:nogroup /usr/local/cppcms
su -s /bin/bash nobody
cd /tmp
wget "http://ufpr.dl.sourceforge.net/project/cppcms/cppcms/1.0.2/cppcms-1.0.2.tar.bz2"
tar -xjf cppcms-1.0.2.tar.bz2
cd cppcms-1.0.2
mkdir build
cd build/
cmake ../ -DCMAKE_INSTALL_PREFIX=/usr/local/cppcms
make
make test
make install
exit


CppDB

mkdir /usr/local/cppdb
chown nobody:nogroup /usr/local/cppdb
su -s /bin/bash nobody
cd /tmp
wget -c http://ufpr.dl.sourceforge.net/project/cppcms/cppdb/0.3.1/cppdb-0.3.1.tar.bz2
tar -xjf cppdb-0.3.1.tar.bz2
cd cppdb-0.3.1
mkdir build
cd build/
cmake ../ -DCMAKE_INSTALL_PREFIX=/usr/local/cppdb
make
make install
exit


Prepare your HTTP server

Apache

Depends

# deb http://ftp.debian.org/debian stable main contrib non-free
apt-get install libapache2-mod-fastcgi
a2enmod fastcgi

Apache config

from http://whocares.de/fastcgiexternalserver-demystified/all/1/

<IfModule mod_fastcgi.c>
    FastCGIExternalServer /tmp/my_app.fastcgi -host 127.0.0.1:9000
    Alias /my_app /tmp/my_app.fastcgi
</IfModule>


Nginx

Add the follow lines on your nginx.config in the section server:

        location /my_app {

            # We should manually create PATH_INFO
            # variable using URL rewriting
            set $path_info "";
            if ( $fastcgi_script_name ~ ^/my_app(.*)$ ) {
                set $path_info $1;
            }

            fastcgi_param  PATH_INFO $path_info;

            fastcgi_pass   127.0.0.1:9000;
            include        fastcgi_params;
        }


First CppCms app

#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <iostream>

class my_hello_world : public cppcms::application {
public:
    my_hello_world(cppcms::service &srv) :
        cppcms::application(srv) 
    {
    }
    virtual void main(std::string url);
};

void my_hello_world::main(std::string /*url*/)
{
    response().out()<<
        "<html>\n"
        "<body>\n"
        "  <h1>Hello World</h1>\n"
        "</body>\n"
        "</html>\n";
}

int main(int argc,char ** argv)
{
    try {
        cppcms::service srv(argc,argv);
        srv.applications_pool().mount(cppcms::applications_factory<my_hello_world>());
        srv.run();
    }
    catch(std::exception const &e) {
        std::cerr<<e.what()<<std::endl;
    }
}
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4


Compile and Run

The examples are in the source path:

g++ -Wall -I /usr/local/cppcms/include/ my_app.cpp -o my_app -L /usr/local/cppcms/lib -lcppcms -lbooster
cat > config.js << EOF
{
	"service" : {
		"api" : "fastcgi",
		"port" : 9000
	},
	"http" : {
		"script" : "/my_app"
	},
}
EOF
LD_LIBRARY_PATH=/usr/local/cppcms/lib/ ./my_app -c config.js

See the result on http://localhost/my_app


Set-up an Eclipse CDT project =

Create an Eclipse project, maybe like there


Configure Index

Project -> Properties
C/C++ General -> Paths and Symbols
switch to tab 'Includes'
select 'GNU C++' an click 'Add...'
Directory: "/usr/local/cppcms/include"
click 'OK'
switch to tab 'Library Paths'
click 'Add...'
Directory: "/usr/local/cppcms/lib"
click 'OK'
click 'OK' again


Configure to Run and Debug

Run -> Debug Configurations
select 'C/C++ Application'
click on 'New launch configuration'
Name: "TestCppCms Debug"
C/C++ Application: "TestCppCms/build/debug/TestCppCms"
to tab 'Arguments'
Program arguments: "-c config.js"
Working directory: "${workspace_loc:TestCppCms/TestCppCms}"
to tab 'Environment'
click New...
Name: "LD_LIBRARY_PATH"
"/usr/local/cppcms/lib/"
click Ok
click on Close


Now you can see the debug working, and run the page on http://localhost/my_app


References