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
    1. #ifndef _MAIN_H
    2. #define _MAIN_H
    4. #include
    6. class HelloWorld:public IHttpRequestHandler
    7. {
    8. private:
    9.         Server server;
    10. public:
    11.         HelloWorld();
    12.         void Run();
    13.         void Handle(HttpRequest* request,HttpResponse* response);
    14. };
    16. #endif

    Source Code Main.cpp
    1. #include "Main.h"
    3. HelloWorld::HelloWorld():server(8080,"")
    4. {
    5.         server.RegisterHandler(this);
    6. }
    8. void HelloWorld::Run()
    9. {
    10.         server.Run();
    11. }
    13. void HelloWorld::Handle(HttpRequest* request,HttpResponse* response)
    14. {
    15.         response->Write("

      Hello, world!

    16. }
    18. int main()
    19. {
    20.         try
    21.         {
    22.                 HelloWorld app;
    23.                 app.Run();
    24.         }
    25.         catch(exception& e)
    26.         {
    27.                 cout<
    28.         }
    29. }

    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.

    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 ):

    • Lightweight
    • Cross platform
    • Very easy setup
    • Supports custom styles
    • Supports file upload
    • Supports unicode in file names

    Liked? Join now! :)

    Also popular now: