Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Cors to RestInterface #1299

Merged
merged 8 commits into from
Nov 3, 2015
Merged

Adding Cors to RestInterface #1299

merged 8 commits into from
Nov 3, 2015

Conversation

skoppe
Copy link
Contributor

@skoppe skoppe commented Oct 20, 2015

This pull request adds cors support for rest.

  • getRoutesGroupedByPattern() to RestInterface struct
  • add unittest for getRoutesGroupedByPattern()
  • extends jsonMethodHandler signature to take RestInterfaceSettings
  • add code to jsonMethodHandler to handle cors request
  • add code to generate OPTIONS routes in registerRestInterface to handle preflight cors requests
  • add test code in tests/restclient

@skoppe
Copy link
Contributor Author

skoppe commented Oct 20, 2015

need to replace the asLowerCase into dmd 2.066 equivalent

@skoppe
Copy link
Contributor Author

skoppe commented Oct 20, 2015

hmm, apparently chunkBy and the way I use tuple isn't valid on 2.066..

@s-ludwig
Copy link
Member

Seems like groupBy got renamed to chunkBy, but at the same time SortedRange.groupBy was added: https://issues.dlang.org/show_bug.cgi?id=14183

@s-ludwig
Copy link
Member

And tuple didn't yet have support for named fields at all... always fun to be forced to support a range of compiler versions.

@skoppe
Copy link
Contributor Author

skoppe commented Oct 20, 2015

Didn't know about the groupBy, that should make it easier. Will do after lunch.

import std.conv : text;
import std.array : array;
import vibe.http.common : httpMethodString, httpMethodFromString;
// NOTE: don't know what is better, to keep this in memory, or generate on each request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be good as it is now. In case of such small allocations, I'd always opt to reduce the per-request GC pressure (even if there remains a lot to do for the REST module in that regard).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense. In principle I could get the array of methods at compile time, but that entailed more work.

About the GC, I suppose it would be possible to do some static analysis and see which lines of code would allocate. That would be cool. Although it depends entirely on which compiler is used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was actually my first thought, too, doing it at compile time. But for a first version, this is totally sufficient.

The long term goal would be to add some @nogc unit tests for cases where there should be no allocations necessary. But there is still a lot to do/annotate before that will work. I made some attempts, but it was mostly impossible due to so many Phobos functions that either allocated, or weren't annotated @nogc.

@skoppe
Copy link
Contributor Author

skoppe commented Oct 22, 2015

Hmm... groupBy isn't available on 2.066.1

@s-ludwig
Copy link
Member

Huh... I would have never expected this, I thought that that had been included ages ago, but obviously I mixed it up with group(). In that case it looks like some classical loops are needed, there seems to be no useful replacement in 2.066.

foreach (i, func; intf.RouteFunctions) {
auto route = intf.routes[i];

// normal handler
auto handler = jsonMethodHandler!(func, i)(instance, intf);
auto handler = jsonMethodHandler!(func, i)(instance, intf, settings);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is intf.settings, so settings doesn't need to be passed here as an additional parameter. This also has the advantage of always being !is null, so that all null checks can be removed.

@s-ludwig
Copy link
Member

Apart from the settings parameter, looks good to merge now.

@skoppe
Copy link
Contributor Author

skoppe commented Oct 24, 2015

In the tests/restclient I depended on directly referencing/changing the settings, with intf.settings that is not possible since it is copied. Will have to change that.

@skoppe
Copy link
Contributor Author

skoppe commented Nov 3, 2015

Hmm, I might have done something wrong with rebasing...

I wanted to notch travis to do a recompile (I read somewhere that the libasync was working again), and also figured an update wouldn't hurt. Now all these other commits show up. Oops... Well, at least the CI passes.

What is the normal strategy here?

Should I cleanup this stuff by creating new branch?

Fixed

@s-ludwig
Copy link
Member

s-ludwig commented Nov 3, 2015

Okay, thanks! Looks good to merge now. It'll not make it into the upcoming release, but I can tag an early alpha release later to make it available for testing.

s-ludwig added a commit that referenced this pull request Nov 3, 2015
Add CORS support to RestInterface. Fixes #546.
@s-ludwig s-ludwig merged commit efdca6e into vibe-d:master Nov 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants