This shows you the differences between two versions of the page.
|
dss:project [2019/06/20 17:47] eduard.staniloiu |
dss:project [2019/06/20 19:06] (current) eduard.staniloiu |
||
|---|---|---|---|
| Line 27: | Line 27: | ||
| * Starts the download of a given file from as many peers as possible | * 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: | ||
| + | <code bash> | ||
| + | cd /path/to/my/projects | ||
| + | dub init <project-name> -t vibe.d | ||
| + | </code> | ||
| + | |||
| + | 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: | ||
| + | <code bash> | ||
| + | project-name/ | ||
| + | dub.json | ||
| + | public/ | ||
| + | source/ | ||
| + | app.d | ||
| + | views/ | ||
| + | </code> | ||
| + | |||
| + | The default **app.d** file has the following content | ||
| + | <code d> | ||
| + | import vibe.vibe; | ||
| + | |||
| + | void main() | ||
| + | { | ||
| + | auto settings = new HTTPServerSettings; | ||
| + | settings.port = 8080; | ||
| + | settings.bindAddresses = ["::1", "127.0.0.1"]; | ||
| + | listenHTTP(settings, &hello); | ||
| + | |||
| + | logInfo("Please open http://127.0.0.1:8080/ in your browser."); | ||
| + | runApplication(); | ||
| + | } | ||
| + | |||
| + | void hello(HTTPServerRequest req, HTTPServerResponse res) | ||
| + | { | ||
| + | res.writeBody("Hello, World!"); | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | 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: | ||
| + | |||
| + | <code bash> | ||
| + | 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"... | ||
| + | Linking... | ||
| + | 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. | ||
| + | </code> | ||
| + | |||
| + | 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: | ||
| + | |||
| + | <code d> | ||
| + | import vibe.d; | ||
| + | |||
| + | void userInfo(HTTPServerRequest req, HTTPServerResponse res) | ||
| + | { | ||
| + | auto username = req.params["user"]; | ||
| + | render!("userinfo.dt", username)(res); | ||
| + | } | ||
| + | |||
| + | void addUser(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.post("/adduser", &addUser); | ||
| + | router.get("*", serveStaticFiles("./public/")); | ||
| + | |||
| + | listenHTTP(new HTTPServerSettings, router); | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | 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 [[http://vibed.org/templates/diet|docs]]. | ||
| + | <code dt> | ||
| + | doctype html | ||
| + | html | ||
| + | head | ||
| + | title Example page | ||
| + | body | ||
| + | p Hello user '#{username}' | ||
| + | </code> | ||