C ++ web application development
After reading a recent topic about using C ++ and fastcgi, I finally decided to publish my own developments on the topic of Web and C ++.
Existing solutions, from my point of view, implement simple things in a complex way. My goal was to eliminate this annoying misunderstanding by writing a library that allows you to write effective cross-platform web applications in C ++ as easily and quickly as in PHP, Python, Java, etc.
In order not to torment the reader with expectation, let us go straight to the code of the simplest application written using this library ( project page on Google Code , MIT license).
Source Code Main.h
Source Code Main.cpp
- #ifndef _MAIN_H
- #define _MAIN_H
- #include
- class HelloWorld:public IHttpRequestHandler
- {
- private:
- Server server;
- public:
- HelloWorld();
- void Run();
- void Handle(HttpRequest* request,HttpResponse* response);
- };
- #endif
This application does not need a third-party web server at all. It turns out a regular executable file (the size on Windows is 29 Kb), which you can simply take and run. You can see the result of its implementation at the very beginning of the article.
- #include "Main.h"
- HelloWorld::HelloWorld():server(8080,"0.0.0.0")
- {
- server.RegisterHandler(this);
- }
- void HelloWorld::Run()
- {
- server.Run();
- }
- void HelloWorld::Handle(HttpRequest* request,HttpResponse* response)
- {
- response->Write("
Hello, world!
");- }
- int main()
- {
- try
- {
- HelloWorld app;
- app.Run();
- }
- catch(exception& e)
- {
- cout<
- }
- }
The library is cross-platform, it can be used on both Windows and Linux (by the way, are there anyone who wants to port to FreeBSD or MacOS?). Its small size allows web applications to run in embedded systems. For example, on a router (I checked on my home, ASUS WL-500gP V1, with OpenWRT firmware installed).
And if you use some embedded database, for example, sqlite, then you can create an application that is fully executed in one process. And this means that extra time will not be spent on interprocess communication. The benefit is obvious - speed. And given that the program itself is written in C ++, then perhaps this is the key to writing the fastest web applications. :)
And, as a bonus - a ready-to-use application - a file server (you can download it there ):
Goodies:
- Lightweight
- Cross platform
- Very easy setup
- Supports custom styles
- Supports file upload
- Supports unicode in file names
Liked? Join now! :)