Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed Apr 25, 2015
1 parent 3028d6e commit ff196e8
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 40 deletions.
56 changes: 48 additions & 8 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,47 @@ Phobos_ is the standard runtime library that comes with the D language compiler.
Simple project with dub
~~~~~~~~~~~~~~~~~~~~~~~

DUB_ is a build tool for D projects with support for automatically retrieving dependencies and integrating them in the build process. The design emphasis is on maximum simplicity for simple projects, while providing the opportunity to customize things when needed.
DUB_ is a build tool for D projects with support for automatically retrieving dependencies and integrating them in the build process. The design emphasis is on maximum simplicity for simple projects, while providing the opportunity to customize things when needed.

To create the initial project with name ``component``, run ``dub init component``.

Remove automatically created ``component/source/app.d`` file
and create the following file structure

.. code-block:: text
dub.json
component/
source/
component/
mod.d
package.d
where ``component/package.d`` is the main module ``component``

.. literalinclude:: ../../examples/dub-example/component/source/component/package.d
:language: d
:tab-width: 4

and ``component/mod.d`` is the inner module ``component.mod``

.. literalinclude:: ../../examples/dub-example/component/source/component/mod.d
:language: d
:tab-width: 4

To test this module, run ``dub test`` from package's folder.

``removeSingleLineComments`` can be imported with ``import component;`` and ``import component.mod;``.
To use *component* package, put the following dependency into your project's dub.json into the dependencies section:

.. code-block:: js
{
...
"dependencies": {
"component": "~master"
}
}
.. _DUB: http://code.dlang.org/getting_started

Expand Down Expand Up @@ -89,9 +129,9 @@ reads data from a file and shows the histogram.
Web Application
~~~~~~~~~~~~~~~

Web application is a pretty good example of the last chapters of the book, where the reader is suggested to make use of the means of expression in the language. As a rule, web application is a complex product, both in terms of knowledge of the language and in terms of complexity used in this library.
Web application is a pretty good example of the last chapters of any book, where the reader is suggested to make use of the means of expression in the language. As a rule, web application is a complex product, both in terms of knowledge of the language and in terms of code complexity of the used libraries.

And this example is no exception. Then why do people who want to learn this language very quickly have a need in it? Many of them have a reason and it is that they need to integrate quickly programs written in D with other services, programming languages and databases.
And this example is no exception. Then why do people who want to learn D language very quickly still need example of web app? Many of them have a reason and it is that they need to integrate quickly programs written in D with other services, programming languages and databases.

The article "`Creating a simple restful web app with node.js, Express, and MongoDB <http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/>`_" by Christopher Buecheler is taken as a basis for this example.

Expand All @@ -105,7 +145,7 @@ To create a skeleton web application, run:

.. code-block:: shell
$ dub vibed-mongo vibe.d
$ dub init vibed-mongo vibe.d
This will make in directory ``vibed-mongo`` with a minimal HTTP server based on vibe.d.

Expand Down Expand Up @@ -135,14 +175,14 @@ The project has the following structure:
style.css
favicon.ico
After installing MongoDB run Mongo servers
After installing MongoDB, run the server

.. code-block:: shell
$ mongod
In another console run Mongo console
In another console run the Mongo interpreter

.. code-block:: shell
Expand All @@ -163,7 +203,7 @@ In another console run Mongo console
> exit
bye
The script above will create a ``vibed`` database with a ``userlist`` collection, which will contain one record.
The above script creates a ``vibed`` database with a ``userlist`` collection, which will contain one record.
Patches
^^^^^^^^^
Expand Down Expand Up @@ -193,7 +233,7 @@ Service performs an ``insert``, ``select`` and ``remove`` operations for user en
App
^^^^^^^^^^^^^
Following static constructor connects ``vebid`` MongoDB, creates vibe.d server and implements an error handler.
The following static constructor connects ``vibed`` database, creates the HTTP server and implements the errors handler.
.. literalinclude:: ../../examples/vibed-mongo/source/app.d
:language: d
Expand Down
8 changes: 4 additions & 4 deletions docs/source/integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ Integration with other languages
C and friends
-------------

D that has full support for C ABI (application binary interface) had recently been significantly improved from C++ ABI [#ABI]_ (however it's worth noting that there is no support for exceptions). Jacob Carlborg did a great job of integrating with Objective-C, which is still waiting to be no less grandiose Review by Walter Bright.
D that `has <http://dlang.org/interfaceToC.html>`_ full support for C ABI [#ABI]_ had recently been significantly improved for `interfacing with C++ <http://dlang.org/cpp_interface.html>`_ (however it's worth noting that there is no support for C++ exceptions). Jacob Carlborg did a great job of integrating with Objective-C, which is still waiting to be no less grandiose Review by Walter Bright.

Scripting languages
-------------------

You are already somehow familiar with the integration of scripting languages on the example of the use of the library matplotlib (python). Since most of them have a C API [#API]_, their integration with D can be performed without problems.
You are already somehow familiar with the integration of scripting languages on the example of the use of the matplotlib library and `PyD <https://github.com/ariovistus/pyd>`_. Since most of them have a C API [#API]_, their integration with D can be performed without problems.

There is a realization of the ECMA 262 (Javascript) programming language written by Walter Bright and updated by Dmitry Olshansky.
There is a `realization <https://github.com/DigitalMars/DMDScript>`_ of the ECMA 262 (Javascript) programming language written by Walter Bright and updated by Dmitry Olshansky.

It is also worth mentioning a popular computer games scripting language Lua. Unlike many other libraries built on the Lua C API, LuaD does not expose the Lua stack - instead, it has wrappers for references to Lua objects, and supports seamlessly and directly converting any D type into a Lua type and vice versa
It is also worth mentioning a popular computer games scripting language Lua. Unlike many other libraries built on the Lua C API, `LuaD <https://github.com/JakobOvrum/LuaD>`_ does not expose the Lua stack - instead, it has wrappers for references to Lua objects, and supports seamlessly and directly converting any D type into a Lua type and vice versa.

.. [#API] Application Programming Interface
.. [#ABI] Application Binary Interface
29 changes: 10 additions & 19 deletions docs/source/links.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ Links

NOT READY!

General
-----------

Libraries and Frameworks
-------------------------
Books
-------------------

Articles
------------

Compilers
--------------

Development Environments
------------------------



.. DMD
.. ~~~
Expand All @@ -30,21 +32,7 @@ Compilers
.. GDC
Build systems
--------------

.. Makefile
.. ~~~~~~~~
.. rdmd
.. ~~~~~~~~
.. dub
.. ~~~~~~~~
Development Environments
--------------
.. Sublime Text 3
.. ~~~~~~~~~~~~~~
Expand Down Expand Up @@ -90,3 +78,6 @@ Online tools
.. Interactive D Compiler and Disassembler
.. ----------------------------------------
Libraries and Frameworks
-------------------------
5 changes: 5 additions & 0 deletions examples/dub-example/application/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.dub
docs.json
__dummy.html
*.o
*.obj
9 changes: 9 additions & 0 deletions examples/dub-example/application/dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "application",
"description": "A minimal D application.",
"copyright": "Copyright © 2015, ilya",
"authors": ["ilya"],
"dependencies": {
"comp": "~master"
}
}
8 changes: 8 additions & 0 deletions examples/dub-example/application/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import component;

import std.stdio;

void main()
{
writeln("Edit source/app.d to start your project.");
}
5 changes: 5 additions & 0 deletions examples/dub-example/component/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.dub
docs.json
__dummy.html
*.o
*.obj
3 changes: 3 additions & 0 deletions examples/dub-example/component/dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "component",
}
64 changes: 64 additions & 0 deletions examples/dub-example/component/source/component/mod.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/++
Module a;
+/
module component.mod;

import std.algorithm, std.ascii, std.range, std.string, std.functional;

/++
Params:
or = output range
ir = input range
cmt = comment prefix (like // in or # in Python)
Returns
Text without single line comments.
+/
void removeSingleLineComments
(OutputRange, Range1, Range2) // template parameters
(OutputRange or, Range1 ir, Range2 cmt) // function parameters
{
foreach(line; lineSplitter(ir))
{
auto s = line.save.find!(not!isWhite);
if(!s.startsWith(cmt))
{
put(or, line.until(cmt));
put(or, "\n");
}
}
}

/// Unittests with comment appears in documentation.
unittest
{
auto app = appender!string;

immutable text0 =
`
// main function
int main()
{
// return statement
return 0; //returns 0
}
`;

immutable text1 =
`
int main()
{
return 0;
}
`; // Note: "return 0; " ends with a space character

removeSingleLineComments(app, text0, "//");

debug
{
import std.stdio;
writeln("text:", app.data);
}

assert(app.data == text1);
}
6 changes: 6 additions & 0 deletions examples/dub-example/component/source/component/package.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/++
Pacakge component
+/
module component;

public import component.mod : removeSingleLineComments;
14 changes: 5 additions & 9 deletions examples/vibed-mongo/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,21 @@ shared static this()
auto db = connectMongoDB("localhost").getDatabase("vibed");
auto collection = db["userlist"];

logInfo("Querying DB...");
//Bson query = Bson(["name" : Bson("hans")]);
auto result = collection.find();

logInfo("Iterating results...");
foreach (i, doc; result)
logInfo("Item %d: %s", i, doc.toJson().toString());

logInfo("Creating service...");
auto mongoService = new MongoService(collection, title);

auto mongoServiceSettings = new WebInterfaceSettings;
mongoServiceSettings.urlPrefix = "/users";


logInfo("Setup router...");
auto router = new URLRouter;
router.registerWebInterface(mongoService, mongoServiceSettings);
router
.get("/", (req, res)
{ res.redirect("/users"); } )
.get("*", serveStaticFiles("public/"));

logInfo("Setup HTTP server...");
auto settings = new HTTPServerSettings;
with(settings)
{
Expand All @@ -45,5 +40,6 @@ shared static this()
};
}

logInfo("Listen HTTP");
listenHTTP(settings, router);
}

0 comments on commit ff196e8

Please sign in to comment.