⬆️ ⬇️

Own Web project on D under Ubuntu OS

This material was born thanks to the article Simple website on D by danial72 . Only there the moment was disassembled under Windows OS . In my article, the process of preparing the working environment for creating your own Web project in the D programming language will be considered step by step on the example of Ubuntu 13.04 OS .



The point is, if you have a complex and resource-intensive project, and the thought that tens of thousands of lines of php-code will be parsed every time you call the page, and there are classes with inheritance, and template engines some, and even active there will be thousands of users, cause you panic and horror, then it makes sense to think about a rational and optimal solution for the implementation of such development. On the contrary, if you have never thought about it, then most likely you will not have any powerful Server.



Mark Zuckerberg and his team, once solved this problem by developing HipHop , which converted the PHP script to C ++ and that, in turn, was going to a compiled server, sharpened for this project. We are with you, we will do something similar, but we will immediately write everything in a compiled language to avoid unnecessary code translations and to avoid possible bugs in this process. Moreover, the D language is so convenient that you can easily use it to solve your problems.



The following steps will show how to deploy the whole thing:

  1. Visit the page DLang.org and download the distribution for your system.
  2. Next you need to install the DUB manager . With it, we will deploy, compile and run our project. You need to add the repository, the key and update the dependencies.

    $ sudo wget http://netcologne.dl.sourceforge.net/project/d-apt/files/d-apt.list -O /etc/apt/sources.list.d/d-apt.list $ sudo apt-get update && sudo apt-get -y --allow-unauthenticated install --reinstall d-apt-keyring && sudo apt-get update 
    Thereafter

     $ sudo apt-get install dub 
  3. Upon completion of the installation, we create the projects folder in which our project will be created.

     $ cd /home/< >/projects $ dub init < > 
    This operation will create the following file structure in your project folder.

     public/ source/ app.d views/ package.json 
  4. We are interested in the package.json file. Open it and add the following text:

     "dependencies": { "vibe-d": ">=0.7.16" } 
    Now, at compile time, DUB automatically downloads the latest package vibe.d
  5. Next, open the file app.d with any editor. I would advise to use IDE, but unfortunately I did not find a full-fledged IDE for MS Visual Studio 2008 or Borland Delphi for the D language under Ubuntu, but you can only use partly or with crutches. My choice, while I stopped at Geany .

    In the file at the very beginning we write ...
     #!/usr/bin/rdmd 
    This command indicates that the file will be compiled automatically at the time of execution using the RDMD utility from the DMD package.

    The file itself is made executable.
     chmod 775 
    In addition, the specified example in the file generated automatically, you can delete and paste this code
     import vibe.d; import std.file; 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 css(HTTPServerRequest req, HTTPServerResponse res) { auto css = readText(format("./public/css/%s", req.params["f"])); res.writeBody(css,"text/css"); } 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")(req); } shared static this() { auto router = new URLRouter; router.get("/:r",&index_req); router.get("/", &index); router.get("/css/:f",&css); router.get("/images/:f",&image); auto settings = new HTTPServerSettings; settings.port = 8080; listenHTTP(settings, router); } 


    Styles and pictures, as you probably have noticed in this example, should be stored in folders public / css / and public / images / respectively.

    Two files should be added to the views folder:

    layout.dt

     !!! 5 html head title Example page body block body 
    and index.dt

     extends layout block body h1 Example page - Home p Hello, World! 
    This is a template engine. How to convert your pages is described here , and an automatic converter is here .

    Save all our files.
  6. Go to the folder of our project and run DUB in the command line.

     $ cd /home/< /projects/< > $ dub 
    If everything is correct, the following text should appear on the screen:

     Checking dependencies in '/home/< >/Projects/< >' Building configuration "application", build type debug Running dmd (compile)... Compiling diet template 'index.dt' (compat)... Compiling diet template 'index.dt' (compat)... Linking... Running /tmp/dub/1994091216/< >... Listening for HTTP requests on :::8080 Failed to listen on 0.0.0.0:8080 
    The last line seems to correspond to an error, but in fact it is an attempt to listen to IPv6, which is not active in my case. Only IPv4 is used.

    ')

    In the browser window we drive 127.0.0.1:8080



    Voila ... our site.


After any changes, it is enough to interrupt the DUB operation in the terminal and start it again. All files are automatically recompiled.

Source: https://habr.com/ru/post/185878/



All Articles