# Le Project

In order to keep your momentum going, we're proposing you the following project.

A simple example flow would be:

1. Ask peers for their list of files
3. Ask peers for chunks of the same file (must have the same MD5) and rebuild the file, writing it on disk
4. Check that the downloaded file has the same MD5 as the source. If the file is corrupted notify the user

You are required to use the vibe.d framework, as this will make the development process sweet and simple. vibe.d provides a toolkit for doing asynchronous I/O in a familiar and natural procedural style. It combines the intuitive and efficient programming style of synchronous I/O with the scalability of asynchronous I/O.

• Authenticates an user. Only a logged in user can trigger a download action
• Sends a register request to a new peer
• Sends a register response ACK when accepting a register request
• Sends a request of the list of shared files to all the known peers
• Provides a JSON with the list of shared files and their corresponding MD5 as an answer to a list request
• Starts the download of a given file from as many peers as possible

## Getting started with vibe.d

The easiest way to get started is to use the DUB package manager and let it handle the downloading and building of vibe.d and derived applications.

To initialize the skeleton of a simple app, run the following command from your projects directory:

cd /path/to/my/projects
dub init <project-name> -t vibe.d

This will create a new directory with the given name and creates the basic directory structure that is recommended for vibe.d projects. Running it will start up an HTTP server on port 8080, serving a simple plain-text page.

Your hierarchy should look like this:

project-name/
dub.json
public/
source/
app.d
views/

The default app.d file has the following content

import vibe.vibe;

void main()
{
auto settings = new HTTPServerSettings;
settings.port = 8080;
listenHTTP(settings, &hello);

runApplication();
}

void hello(HTTPServerRequest req, HTTPServerResponse res)
{
res.writeBody("Hello, World!");
}

Once you have the project in place, simply run DUB from the project's root directory and it will get all dependencies, compile the application, and run it:

cd path/to/project
dub
Performing "debug" build using dmd for x86_64.
vibe-d 0.7.26: target for configuration "libevent" is up to date.
vibedtest ~master: building configuration "application"...
To force a rebuild of up-to-date targets, run again with --force.
Running ./vibedtest
Listening for requests on http://::1:8080
Listening for requests on http://127.0.0.1:8080
Please open http://127.0.0.1:8080/ in your browser.

As the next step, you can go ahead and edit the source/app.d file. For a simple web application, the app.d file could look similar to this one:

import vibe.d;

void userInfo(HTTPServerRequest req, HTTPServerResponse res)
{
}

{
enforceHTTP("user" in req.form, HTTPStatus.badRequest, "Missing user field.");
res.redirect("/users/" ~ req.form["user"]);
}

shared static this()
{
auto router = new URLRouter;
router.get("/users/:user", &userInfo);
router.get("*", serveStaticFiles("./public/"));

listenHTTP(new HTTPServerSettings, router);
}

An example of the html template userinfo.dt file could look something like this. Those files are called Diet templates. To get used to the syntax please read the docs.

doctype html
html
p Hello user '#{username}'