📜 ⬆️ ⬇️

Wt, C ++ Library for Web Application Development

Hello!

It so happened that on Habré there was not a single mention of Wt, C ++ Web Toolkit , except for one comment .

The Wt library may be of interest to those who write in C ++ and want to look towards the Web, but do not want to learn and / or use HTML, CSS, JavaScript, SQL and additional technologies related to web development and work with the database. In this article, my goal was to draw community attention to Wt, and not to highlight all of its features.

If you are eager to see how this works, here is an example showing various widgets: www.webtoolkit.eu/widgets
')
Overview of the library in Russian: www.webtoolkit.eu/wt/ru/features

Wt features

Here I will give some of the features that seem to me the most attractive.

Wt is a cross-platform free supported C ++ library. Releases come out several times a year. Available under the terms of the GNU GPL 2 license or commercial license. Let me remind you that the GNU GPL 2 license does not prohibit you to create an application with a closed code in order to use it personally, in particular for deployment on your server.

Programming using Wt is built around widgets, like Qt. Many things are similar to Qt, which makes Wt easy to learn for those familiar with Qt. Unlike Qt, no code generation occurs - signals based on boost.signals are used, which are plain C ++ code that does not require generation. Widget signals (for example, a button click signal) can be attached to any C ++ functions (and functional objects). Fully asynchronous input / output (based on the boost.asio library) is event driven. There is no constant spawning and termination of threads. For each session on the server, a WApplication object is stored, through which the entire tree of widgets is accessible. Access to session widgets is carried out in a way that is protected from “races”, which makes it possible to securely interact with each other and with the server. This is demonstrated in the chat example . The same example shows the use of events initiated by the server (and not by the client) and the possibility of including the application on other pages or sites as a downloadable script (such as how a google maps widget can be embedded in other sites).

Wt-applications work in main browsers, and if JavaScript is working, an Ajax version is issued, and if it does not work, similar HTML code is a unique feature of Wt, at least among C ++ frameworks . This allows without additional efforts on the part of the developer to create an application that takes advantage of technologies such as Ajax, Comet , WebSocket , but at the same time works if they are not available. In addition, there is the possibility of optimizing the issuance for search robots (this is not about cloaking, but about improvements like the rejection of random object identifiers). Wt can use such HTML5 innovations as URL rewriting in the browser (to create REST applications ), select multiple files, video and audio tags, but if they are not present, workarounds are used whenever possible (for example, sound and video via flash instead of tags video and audio).

It is possible to create web applications connected via http, fastcgi or ISAPI (the latter only for Win32 platforms). What kind of connection method will be used is determined at the layout stage by the choice of the required library. Compiling your code with the library of the built-in http-server, we get a full-fledged Web server, which is convenient to use when debugging, but you can also run on production, although in the latter case you should also consider using other connection methods.

The use of Unicode and localization is built into Wt.

Low resource consumption allows you to run applications on devices with limited resources, for example, on routers.

The library is convenient to use to create web interfaces for existing programs written in C ++.

The library provides a high level of security, in particular, protection against XSS or SQL-injection attacks. For example, the WText widget, which is responsible for displaying text, makes sure that its text does not contain “dangerous” attributes and tags (like a script), and cleans them if necessary. HTTPS support available. There is protection against DDoS attacks.

The library also includes a number of add-ons that are useful in web development , in particular, the drawing system, the diagramming system, the ORM (mapping of C ++ classes on database structures), and the authentication system.

In general, I would like to note the purity and high quality of the library code: it is easy to read not only the documentation, but if necessary, the implementation code. Made in general and flexibly. Nowhere (even when compiling the code associated with the database) code generation does not occur. The part of the library responsible for working with the database can be used (or purchased a license) separately from the other components. This part of the library deserves a separate article.

Wt versions are available in Java and other languages , but here I will limit myself to considering the C ++ version. Version 1.0.0 was published in 2005, so that by its “maturity” Wt can compete with Django and Ruby on Rails. It should be noted that in C ++ there are at least two more active projects: CppCMS and Tntnet . Comparison of opportunities presented in Wikipedia. My choice fell on Wt primarily for the ability to completely abstract from HTML, CSS, JavaScript in favor of one language - C ++, as well as for automatic support for both Ajax browsers and non-Ajax browsers.

Simple application example

An example is taken from here: www.webtoolkit.eu/wt/ru/examples

#include <Wt/WApplication> #include <Wt/WBreak> #include <Wt/WContainerWidget> #include <Wt/WLineEdit> #include <Wt/WPushButton> #include <Wt/WText> using namespace Wt; /* * A simple hello world application class which demonstrates * how to react to events, read input, and give feed-back. */ class HelloApplication : public WApplication { public: HelloApplication(const WEnvironment& env); private: WLineEdit *nameEdit_; WText *greeting_; void greet(); }; /* * The env argument contains information about the new session, and * the initial request. It must be passed to the WApplication * constructor so it is typically also an argument for your custom * application constructor. */ HelloApplication::HelloApplication(const WEnvironment& env) : WApplication(env) { setTitle("Hello world"); // application title root()->addWidget(new WText("Your name, please ? ")); // show some text nameEdit_ = new WLineEdit(root()); // allow text input nameEdit_->setFocus(); // give focus WPushButton *button = new WPushButton("Greet me.", root()); // create a button button->setMargin(5, Left); // add 5 pixels margin root()->addWidget(new WBreak()); // insert a line break greeting_ = new WText(root()); // empty text /* * Connect signals with slots * * - simple Wt-way */ button->clicked().connect(this, &HelloApplication::greet); /* * using an arbitrary function object * (binding values with boost::bind()) */ nameEdit_->enterPressed().connect (boost::bind(&HelloApplication::greet, this)); } void HelloApplication::greet() { /* * Update the text, using text input into the nameEdit_ field. */ greeting_->setText("Hello there, " + nameEdit_->text()); } WApplication *createApplication(const WEnvironment& env) { /* * You could read information from the environment to decide whether * the user has permission to start a new application */ return new HelloApplication(env); } int main(int argc, char **argv) { /* * Your main method may set up some shared resources, but should then * start the server application (FastCGI or httpd) that starts listening * for requests, and handles all of the application life cycles. * * The last argument to WRun specifies the function that will instantiate * new application objects. That function is executed when a new user surfs * to the Wt application, and after the library has negotiated browser * support. The function should return a newly instantiated application * object. */ return WRun(argc, argv, &createApplication); } 


This is an example of a simple application asking for a username. When the user enters the name and presses Enter or the “Greet me.” Button, the application will greet him: “Hello there,% USERNAME%”. I think it makes no sense to parse the code in detail, especially since it has comments.

View a running example here: www.webtoolkit.eu/wt/examples/hello/hello.wt

To compile this example, we need a C ++ compiler and an installed Wt library. The lucky users of Debian and Ubuntu can simply install the witty-dev package.

In Linux, the compilation command looks like this:

 g++ hello.cpp -lwt -lwthttp -lboost_signals -o hello 


Startup command:

 ./hello --docroot . --http-address 127.0.0.1 --http-port 8000 


After that, the application can be opened in the browser at 127.0.0.1:8000

Links

Project website: www.webtoolkit.eu
Blog: www.webtoolkit.eu/wt/blog
Introduction: www.webtoolkit.eu/wt/doc/tutorial/wt.html
Examples: www.webtoolkit.eu/wt/examples
Download: www.webtoolkit.eu/wt/download
Documentation: www.webtoolkit.eu/wt/documentation
Error tracking system: redmine.emweb.be
The project is also presented at github.com/kdeforche/wt

Source: https://habr.com/ru/post/142585/


All Articles