Main  /  Edit version 7  /  Edit version 8  /   /  Users Area

Difference "Tutorial: Hello World (v 1.x)" ver. 7 versus ver. 8

Content:

Lets write our first CppCMS based application:
First we create a file called hello.cpp and store in
it the following content.
_Includes:_
We should import some definitions for our 1st application:
- `cppcms/application.h` - the class `application` that every CppCMS application is derived from.
- `cppcms/applications_pool.h` - class `applications_pool` - the class that manages user applications - creates new per HTTP request and caches them for future use.
- `cppcms/service.h` - the service - central event loop of the network service.
- `cppcms/http_response.h` the class we write our response to the client with.
So let's include them all.
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <iostream>
Now we define our first application class, that is
derived from `cppcms::application`. It should receive
in its constructor a reference to the central CppCMS service
that runs our application.
class hello : public cppcms::application {
public:
hello(cppcms::service &srv) :
cppcms::application(srv)
{
}
virtual void main(std::string url);
};
At this point, in order to respond to user requests we
just override `cppcms::application::main` member
function that is always called for each request.
In this function we will simply write HTML to the output stream. The output stream can be accessed using the `response` member function of the `application` class that gives us access to the output stream `out()`.
void hello::main(std::string /*url*/)
{
response().out() <<
"<html>\n"
"<body>\n"
" <h1>Hello World</h1>\n"
"</body>\n"
"</html>\n";
}
Now lets setup our main function. First we create the
service class that parses command line parameters and searches for configuration options
int main(int argc,char ** argv)
{
try {
cppcms::service srv(argc,argv);
Then we tell the `applications_pool` class to use our application - `hello` - by providing it a predefined
object factory `cppcms::applications_factory<hello>()`. We then so call mount our application to the pool so it will
be used for request handling.
srv.applications_pool().mount(
cppcms::applications_factory<hello>()
);
And now we run our event loop. The `run()` function
exits when the process receives `SIGINT`, `SIGTERM` or `SIGUSR1` signal.
srv.run();
}
catch(std::exception const &e) {
std::cerr << e.what() << std::endl;
}
}
That's it, our application is ready, now we can compile it as simply as:
c++ hello.cpp -lcppcms -o hello
Now we need to prepare the configuration file `config.js` that uses JSON file format for passing all options:
{
"service" : {
"api" : "http",
"port" : 8080
},
"http" : {
"script_names" : [ "/hello" ]
}
}
We setup our service to listen on port 8080 using HTTP
protocol (it is very useful for debugging our applications).
We also provide the `script_names` parameter that defines
the path to the virtual "CGI Scripts" or in simple words
the path that our application should be called from.
Now we can start the application
./hello -c config.js
Go to our browser and hit <http://localhost:8080/hello>
Lets write our first CppCMS based application:
First we create a file called hello.cpp and store in
it the following content.
_Includes:_
We should import some definitions for our 1st application:
- `cppcms/application.h` - the class `application` that every CppCMS application is derived from.
- `cppcms/applications_pool.h` - class `applications_pool` - the class that manages user applications - creates new per HTTP request and caches them for future use.
- `cppcms/service.h` - the service - central event loop of the network service.
- `cppcms/http_response.h` the class we write our response to the client with.
So let's include them all.
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <iostream>
Now we define our first application class, that is
derived from `cppcms::application`. It should receive
in its constructor a reference to the central CppCMS service
that runs our application.
class hello : public cppcms::application {
public:
hello(cppcms::service &srv) :
cppcms::application(srv)
{
}
virtual void main(std::string url);
};
At this point, in order to respond to user requests we
just override `cppcms::application::main` member
function that is always called for each request.
In this function we will simply write HTML to the output stream. The output stream can be accessed using the `response` member function of the `application` class that gives us access to the output stream `out()`.
void hello::main(std::string /*url*/)
{
response().out() <<
"<html>\n"
"<body>\n"
" <h1>Hello World</h1>\n"
"</body>\n"
"</html>\n";
}
Now lets setup our main function. First we create the
service class that parses command line parameters and searches for configuration options
int main(int argc,char ** argv)
{
try {
cppcms::service srv(argc,argv);
Then we tell the `applications_pool` class to use our application - `hello` - by providing it a predefined
object factory `cppcms::applications_factory<hello>()`. We then so call mount our application to the pool so it will
be used for request handling.
srv.applications_pool().mount(
cppcms::applications_factory<hello>()
);
And now we run our event loop. The `run()` function
exits when the process receives `SIGINT`, `SIGTERM` or `SIGUSR1` signal.
srv.run();
}
catch(std::exception const &e) {
std::cerr << e.what() << std::endl;
}
}
That's it, our application is ready, now we can compile it as simply as:
c++ hello.cpp -lcppcms -o hello
Now we need to prepare the configuration file `config.js` that uses JSON file format for passing all options:
{
"service" : {
"api" : "http",
"port" : 8080
},
"http" : {
"script_names" : [ "/hello" ]
}
}
We setup our service to listen on port 8080 using HTTP
protocol (it is very useful for debugging our applications).
We also provide the `script_names` parameter that defines
the path to the virtual "CGI Scripts" or in simple words
the path that our application should be called from.
Now we can start the application
./hello -c config.js
Go to our browser and hit <http://localhost:8080/hello>
link to see our first CppCMS application working.

Sidebar:

← [Notes for Microsoft Windows users](http://cppcms.com/wikipp/en/page/notes_for_windows_users)
[Starting With Templates](http://cppcms.com/wikipp/en/page/cppcms_1x_tut_hello_templates)→

About

CppCMS is a web development framework for performance demanding applications.

Support This Project

SourceForge.net Logo

Поддержать проект

CppCMS needs You


Navigation

Main Page


Valid CSS | Valid XHTML 1.0