www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D for Web Development?

reply Michael Reiland <Michael.Reiland gmail.com> writes:
Hey guys,

I'm looking for a web solution that's:

1. Supported on Linux
2. Statically typed,
3. Reasonably performant,
4. Reasonably productive.
5. Simplicity (in terms of infrastructure and the language 
itself).


The contenders as I see them are .Net Core, Go, and D.

I know next to nothing about Go and D and I've never attempted to 
use .Net Core.

I'd like to evaluate D, but since I'm so ignorant I thought I'd 
post here for people's thoughts.

It seems as though vibe.d is the most prevalent framework, and 
even has a book about it here:  
https://www.amazon.com/D-Web-Development-Kai-Nacke/dp/178528889X/


A few questions:

- Is vibe.d the recommended way of doing web work?
- Is that book worth purchasing?
- Does D have a good library for accessing Postgres?  I see 
several listed but I don't know what the most stable would be for 
production:  
https://wiki.dlang.org/Libraries_and_Frameworks#Databases

- Why is D a better solution than Go or .Net Core?  Is there 
something else you would recommend given what I'm looking for?

Here's my take on the last one.

.Net Core is still immature and probably the most complex in 
terms of infrastructure setup/maintenance.  And my impression is 
that Go vs D in the web space is more about personal preference 
than any particular advantage, and I'm coming from C++ so I think 
D would be more my cup of tea.

But that's just my impression from some initial googling.

thoughts?  recommendations?
Jun 08 2017
next sibling parent Mike Parker <aldacron gmail.com> writes:
On Thursday, 8 June 2017 at 07:32:44 UTC, Michael Reiland wrote:

 - Is vibe.d the recommended way of doing web work?
Yes
 - Is that book worth purchasing?
Yes
 - Does D have a good library for accessing Postgres?  I see 
 several listed but I don't know what the most stable would be 
 for production:  
 https://wiki.dlang.org/Libraries_and_Frameworks#Databases
Go to the code.dlang.org, at the top of the page select the category 'Development Libraries` and the subcategory 'Database clients or implementations' (or follow the link at [0]) and you'll see a few, like dpq2 [1]. [0] https://code.dlang.org/?sort=updated&category=library.database [1] https://code.dlang.org/packages/dpq2
Jun 08 2017
prev sibling next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
Welcome!

On Thursday, 8 June 2017 at 07:32:44 UTC, Michael Reiland wrote:
 A few questions:

 - Is vibe.d the recommended way of doing web work?
Yes. Adam D. Ruppe also has some easy to use libraries that may suit your need.
 - Is that book worth purchasing?
Don't know.
 - Does D have a good library for accessing Postgres?  I see 
 several listed but I don't know what the most stable would be 
 for production:  
 https://wiki.dlang.org/Libraries_and_Frameworks#Databases
When looking for D libraries use code.dlang.org. Sorting by most recently updated is the best proxy for activity at the moment.
 - Why is D a better solution than Go or .Net Core?  Is there 
 something else you would recommend given what I'm looking for?

 Here's my take on the last one.

 .Net Core is still immature and probably the most complex in 
 terms of infrastructure setup/maintenance.  And my impression 
 is that Go vs D in the web space is more about personal 
 preference than any particular advantage, and I'm coming from 
 C++ so I think D would be more my cup of tea.

 But that's just my impression from some initial googling.

 thoughts?  recommendations?
If you're coming from C++ you should pick up D in almost no time and it will feel much more natural and less repetitive than go. The best introductory reference is http://ddili.org/ders/d.en/index.html if you come across something you're unfamiliar with, but that is aimed more at beginners, so you can just skim it. The vibe.d site has may good examples. You've already discovered one of the good places to ask questions (the other being the IRC), so feel free to ask away if you have any more. Good luck!
Jun 08 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 8 June 2017 at 08:36:38 UTC, Nicholas Wilson wrote:
 Yes. Adam D. Ruppe also has some easy to use libraries that may 
 suit your need.
Indeed, my cgi.d, database.d, and postgres.d would give a foundation. https://github.com/adamdruppe/arsd you download the individual files and compile them into your program. Depending on how you want to set up, you can change the backend with `-version=embedded_httpd` or `-version=fastcgi` or `-version=scgi`. I recommend using a production grade web server in front of any application server, whether it is mine or vibe or node or whatever, then use a cgi mode to talk to the app or reverse proxy to an embedded http server. anyway, if you are interested let me know and I'll write a simple demo. vibe is more popular but with it's async lib requirement it isn't as compatible. With mine you can use C libraries with ease regardless of i/o model.
Jun 08 2017
parent reply Michael Reiland <Michael.Reiland gmail.com> writes:
Thanks for the link to those resources, that'll definitely help 
giving me a broad overview, which for me is best.  If I know 
something is there I can dive into the details when it becomes 
more important to what I'm doing.


On Friday, 9 June 2017 at 03:34:20 UTC, Adam D. Ruppe wrote:
 anyway, if you are interested let me know and I'll write a 
 simple demo.

 vibe is more popular but with it's async lib requirement it 
 isn't as compatible. With mine you can use C libraries with 
 ease regardless of i/o model.
I'm definitely interested, a small example with the integrated httpd would be great. Just a hello world would be perfect. I was looking around at templating engines for D and came acros Diet, Temple, and embd. Do you have any recommendations on them (or others)? From the documentation for the projects, I'm leaning towards Temple, but it would be nice to get input from someone who has experience with them.
Jun 09 2017
next sibling parent Wulfklaue <wulfklaue wulfklaue.com> writes:
On Friday, 9 June 2017 at 08:20:34 UTC, Michael Reiland wrote:
 Thanks for the link to those resources, that'll definitely help 
 giving me a broad overview, which for me is best.  If I know 
 something is there I can dive into the details when it becomes 
 more important to what I'm doing.

 I'm definitely interested, a small example with the integrated 
 httpd would be great.  Just a hello world would be perfect.
Try this: Module: https://dpaste.dzfl.pl/61bdcc0716eb Run with:
 import webserver;
 
 void main()
 {
     auto Web = new Webserver();
 
     Web.run_();
 }
Sorry if its not a great example, also new to D :)
Jun 09 2017
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Jun 09 2017
parent reply Michael Reiland <Michael.Reiland gmail.com> writes:
On Friday, 9 June 2017 at 15:07:03 UTC, Adam D. Ruppe wrote:
 // 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;
So if I'm understanding this code correctly, your libs don't have any sort of routing setup either, and I would have to write the code to inspect the URL and do the right thing?
 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`.
I think I'd prefer to use more of a template system as it feels more productive having it all inline. I have concerns about the complexity of getting "helper" type functionality in Temple, but I'll tackle that when I get to it. I really really dislike the syntax for DIET, I was never a big HAML fan.
Jun 09 2017
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 9 June 2017 at 22:01:14 UTC, Michael Reiland wrote:
 So if I'm understanding this code correctly, your libs don't 
 have any sort of routing setup either, and I would have to 
 write the code to inspect the URL and do the right thing?
web.d does (it is automatic via names in the source code and nesting of types or aliases), but I don't support that for third parties. It isn't really difficult to do though, `cgi.pathInfo` is all the stuff at the end and you can pretty easily do whatever with it. (to be honest I have never understood why routing is such a major hassle in other libs. It has always been either a just-works or a trivial matter to me.)
 I think I'd prefer to use more of a template system as it feels 
 more productive having it all inline.
huh, I hate template systems. They just so quickly become complete messes with tons of boring code duplication and idiotic syntaxes that are harder to use than plain html+helpers.
Jun 09 2017
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2017-06-08 09:32, Michael Reiland wrote:

 A few questions:

 - Is vibe.d the recommended way of doing web work?
Yes.
 - Is that book worth purchasing?
Yes.
 - Does D have a good library for accessing Postgres?  I see several
 listed but I don't know what the most stable would be for production:
 https://wiki.dlang.org/Libraries_and_Frameworks#Databases
I recommend ddb [1], it's compatible with vibe.d.
 - Why is D a better solution than Go or .Net Core?
D is a very flexible and powerful language that doesn't force you to program in a particular style. In my opinion Go is that that good a creating abstractions. [1] http://code.dlang.org/packages/ddb -- /Jacob Carlborg
Jun 08 2017
prev sibling parent Wulfklaue <wulfklaue wulfklaue.com> writes:
On Thursday, 8 June 2017 at 07:32:44 UTC, Michael Reiland wrote:
 Hey guys,

 I'm looking for a web solution that's:
 1. Supported on Linux
First tier: D, Rust, Go, .netCore Second tier: Nim, Crystal, ... plenty of choices :) Not advised for Linux: Swift... Unless you go for a pure Swift framework, the basic Linux support is horrible.
 2. Statically typed,
 3. Reasonably performant,
Micro benchmarks, the root of all evil: https://github.com/kostya/benchmarks But they show a trend in regards to D. With LDC almost always in the top 5. Comparable speed / memory usage to C/C++.
 4. Reasonably productive.
Unless you go with a very exotic langue, most of them are productive. Try Brainf*ck :)
 5. Simplicity (in terms of infrastructure and the language 
 itself).
Go ... defiantly the most simple. Until you start doing some advanced work and then the skill level also needs to increase. And the whole lacking generics, meta programming etc ... It becomes a issue faster then you think. Like i said, it depends on what you want to write. For pure web, Go is not a bad choice. D is more if people already have some C/C++ background. Java or PHP sure ... you can adapt also from there. The learning curve is harder the Go but not that much.
 The contenders as I see them are .Net Core, Go, and D.

 I know next to nothing about Go and D and I've never attempted 
 to use .Net Core.
Go: Great amount of plugins. But the language is limiting. Good for web development and smaller tasks. Great Cross compile support. Go and Rust have about the most easy cross platform compile support. Something that i find missing in D. .Net Core: Very mature ( if you use the 2.0 version ) but ... memory usage... Say hello to Java. And not exact the most performend.
 I'd like to evaluate D, but since I'm so ignorant I thought I'd 
 post here for people's thoughts.

 It seems as though vibe.d is the most prevalent framework, and 
 even has a book about it here:  
 https://www.amazon.com/D-Web-Development-Kai-Nacke/dp/178528889X/


 A few questions:

 - Is vibe.d the recommended way of doing web work?
Or write your own. You much more experience that way. Look at vibe.d and other frameworks there code for hints.
 - Is that book worth purchasing?
No idea...
 - Does D have a good library for accessing Postgres?  I see 
 several listed but I don't know what the most stable would be 
 for production:  
 https://wiki.dlang.org/Libraries_and_Frameworks#Databases

 - Why is D a better solution than Go or .Net Core?  Is there 
 something else you would recommend given what I'm looking for?
Faster then Go, way faster then .Net Core. Way better memory usage then .Net Core. D's GC seems to be also better then .Net Core. And you can run without GC if needed in specific functions ( nogc ). Is D the best, no ... no language is the best - The manual needs work ( there is a better more overview able one under /Library but most new people like me do not know it ) - Cross platform compile needs work - Some other minor stuff like editor support is a bit weak. But that is also a issue with other "new" languages. But its very feature complete, has all the big boy toys, multiple compilers to fit your need ( dmd for fast compiling, LDC for actual production = 2 a 3 times faster ). If you want stable, features complete, somewhat "boring" then its D for sure. Do not expect to see every buzz word like Swift with constantly changing APIs and unstable changes. It just is a bit boring *lol* ... hard to explain. Its a work language not a hype language.
 Here's my take on the last one.

 .Net Core is still immature and probably the most complex in 
 terms of infrastructure setup/maintenance.
Incorrect... .Net Core 2.0 is fairly future complete. You can run the preview version without issue. I ran the beta from months ago and everything worked like a charm. Its fairly easy to setup on Linux. It just has other issues if you care about performance and memory usage. That might change if the CoreRT cpp compiler ever comes out but that may take years before its really stable. And interacting with C libraries somewhat funky. No manual memory management... Every language has issues.
 And my impression is that Go vs D in the web space is more 
 about personal preference than any particular advantage, and 
 I'm coming from C++ so I think D would be more my cup of tea.
It looks like you made up your mind and just need reaffirming. I know the feeling :) You have only looked between .Net Core, Go and D. Trust me when you have done the list of D, Rust, Go, .netCore, Crystal, Nim, Haxe, Crystal, Jai, Odin or even try to write your own lexer/parser in LLVM, then you know there is no perfect language. And yet i ended up with D despite no C/C++ background. But given your C++ background, D is a perfect fit. In my honest opinion. But with every language, the first steps are the most difficult until you get going. The biggest issue is that D is so feature complete but somewhat bad at drawing people at its future. Go: you think Coroutines. Rust: you think manual memory management, safe. Nim: Meta programming... D ... it has ... well, everything and more but because it has it all, it has no one thing that people talk about all the time. If somebody tells you "defer". You think Go, maybe Swift. You do not think scope(exit) what does the exact same thing in D. Go: Coroutines ... D: fibers ( technically not 100% the same but close ). Etc ... Anyway, lunch break is almost over. I hope this helped you with your choice. Pick what fits for your project.
Jun 09 2017