Login Register

Tutorial Setting up Wt - creating C++ websites filter_list
Setting up Wt - creating C++ websites #1
Ok, so if you didn't know already, there is a framework called Wt (Webtoolkit), which allows you to make full web applications using C++, it's intuitively object oriented, robust, and much faster than PHP. The creators of Wt seem to prefer that each application be a standalone web server, though they do support using other servers, their documentation for it is not good (at all). So, I'm going to walk you through it.

First off, go ahead and set up your debian or ubuntu server (using those for simplicity, its a PITA with arch).

You're going to want to install apache2, suitable compilers, cmake, make, the general stuff. Make sure you install mod_fcgid and enable it!

Ok, so go ahead and clone down the repository
This repo makes it easy to get all the sample code, I built mine from scratch, but you don't have to. Odds are your OS has these packages already:
[Image: 52ugGMl.png]

So, you can either build from scratch (cmake ,,;make;make install) or install these packages:

Ok, so now let's go ahead and write our first app

#include <Wt/WApplication.h>
#include <Wt/WBreak.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h>
#include <Wt/WText.h>

class HelloApplication : public Wt::WApplication
    HelloApplication(const Wt::WEnvironment& env);
    Wt::WLineEdit *nameEdit_;
    Wt::WText *path_;

HelloApplication::HelloApplication(const Wt::WEnvironment& env) : Wt::WApplication(env)
    setTitle("Hello Test2");
    root()->addWidget(std::make_unique<Wt::WText>("Your name, please? ")); // adds the label
    nameEdit_ = root()->addWidget(std::make_unique<Wt::WLineEdit>()); // the text box
    Wt::WPushButton *button = root()->addWidget(std::make_unique<Wt::WPushButton>("Greet")); // submit button
    root()->addWidget(std::make_unique<Wt::WBreak>()); // line break
    path_ = root()->addWidget(std::make_unique<Wt::WText>()); // the internal path label
    path_->setText("Internal Path: " + internalPath()); // set the label for path
    root()->addWidget(std::make_unique<Wt::WBreak>()); // line break
        auto greeting_ = root()->addWidget(std::make_unique<Wt::WText>());
        greeting_->setText("Hello, " + nameEdit_->text());
    }); // connect button click -> that lambda

int main(int argc, char **argv)
    return Wt::WRun(argc, argv, [](const Wt::WEnvironment& env) { return std::make_unique<HelloApplication>(env); });

Let's save this as hello.cc, and compile it
g++ -o hello.wt hello.c -I/usr/local/include -L/usr/local/lib -lwt -lwtfcgi

It should work just fine. Ok, so let's set up a directory for our app:
mkdir /var/www/html/cpp
chown www-data:www-data /var/www/html/cpp

Cool. Now, copy in that file
[Image: UMgyH0F.png]

So, if we try and run this, it just downloads it, that's not what we want. We need to edit a config file at /etc/apache2/mods-enabled/fcgid.conf
[Image: 8BYczMz.png]
<IfModule mod_fcgid.c>
 AddHandler fcgid-script .wt
 SocketPath /var/lib/apache2/fcgid/sock
 IdleTimeout -1
 ProcessLifeTime -1
 MaxProcessCount 10
 DefaultMaxClassProcessCount 10
 DefaultMinClassProcessCount 1

Go ahead and save that, and restart apache
[Image: tU7ZdAe.png]

Ok, that's better, it means it recognized it as CGI, but refused to execute it. We need to make another file: /etc/apache2/sites-available/cpp.conf
[Image: RtAxg3r.png]
<Directory /var/www/html/cpp/>
 #Order Deny,Allow
 Allow from all
 # Enable CGIs to be executed
 Options +ExecCGI +Indexes

Ok, now go ahead and
a2ensite cpp
systemctl restart apache2

Now, refresh your page
[Image: gYy87zq.png]
Cool! it works, you can type whatever into the box and it will greet you, but also take a look at that "Internal Path" line. Go ahead and navigate to hello.wt/whatever
[Image: 01V7Bbj.png]
Wt has the very useful feature of using internal paths, so you can use this to create dynamic web pages on the fly, pretending to have files backing each page, when they aren't. But, right now, it looks a little weird, having that ,wt extension doesn't it? Let's go ahead and just remove that.
mv /var/www/html/cpp/hello.wt /var/www/html/cpp/hello

And, of course drop the .wt from the end
[Image: PMqYDFs.png]
Well that's weird....ok, so drop the /whatever? Oh...that just downloaded the file. Ok, so we aren't detecting that it needs to be executed. That's an easy fix
Go ahead and open our cpp.conf file again, and just before the close directory tag, add the following:
SetHandler fcgid-script
[Image: 4MFngtf.png]

Go ahead and reload apache again
[Image: 3CjxbJ9.png]

And there you have it! Those mystical extensionless web pages that seem to have folders all over the place, all written in C++. Happy developing!
(This post was last modified: 02-27-2018, 07:58 PM by phyrrus9.)


Users browsing this thread: 1 Guest(s)