At Habré, the D language has already been mentioned. But it didn’t gain popularity because of the impossibility of practical use, or rather, most people simply don’t need it. Today I want to tell you about one useful framework for D. but these languages are too low level for such a task. For such a task, you can use this language in conjunction with the vibe.d framework;
Installing dmd and vibe.d is simple enough. When installing the framework under windows, do not forget to register the necessary paths in the PATH
To begin, create a project.
')
vibe init project_name
The framework itself will create the desired structure (3 folders and a manifest)
./views - templates
./public - static files, ala * .css, *. js
./src - application source code
//app.d import vibe.d; import std.stdio; import std.string; import std.file; import std.array; // void image (HTTPServerRequest req, HTTPServerResponse res) { auto file = format("./public/images/%s", req.params["f"]); if(exists(file)) { auto image = cast(ubyte[]) read(file); res.writeBody(image,"image"); } else { res.writeBody("Not Found","text/plain"); } } // , void style (HTTPServerRequest req, HTTPServerResponse res) { auto css = readText(format("./public/styles/%s", req.params["f"])); res.writeBody(css,"text/css"); } // , CSS void error(HTTPServerRequest req, HTTPServerResponse res, HTTPServerErrorInfo error) { res.writeBody("Some error, man","text/plain"); } // void index_req(HTTPServerRequest req, HTTPServerResponse res) { auto request = req.params["r"]; // res.renderCompat!("index.dt", HTTPServerRequest, "req",string,"title")(req,request); // . } void index(HTTPServerRequest req, HTTPServerResponse res) { res.renderCompat!("index.dt", HTTPServerRequest, "req",string,"title")(req,"Main page"); // . } // , . shared static this() { auto settings = new HttpServerSettings; settings.port = 8080; // http 8080 //settings.errorPageHandler = toDelegate(&error); // // ( ) auto router = new URLRouter; router.get("/:r",&index_req); router.get("/",&index); router.get("/style/:f",&style); router.get("/images/:f",&image); // listenHTTP(settings,router); // }
The vibe has a good template engine, a bit controversial, but not bad. Based on the JADE template engine. CSS connects depending on the handler, but the styles should be kept in the public folder. The template files should be kept in views.
!!! 5 html head title Hi world meta(charset="utf-8") link(rel="stylesheet",href="/style/main.css") body div#menu a#logo(href="/") div.links a(href="/main")Main page a(href="/about")About a(href="/some")Some div#content div.article h3 #{title} p Sorry,not founded
The undeniable plus of vibed is work with projects. It is very convenient to work from the console, creating, compiling and debugging a project. I have done a couple of scripts for Notepad ++ myself.
vibe build
Build the project (of course in the project folder).
After compilation, an executable file is created, the necessary libraries are connected. Another plus of this framework is the Batteries included. There are really enough libraries for everything. Moreover, it is not only writing cgi applications, it is a full-fledged asynchronous server. With the development of the project is assumed to develop a load balancer (if understood correctly).
Pros: Speed, Relative simplicity, Low threshold of entry, More equipment, Flexibility (works not only with http, but also directly with tcp)
Cons: Dampness of the project and language, small community
In general, the framework is quite good and convenient. In combination with a convenient language, I think there will be a new competitor GO, node.js.