31,19 €
Leverage the power of D and the vibe.d framework to develop web applications that are incredibly fast
Whether you are new to the world of D, or already have developed applications in D, or if you want to leverage the power of D for web development, then this book is ideal for you. Basic knowledge of core web technologies like HTML 5 is helpful but not required. This book explains the difficult details to speed your web development.
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.
A tutorial-style guide to develop web applications with D and the vibe.d framework. Each topic is explained in detail and illustrated with source code, providing you with hands-on assistance for your application.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 216
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2016
Production reference: 1250116
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-889-0
www.packtpub.com
Author
Kai Nacke
Reviewers
Orfeo Da Vià
Stephan Dilly
Paul Féraud
Kazuki Komatsu
Adam D. Ruppe
Robert "burner" Schadek
Acquisition Editor
Tushar Gupta
Content Development Editor
Merwyn D'souza
Technical Editor
Pranil Pathare
Copy Editor
Tasneem Fatehi
Project Coordinator
Neha Bhatnagar
Proofreader
Safis Editing
Indexer
Mariammal Chettiyar
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
As a general-purpose language, D has held good potential of being applied in the burgeoning web server domain. D's build speed makes its convenience close to that of scripting languages—the argument goes—and there's also a lot to like about the running speed of the resulting native code, too.
This has remained a theoretical possibility for a good while, until vibe.d came out of nowhere to take the D community by storm. The vibe.d framework is everything that I'd hoped it to be—a comprehensive, compelling, modern framework that wonderfully uses D's features to strike a balance between flexibility, performance, and ease of use.
It is everything I hoped... except for one thing. It doesn't have a good book teaching it properly. Therefore, it's easy to imagine my giddiness now that I was offered the honor to write this foreword for such a book.
Written by Kai Nacke, a long-standing and respected luminary of the D community (known among other things for LDC, the LLVM-based D compiler), D Web Development does an admirable job of taking its reader from not knowing much about web development (as I confess your truly is, or at least was) to getting a high-performance server up and running. Also, customizing it in so many ways: content, localization, data connectivity, interoperation, and defining extensions.
Since its creation, vibe.d has slowly but surely become one of the most important frameworks written in D and simultaneously one of the best examples of using D on large scale, so much so that vibe.d is being made part of the reference D distribution. This book is a necessary and welcome term of that equation.
Andrei Alexandrescu
Co-developer of the D programming language
Kai Nacke is a professional IT architect living in Düsseldorf, Germany. He holds a diploma in computer science from the University of Dortmund. His diploma thesis about universal hash functions was recognized as the best of the semester. He has been with IBM for more than 15 years, and has great experience in the development and architecture of business and enterprise applications.
Fascinated by the first home computer, he learned to program a VIC-20 in BASIC. Later, he turned to Turbo PASCAL and Small C on CP/M. Experimenting with the source of Small C created his interest in compiler technology. Many computers, operating systems, and languages followed these first steps.
Around 2005, he became interested in the D programming language and created the first fun applications in D. Missing a 64-bit D compiler for Windows, he started to contribute to the LLVM compiler framework and LDC, the LLVM-based D compiler. Soon, he became committer of both projects and is now the current maintainer of LDC.
He is also a speaker at the Free and Open Source Software Developers' European Meeting (FOSDEM) and was one of the reviewers of D Cookbook, Packt Publishing.
Writing a book is challenging. I would like to thank everybody who supported me by answering questions, accepting pull requests, or by simply encouraging me to go on. I would also like to thank the reviewers who did a great job. Their comments really contributed to the quality of the book.
Orfeo Da Vià is an Italian software developer and has been professionally developing software since 1994. Over the past four years, he has written a number of D software applications.
Orfeo is currently employed as a senior developer at Microline.
Outside the software world, Orfeo enjoys spending time with his two daughters, Raffaella and Adele, and his wife, Alessandra.
Stephan Dilly works as the head of front-end engineering at InnoGames in Germany. In the nine years of professional software development, he has worked in the games industry for Funatics and Ubisoft Blue Byte. He has also worked as a software consultant at Sopra Steria Consulting. The D programming language has been his language of choice for his spare-time projects since 2006. In 2014, Stephan was a speaker at the DConf in San Francisco, where he talked about a backend server architecture developed in D.
Paul Féraud is software engineer with passion for math, algorithms, and programming. He holds a diplôme d'Ingénieur in mechanical engineering from the École Centrale de Nantes in France and a master's degree in software engineering from Keio University in Japan.
Paul has worked for Amadeus, developing a business rule engine that is designed for very high throughput. He has also worked for Dassault Systèmes, developing the architecture backing SIMULIA's finite element simulation systems.
In parallel to this, Paul took great interest in the D programming language and began contributing to its development. He became a member of the core development team and has participated in the design and implementation of its standard library.
Paul now works for Google in Switzerland. He spends most of his time raising his beautiful daughter with his loving and supporting wife.
Kazuki Komatsu is a university student, currently majoring in wireless communication engineering. He started learning D programming language at the age of 16. He has been writing D grammar documents in Japanese and creating a variety of D libraries, such as linear algebra, compile-time meta programming, and Twitter client. Recently, Kazuki has been creating GUI toolkit, awebview, which is similar to GitHub's Electron; however, awebview is written with D and we can write GUI apps with D, HTML, CSS, and JavaScript.
Adam D. Ruppe is the author of D Cookbook, Packt Publishing and a long-time contributor to the D ecosystem.
Robert "burner" Schadek is a regular contributor to the standard D library, Phobos. His D journey started when he used D to create a Distributed Multithreaded Caching D compiler for his computer science master's thesis. He presented this work at DConf 2013. His commitment to Phobos can be seen all over the library. His biggest contribution to Phobos is the experimental logging framework. He is currently working on his PhD in computer science at the University of Oldenburg, Germany. There, he uses the high-performance computing (HPC) facility of the university and a lot of C++ to crunch the numbers on his original data-replication protocol. However, during his programming, he has learned one thing to be true—every untested function is buggy.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
In the cloud age, web technologies are more important than ever. The vibe.d framework enables you to use the D programming language for a wide range of web-related tasks. The D programming language allows elegant solutions for common problems, while native compilation produces fast binaries. The vibe.d framework takes advantage of these language features. Together with the innovative use of fibers, the applications that you build are scalable and have a very quick response time.
This book will explain everything you need to know about the vibe.d framework in order to successfully build and run web applications.
Chapter 1, Getting Started with Your First Web Application, explains how to set up and use your development environment. At the end of this chapter, you will have already created your first web application.
Chapter 2, Using Templates for Your Web Pages, covers the Diet template engine. You will learn all about templates—from creating simple static templates to using D code in templates.
Chapter 3, Get Interactive – Forms and Flow Control, brings web forms to your application and introduces route matching.
Chapter 4, Easy Forms with the Web Framework, discusses how to validate user input.
Chapter 5, Accessing a Database, shows how to use a database in an application using a variety of SQL and NoSQL bases.
Chapter 6, Using the REST Interface, teaches you about REST services. You will learn how to provide and consume a generated REST service. You will also study how to interface with an existing REST service.
Chapter 7, The vibe.d Internals, introduces you to the fiber-based pseudo-blocking programming model that is the base for vibe.d.
Chapter 8, Using vibe.d with a GUI Client, applies the vibe.d programming model to a graphical UI application.
Chapter 9, Power Your Application with vibe.d Extensions, shows you what other developers have already implemented with vibe.d and how to publish your application.
You need a Linux, Windows, or Mac PC that is capable of running the DMD D compiler and the DUB package manager. Both software packages are available at http://code.dlang.org/. The vibe.d framework requires an SSL library and the libevent library to be installed. The various sources are described in Chapter 1, Getting Started with Your First Web Application.
In order to compile the examples, you need an Internet connection in order to allow the automatic download of dependent software.
For the example in Chapter 5, Accessing a Database, you need Redis 3.x (available at http://redis.io/), MongoDB 3.x (available at http://ww.mongodb.org/), and MySQL 5.x (available at http://dev.mysql.com/downloads/). Chapter 6, Using the REST Interface, uses CouchDB (available at http://couchdb.apache.org/) and Chapter 9, Power Your Application with vibe.d Extensions, uses Cassandra (available at http://cassandra.apache.org/).
Whether you are new to the world of D, already have developed applications in D, or you want to leverage the power of D for web development, then this book is ideal for you. Basic knowledge of core web technologies, such as HTML 5, is helpful; however, it not necessary. This book explains the complex details to speed your web development.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "You also need to install openssl and libevent, which are used by vibe.d."
A block of code is set as follows:
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Click on the Unlock button to unlock the window and change the account type to Administrator."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/8890OS_ColouredImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
Today's World Wide Web is shaped by sites, such as Facebook and Amazon, that handle millions of users. The vibe.d framework is designed to create fast and scalable web applications. A significant difference from other frameworks is the use of the D programming language.
In this chapter, you will learn the following:
The D programming language is a general purpose programming language that offers modern convenience, modeling power, and native efficiency with a familiar C-style syntax. If you are new to D, then you should visit http://dlang.org/. This site contains the language definition, references to the standard libraries, downloads for the D compiler, and a lot of other tools.
If you have any questions about D, then visit the web forum at http://forum.dlang.org/.
To develop an application in D, all you need is your favorite text editor and the D compiler. The vibe.d framework is used for web applications. You can find information about vibe.d and the programming documentation at http://vibed.org/. For any questions about vibe.d, you can use the web forum at http://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/.
The vibe.d framework has some external dependencies, too. It requires the event notification library libevent and the OpenSSL libraries. If you need more information about these libraries, then please visit http://libevent.org/ and https://openssl.org/.
The following section describes how to install the D compiler, package manager DUB, and dependencies on your operating system.
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.
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:
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:
On Fedora, you use the pre-built compiler binary from thedlang.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:
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.
To install the D compiler and the DUB package manager, you can use the Homebrew package manager (http://brew.sh/):
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.
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