This shows you the differences between two versions of the page.
dss:laboratoare:07 [2021/07/07 12:54] razvan.nitu1305 [1. C function interface] |
dss:laboratoare:07 [2021/07/19 10:08] (current) razvan.nitu1305 [Vibe.d] |
||
---|---|---|---|
Line 194: | Line 194: | ||
</code> | </code> | ||
- | ==== Vibe.d ==== | ||
- | |||
- | The [[http://vibed.org/|vibe.d]] framework [[http://vibed.org/features|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, making the development process sweet and simple. | ||
- | |||
- | === 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> | ||