RejectedSoftware Forums

Sign up

vibe.d rest with http header handler added with @after interception

Hello guys and (girls) to be political correct (it is a bis movement here in germany),

i am really new to vibe.d and the D programming language but very impressive
Look for a alternative for Scala (very slow compile time) and D seems to be a good candidate.

Try to build a simple rest api with vibe.d see (https://github.com/pussinboots/dfirstweb/blob/master/source/app.d).

I need a possibility to set the http header "Access-Control-Allow-Origin" and
try it with the @after feature but i have a problem to define it for a void method.

@after!addAccessControlOrigin()
void getInfo();  

void addAccessControlOrigin(HTTPServerRequest, HTTPServerResponse res)
{	res.headers["Access-Control-Allow-Origin"] = "*";
}

But the compile say no "Input attribute function addAccessControlOrigin(HTTPServerRequest, HTTPServerResponse) argument list does not match provided argument list void, HTTPServerRequest, HTTPServerResponse". I will commit the non working code to git in the next days. But i hope the
example show what i want.

Maybe some of you have an advice like void method are not supported at the moment than i will
look in the vibe.d code and prepare a patch.

Thanks for listen and answer

Re: vibe.d rest with http header handler added with @after interception

void method does not make much sense in REST context and it did not came to my mind someone may need it. GET methods should be idempotent to conform REST. Supporting void return type for POST methods though makes sense, please file an issue for that.

Re: vibe.d rest with http header handler added with @after interception

Am 10.02.2014 20:24, schrieb Frank:

Hello guys and (girls) to be political correct (it is a bis movement here in germany),

i am really new to vibe.d and the D programming language but very impressive
Look for a alternative for Scala (very slow compile time) and D seems to be a good candidate.

Try to build a simple rest api with vibe.d see (https://github.com/pussinboots/dfirstweb/blob/master/source/app.d).

I need a possibility to set the http header "Access-Control-Allow-Origin" and
try it with the @after feature but i have a problem to define it for a void method.

@after!addAccessControlOrigin()
void getInfo();

void addAccessControlOrigin(HTTPServerRequest, HTTPServerResponse res)
{	res.headers["Access-Control-Allow-Origin"] = "*";
}

But the compile say no "Input attribute function addAccessControlOrigin(HTTPServerRequest, HTTPServerResponse) argument list does not match provided argument list void, HTTPServerRequest, HTTPServerResponse". I will commit the non working code to git in the next days. But i hope the
example show what i want.

Maybe some of you have an advice like void method are not supported at the moment than i will
look in the vibe.d code and prepare a patch.

Thanks for listen and answer

As a workaround, you could also use the URLRouter to set the header:

router.any("*", &setAccessControlOrigin);
router.registerRestInterface(...);

This will also avoid having to repeat the @after annotations.

In the long run, this will need to be implemented directly in the REST
interface generator, including a check for the actual origin of clients.
I've opened two issues for this:
#546,
#547

Re: vibe.d rest with http header handler added with @after interception

Hello again,

thanks for the very good answers. I will try the approach by setting the http header with the URLRouter class. Also thanks for posting the feature request.

keep working