Book Image

D Web Development

By : Kai Nacke
Book Image

D Web Development

By: Kai Nacke

Overview of this book

D is a programming language with C-like syntax and static typing. The vibe.d framework builds on powerful D concepts like template meta-programming and compile-time function execution to provide an easy-to-use environment for web applications. The combination of a feature-rich web programming framework with a language compiling to native code solves two common issues in web development today: it accelerates your development and it results in fast, native web applications. Learning the vibe.d framework before you start your application will help you to choose the right features to reach your goal. This book guides you through all aspects of web development with D and the vibe.d framework. Covering the popular operating systems today, this guide starts with the setup of your development system. From the first Hello World-style application you will move on to building static web pages with templates. The concise treatment of web forms will give you all the details about form handling and web security. Using the abstractions of the web framework you will learn how to easily validate user input. Next, you will add database access to your application, providing persistent storage for your data. Building on this foundation, you will expose your component and integrate other components via REST. Learning about the internals of vibe.d you will be able to use low-level techniques such as raw TCP access. The vibe.d concepts can also be used for GUI clients, which is the next topic that you will learn. vibe.d is supported by an active community, which adds new functionality. This comprehensive guide concludes with an overview of the most useful vibe.d extensions and where to find them. It also shows you how to integrate these extensions in your application. The concepts are always illustrated with source code, giving you an insight into how to apply them in your application.
Table of Contents (17 chapters)
D Web Development
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Installing the D compiler and the DUB package manager


The vibe.d framework is written in the D programming language. To get started, you need a D compiler and the DUB package manager. You also need to install openssl and libevent, which are used by vibe.d.

There are three major D compilers available, as follows:

You can use any of these compilers to develop with the vibe.d framework. However, the installation instructions are very different. In this book, only the DMD compiler is used.

A recommended setup is to use the DMD compiler during development and one of the other compilers to produce the final highly optimized binary.

Ubuntu and Debian

The easiest way to install DMD on Ubuntu and Debian is to use the D APT repository provided by Jordi Sayol. The website of this project is located at http://d-apt.sourceforge.net/. The steps to install DMD and DUB are as follows:

  1. Open a terminal and type the following in order to add the repository sources:

    $ sudo wget http://master.dl.sourceforge.net/project/d-apt/files/d-apt.list -O /etc/apt/sources.list.d/d-apt.list
    
  2. Then, you need to update the list of packages:

    $ sudo apt-get update
    

    This command complains that the public key for the d-apt repository could not be verified. The displayed fingerprint is EBCF975E5BA24D5E. Allow an unauthenticated install of d-apt and update the list of packages again:

    $ sudo apt-get -y --allow-unauthenticated install --reinstall d-apt-keyring
    $ sudo apt-get update
    
  3. Now you are ready to install the compiler and the package manager:

    $ sudo apt-get install -y dmd-bin
    $ sudo apt-get install -y dub
    
  4. Lastly, you need to install openssl and libevent:

    $ sudo apt-get install -y libssl-dev libevent-dev
    

Fedora

On Fedora, you use the pre-built compiler binary from the dlang.org website. There is no package for the DUB package manager therefore, you must built this tool from the source. Refer to http://dlang.org/download.html and download the Fedora package of the DMD compiler. The available version at the time of writing this book is 2.068.2. You may need to change the version number when a new version of the compiler is released. The instructions are given for Fedora 22 or later. If you use Fedora 21 or earlier, then replace the dnf command with yum. The steps to install the DMD complier are as follows:

  1. To install dmd, type in a terminal:

    $ sudo dnf install –y dmd-2.068.2-0.fedora.x86_64.rpm
    
  2. Install the required openssl and libevent libraries. You also need to install the development package of libcurl that is used to compile dub:

    $ sudo dnf install –y openssl-devel libevent-devel libcurl-devel
    
  3. After that, go to http://code.dlang.org/download and download source tarball of DUB. The current version number is 0.9.24. Again, you need to change the version number if a newer version is available. To compile and install, just type the following:

    $ tar xvzf dub-0.9.24.tar.gz
    $ cd dub-0.9.24
    $ ./build.sh
    $ sudo cp bin/dub /usr/local/bin
    

Tip

If you get an error message that you are not in the list of sudoers, then you need to change the type of your user to administrator. Open system settings and click on Users. Click on the Unlock button to unlock the window and change the account type to Administrator. Now you can use the sudo command.

OS X

To install the D compiler and the DUB package manager, you can use the Homebrew package manager (http://brew.sh/):

  1. Open a terminal and first update the formulae:

    $ brew update
    
  2. Now install the D compiler:

    $ brew install dmd
    
  3. Next, you will install the DUB package manger:

    $ brew install dub
    
  4. Lastly, you need to install openssl and libevent:

    $ brew install openssl libevent
    

Windows

On Windows, you will simply use the pre-built binaries from the dlang.org website. Go to http://dlang.org/download.html and download the Windows EXE file of the DMD compiler. Double-click on the downloaded file in order to start the installer and then follow the instructions on the screen. The D compiler is now ready to use.

DUB is installed in a similar way. Go to http://code.dlang.org/download and download the installer for Windows. Double-click on the downloaded file to start the installer and then follow the instructions on the screen as done previously.

Building from source

You can also build the applications from source. For some systems, (for example, FreeBSD, Solaris x86, and Linux on non-Intel platforms) this is the only way to install the software. The following are the instructions for a Portable Operating System Interface (POSIX) for a Unix system.

At the time when this book was written, the compiler itself was translated from C++ to D. Version 2.068 is the first one that requires a D compiler for bootstrap. Therefore, you need to install version 2.067.1 before you can install the current version of the compiler.

At build time, you need the GNU C/C++ compiler (gcc/g++) and GNU make (gmake). Using another compiler or make tool may work but is not well tested. For Phobos (the D standard library), you need to have libcurl installed. If your distribution does not provide libcurl, then you can find the source and install the instructions at the http://curl.haxx.se/libcurl/ website.

The easiest way to build DMD from source is using the GitHub repository. First, you build version 2.067.1. This creates a D compiler that you will then use to compile the final version that you want to install, as follows:

  1. Change to your working directory. Then, check the sources from GitHub:

    $ for i in dmd druntime phobos; do
        git clone https://github.com/D-Programming-Language/$i.git;
      done
    
  2. Now, check out version 2.067.1, as follows:

    $ for i in dmd druntime phobos; do
        (cd $i && git checkout v2.067.1);
      done
    
  3. Compile the D compiler and install it to /tmp/dmd as shown in the following code. The CPU model (32 bit or 64 bit) is automatically determined by the makefile. If this fails, you can add MODEL=32 (for 32 bit) or MODEL=64 (for 64 bit) to the make command:

    $ for i in dmd druntime phobos; do
        make -C $i -f posix.mak install INSTALL_DIR=/tmp/dmd
      done
    
  4. Now clean the directories:

    $ for i in dmd druntime phobos; do
        make -C $i -f posix.mak clean
      done
    
  5. Next, check out the version of the compiler that you prefer using. Currently, it is 2.068.2:

    $ for i in dmd druntime phobos; do
        git checkout v2.068.2
      done
    
  6. Then, you can compile and install the version of the compiler. You need to specify the previously installed compiler. The binary is found in sub-folders specifying the operating system and CPU model, for example, linux/bin64 or freebsd/bin32:

    $ make -C dmd -f posix.mak HOST_DC=/tmp/dmd/linux/bin64/dmd
      install INSTALL_DIR=~/dmdss
    $ make -C druntime -f posix.mak install INSTALL_DIR=~/dmd
    $ make -C phobos -f posix.mak install INSTALL_DIR=~/dmd
    
  7. The last step is to add the compiler to your PATH environment variable. The syntax depends on the shell you use. For bash, you run:

    $ export PATH=~/dmd:$PATH
    

Tip

Building the DMD compiler on Windows differs from these generic instructions as you need the Digital Mars C/C++ compiler. I recommend using the binary download for Windows. You can find detailed build instructions for all supported systems in the wiki at http://wiki.dlang.org/Building_DMD.

The DUB package manager is written in D. Go to http://code.dlang.org/download and download the source code archive. Under the file, change the current directory of your shell to the dub-0.9.24 folder and build the application with the following commands. You may need to replace the current version number 0.9.24 with the version you downloaded.

$ tar xvzf dub-0.9.24.tar.gz
$ cd dub-0.9.24
$ ./build.sh

You'll find the binary in the bin folder. You can add this folder to your PATH variable or copy the file to a standard folder, for example, /usr/local/bin:

$ cp bin/dub /usr/local/bin

Verifying your environment

Before you start coding, it is a good idea to check whether the tools work as expected. Run the following command:

$ dmd

You should see the help text provided by the compiler. The version number should be the same as the version you have downloaded.

On Linux and other POSIX systems, you can also check whether the additional libraries are installed. Run the following command:

$ touch empty.d && dmd -main empty.d -L-lssl -L-levent && ./empty

It should produce no error messages.

An error, most likely, indicates that the libevent library or the OpenSSL library is not installed on your system. You should check the install instructions that are mentioned previously for common systems. If your system is not mentioned, then you should consult the manual of your OS to know how to install additional software. If your OS does not provide these libraries, then you have to install them from source. You can find install instructions and the source archive for libevent at http://libevent.org/. The https://openssl.org/ OpenSSL site provides install instructions and the source archive. You can check your DUB installation simply by running the following command:

$ dub help

This prints the help text from DUB. The version number should be the same as the version that you have downloaded.