Skip to content

Commit

Permalink
fix vibe-d#1208 - registerRest/WebInterface should return the router
Browse files Browse the repository at this point in the history
- to allow method chaining
  • Loading branch information
MartinNowak committed Aug 10, 2015
1 parent d74b25b commit dad6483
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
15 changes: 8 additions & 7 deletions source/vibe/web/rest.d
Expand Up @@ -61,7 +61,7 @@ import std.traits;
$(D RestInterfaceClient) class for a seamless way to access such a generated API
*/
void registerRestInterface(TImpl)(URLRouter router, TImpl instance, RestInterfaceSettings settings = null)
URLRouter registerRestInterface(TImpl)(URLRouter router, TImpl instance, RestInterfaceSettings settings = null)
{
import std.traits : InterfacesTuple;
import vibe.internal.meta.uda : findFirstUDA;
Expand Down Expand Up @@ -154,23 +154,24 @@ void registerRestInterface(TImpl)(URLRouter router, TImpl instance, RestInterfac
}
}
}
return router;
}

/// ditto
void registerRestInterface(TImpl)(URLRouter router, TImpl instance, MethodStyle style)
URLRouter registerRestInterface(TImpl)(URLRouter router, TImpl instance, MethodStyle style)
{
registerRestInterface(router, instance, "/", style);
return registerRestInterface(router, instance, "/", style);
}

/// ditto
void registerRestInterface(TImpl)(URLRouter router, TImpl instance, string url_prefix,
URLRouter registerRestInterface(TImpl)(URLRouter router, TImpl instance, string url_prefix,
MethodStyle style = MethodStyle.lowerUnderscored)
{
auto settings = new RestInterfaceSettings;
if (!url_prefix.startsWith("/")) url_prefix = "/"~url_prefix;
settings.baseURL = URL("http://127.0.0.1"~url_prefix);
settings.methodStyle = style;
registerRestInterface(router, instance, settings);
return registerRestInterface(router, instance, settings);
}


Expand Down Expand Up @@ -240,8 +241,8 @@ unittest
{
import vibe.http.server, vibe.http.router;

auto router = new URLRouter();
registerRestInterface(router, new API());
auto router = new URLRouter()
.registerRestInterface(new API());
listenHTTP(new HTTPServerSettings(), router);
}
}
Expand Down
10 changes: 5 additions & 5 deletions source/vibe/web/web.d
Expand Up @@ -130,15 +130,15 @@ import vibe.http.websockets;
$(D @vibe.web.common.contentType)
The `@path` attribute can also be applied to the class itself, in which
case it will be used as an additional prefix to the one in
case it will be used as an additional prefix to the one in
`WebInterfaceSettings.urlPrefix`.
Params:
router = The HTTP router to register to
instance = Class instance to use for the web interface mapping
settings = Optional parameter to customize the mapping process
*/
void registerWebInterface(C : Object, MethodStyle method_style = MethodStyle.lowerUnderscored)(URLRouter router, C instance, WebInterfaceSettings settings = null)
URLRouter registerWebInterface(C : Object, MethodStyle method_style = MethodStyle.lowerUnderscored)(URLRouter router, C instance, WebInterfaceSettings settings = null)
{
import std.algorithm : endsWith;
import std.traits;
Expand Down Expand Up @@ -194,6 +194,7 @@ void registerWebInterface(C : Object, MethodStyle method_style = MethodStyle.low
}
}
}
return router;
}


Expand Down Expand Up @@ -247,9 +248,8 @@ unittest {

void run()
{
auto router = new URLRouter;
router.registerWebInterface(new WebService);

auto router = new URLRouter()
.registerWebInterface(new WebService);
auto settings = new HTTPServerSettings;
settings.port = 8080;
listenHTTP(settings, router);
Expand Down

0 comments on commit dad6483

Please sign in to comment.