D for Web Development?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 9 08:07:03 PDT 2017


On Friday, 9 June 2017 at 08:20:34 UTC, Michael Reiland wrote:
> I'm definitely interested, a small example with the integrated 
> httpd would be great.  Just a hello world would be perfect.

Here's one:


---

// compile with the cgi.d, database.d, postgres.d, and dom.d 
library modules
// and make sure your C libpq library is available. so on my 
computer, the
// compile command is:
// dmd webtest.d  ~/arsd/{cgi,database,postgres,dom} 
-L-L/usr/local/pgsql/lib -m64 -version=embedded_httpd
import arsd.cgi;
import arsd.postgres;
import arsd.dom;

void handle(Cgi cgi) {
         // database setup: psql me
         // create table test (id integer, data text);

         auto database = new PostgreSql("dbname = me");
         scope(exit) .destroy(database);

         // insert some data from the request into the database...
         database.query("INSERT INTO test (data) VALUES (?)", 
cgi.request("data"));

         // of course, you could also load the html skeleton from 
a file
         auto document = new Document("<!DOCTYPE 
html><html><head><title>Demo</title></head><body></body></html>", 
true, true);

         foreach(row; database.query("SELECT id, data FROM test")) 
{
                 document.mainBody.addChild("b", row["id"]);
                 document.mainBody.addChild("p", row["data"]);
         }

         cgi.write(document.toString(), true);
}

mixin GenericMain!handle;

----


After you compile it, run the program and it will stay up with 
the http server (or throw an exception if it fails to start). 
Then you can open your browser to:

http://localhost:8085/?data=store+me


The default port is 8085, though you can also change that when 
you start it up using the `--port xxx` command line option to the 
app server.

Fun fact: cgi.d also includes a little command line parser for 
some simulated requests. So instead of running the server and 
using a browser, you might try:


  ./webtest GET / data=cool

from your command line and see its output right there on the 
console.

> I was looking around at templating engines for D

I don't use any of the templating engines... in the demo above, I 
used my dom.d though which is a potential base lib to make one. 
It is a html parser with many functions for modification. You can 
write your "templates" as just .html files and load them with 
this, then search for various ID elements or whatever (dom.d has 
`querySelector` and `querySelectorAll`) and append elements with 
dom or setting things like `innerText` and `innerHTML`.

Then when you're done building the page, use `cgi.write`, which 
takes any arbitrary data, and send the `document.toString` to it.

The `true` argument to cgi.write btw just means this is all the 
data at once. It allows it to write it out more efficiently to 
the browser.

Actually, back in the day, I had a `web.d` that combines 
everything with magical code generation and more template stuff, 
but I haven't updated that for years now :( I think it still 
works but I haven't documented it either.



But since cgi.d just writes any data, whatever system you like is 
compatible with it as long as they have something like a toString 
method.


The official source of docs for my libs is:

http://dpldocs.info/experimental-docs/arsd.cgi.html
http://dpldocs.info/experimental-docs/arsd.dom.html
http://dpldocs.info/experimental-docs/arsd.database.html
http://dpldocs.info/experimental-docs/arsd.postgres.html


The database libraries are very poorly documented, but there's 
not much to them: the `.query` method is what you use for almost 
everything like seen above.


More information about the Digitalmars-d-learn mailing list