Home  /  RSS  /  RSS Comments  /  Project Site  /  Enter

Tutorial: "Hello World!"

We will learn how write a simple web project that displays page with traditional "Hello World!". We will see how to build the program and setup and run it under two popular web servers: Lighttpd and (TODO) Apache.

Requirement: Install "framework" part of this project first.

The Program Itself

First we need to include two headers of cppcms library:

#include <cppcms/worker_thread.h>
#include <cppcms/manager.h>
#include <iostream>
using namespace std;
using namespace cppcms;

First one (worker_thread.h) defined a base class for our class that implements our web application. The second one (manager.h) defines a run time modules that connect our class to the web server API.

Then we implement our quite simple and useless web application:

class my_hello_world : public worker_thread {
public:
    my_hello_world(manager const &m) :
        worker_thread(m) 
    {
    };
    virtual void main();
};

Notes:

  1. The base class "worker_thread" receives a reference to cppcms::manager object as parameter in its constructor, thus, you should implement a constructor that receives this parameter and passes it to base class.
  2. The virtual member function "main" is actually the function that is called upon page request.
void my_hello_world::main()
{
    out+="<html><body><h1>Hello World</h1></body></html>";
}

This function appends to std::string out a "Hello World" html message.

Rationale: Usually you will use "out" string directly, it is usually used as an output buffer for template engine.

int main(int argc,char ** argv)
{
    try {
        manager app(argc,argv);
        app.set_worker(new simple_factory<my_hello_world>());
        app.execute();
    }
    catch(std::exception const &e) {
        cerr<<e.what()<<endl;
    }
}

We create an object of type cppcms::manager that controls general settings like: web server API, run time mode. cache and more. It parses command line arguments, reads configuration file and defines all required properties.

Then we need to create a factory class that would create objects that would work with the requests. The template cppcms::simple_factory defines such factory for your class my_hello_world. Now we can call the main loop of our application execute()

This framework throws exceptions of type cppcms::cppcms_error that are derived from std::runtime_error and can be catch using std::exception.

Now we can build our application running:

gcc hello.cpp -o hello.fcgi -lcppcms

Configuration File

Every CppCMS based application should come with a configuration file. You can find a configuration file template with full comments in docs folder at your CppCMS installation.

We will write a simple file that consists of few lines:

server.api = "fastcgi"
server.mod = "prefork"

This will define a basic settings for the runtime mode:

  1. API that uses CppCMS in communication with web server, in our case "fastcgi". Other options are "scgi" and "cgi".
  2. Worker mode of CppCMS – prefork – application forks several processes that process queries. Other possible options are single process and thread mode – "process" and single process multiple threads mode "thread".

We would save it as "config.txt"

Web Server Configuration

Lighttpd

Now we need to configure our web server to run our application. There are two major options:

  1. Let lighttpd control processes start up and shout down.
  2. Run the worker process manually and define a socket that is used for communication with web server.

In order to use first scheme we should add to lighttpd configuration file following:

fastcgi.server = ( "/hello" =>
  ( "localhost" => 
     (
      "check-local" => "disable",
      "max-procs" => 1,
      "bin-path" => "/path/to/hello.fcgi -c /path/to/config.txt",
      "socket" => "/tmp/hello-fastcgi.socket" )))

Where /hello is our path to the root of the application on the web site. /path/to/hello.fcgi is the path to the location of fastcgi executable we compiled before. Make sure, Lighty has rights to run it. /path/to/config.txt is the location of our configuration file that defines different CppCMS parameters we had prepared.

If you prefer to use independent start of fastcgi application (that is very useful for debugging), you should define following:

fastcgi.server = ( "/hello" =>
  ( "localhost" => 
     (
      "check-local" => "disable",
      "socket" => "/tmp/hello-fastcgi.socket" )))

And add to configuration file of CppCMS following line:

server.socket="/tmp/hello-fastcgi.socket"

Apache

(TODO)

Testing

Now restart your web server, if you use independent FastCGI application start run ./hello.fcgi -c config.txt and now open web browser and go to http://localhost/hello. You should see "Hello World"

Pages

Categories

Development

Powered By

3rd Party