Web servers in D
Andrew Chapman via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Sep 2 02:26:27 PDT 2017
On Friday, 25 August 2017 at 05:25:09 UTC, Hasen Judy wrote:
> What libraries are people using to run webservers other than
> vibe.d?
>
> Don't get me wrong I like the async-io aspect of vibe.d but I
> don't like the weird template language and the fact that it
> caters to mongo crowd.
>
> I think for D to a have good web story it needs to appeal to
> serious backend developers, not hipsters who go after fads
> (mongodb is a fad, jade/haml is a fad).
>
> I probably need to combine several libraries, but the features
> I'm looking for are:
>
> - Spawn an HTTP server listening on a port, and routing
> requests to functions/delegates, without hiding the details of
> the http request/response objects (headers, cookies, etc).
>
> - Support for websockets
>
> - Runs delegates in fibers/coroutines
>
> - Basic database connectivity (No "orm" needed; just raw sql).
>
> - When iterating the result set of a sql query, has the ability
> to automatically map each row against a struct, and throw if
> the structure does not match.
>
> - More generally, map any arbitrary object (such as json) to a
> struct. Something like Zewo/Reflection package for swift[0].
>
> [0]: https://github.com/Zewo/Reflection
>
> I feel like Vibe.d satisfies my first 3 requirements, but for
> the rest I will probably have to look for something else.
> Don't get me wrong I like the async-io aspect of vibe.d but I
> don't like the weird template language and the fact that it
> caters to mongo crowd.
Don't use these components :-)
> - Spawn an HTTP server listening on a port, and routing
> requests to functions/delegates, without hiding the details of
> the http request/response objects (headers, cookies, etc).
Vibe.d does this - just don't use the automatic API generation
feature if you don't like it. Note, you can get access to the
request/response objects even if you do use the API generation by
using an @before method. E.g. in an interface you may have
something like this:
@method(HTTPMethod.POST)
@before!getRequestInfo("requestInfo")
@property Token login(LoginRequestMeta login, RequestInfo
requestInfo);
And then define your getRequestInfo method like this:
static RequestInfo getRequestInfo(HTTPServerRequest req,
HTTPServerResponse res)
{
RequestInfo requestInfo;
requestInfo.headers = req.headers;
requestInfo.ipAddress = req.clientAddress.toAddressString();
requestInfo.userAgent = requestInfo.headers.get("User-Agent",
"");
return requestInfo;
}
In this case I've grabbed the ip address and user agent of the
user, but you could also grab cookies etc.
> - When iterating the result set of a sql query, has the ability
> to automatically map each row against a struct, and throw if
> the structure does not match.
You can do this with MySQL Native whilst using vibe.d. You might
do something like this:
Prepared prepared = prepare(this.conn, sql);
prepared.setArgs(params);
auto row = prepared.queryRow();
if (row.isNull()) {
throw new Exception("Query returned an empty row");
}
T item;
try {
row.toStruct!T(item);
} catch(Exception e) {
}
Where T is your struct type that you're trying to convert the row
to.
As for the template language, you could try:
http://code.dlang.org/packages/diamond
https://github.com/repeatedly/mustache-d
There are probably others.
More information about the Digitalmars-d-learn
mailing list