-
Notifications
You must be signed in to change notification settings - Fork 284
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
Added IHttpRouter interface #177
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,68 @@ import vibe.textfilter.urlencode; | |
import std.functional; | ||
|
||
|
||
/++ | ||
An interface for HTTP request routers. | ||
+/ | ||
interface IHttpRouter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At some point I decided to drop the "I" before interface names, as it doesn't really matter, for a user, if something is an interface or a class. It does, however, make the API kind of ugly if interfaces and classes both occur. For example in vibe.d |
||
public: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Members are public by default, so can be dropped. |
||
// Adds a new route for request that match the path and method | ||
IHttpRouter match(HttpMethod method, string path, HttpServerRequestDelegate cb); | ||
// ditto | ||
final IHttpRouter match(HttpMethod method, string path, IHttpServerRequestHandler cb) { return match(method, path, &cb.handleRequest); } | ||
// ditto | ||
final IHttpRouter match(HttpMethod method, string path, HttpServerRequestFunction cb) { return match(method, path, toDelegate(cb)); } | ||
|
||
// Handles the Http request by dispatching it to the registered request handler | ||
void handleRequest(HttpServerRequest req, HttpServerResponse res); | ||
|
||
// Adds a new route for GET requests matching the specified pattern. | ||
final IHttpRouter get(string url_match, IHttpServerRequestHandler cb) { return get(url_match, &cb.handleRequest); } | ||
// ditto | ||
final IHttpRouter get(string url_match, HttpServerRequestFunction cb) { return get(url_match, toDelegate(cb)); } | ||
// ditto | ||
final IHttpRouter get(string url_match, HttpServerRequestDelegate cb) { return match(HttpMethod.GET, url_match, cb); } | ||
|
||
// Adds a new route for POST requests matching the specified pattern. | ||
final IHttpRouter post(string url_match, IHttpServerRequestHandler cb) { return post(url_match, &cb.handleRequest); } | ||
// ditto | ||
final IHttpRouter post(string url_match, HttpServerRequestFunction cb) { return post(url_match, toDelegate(cb)); } | ||
// ditto | ||
final IHttpRouter post(string url_match, HttpServerRequestDelegate cb) { return match(HttpMethod.POST, url_match, cb); } | ||
|
||
// Adds a new route for PUT requests matching the specified pattern. | ||
final IHttpRouter put(string url_match, IHttpServerRequestHandler cb) { return put(url_match, &cb.handleRequest); } | ||
// ditto | ||
final IHttpRouter put(string url_match, HttpServerRequestFunction cb) { return put(url_match, toDelegate(cb)); } | ||
// ditto | ||
final IHttpRouter put(string url_match, HttpServerRequestDelegate cb) { return match(HttpMethod.PUT, url_match, cb); } | ||
|
||
// Adds a new route for DELETE requests matching the specified pattern. | ||
final IHttpRouter delete_(string url_match, IHttpServerRequestHandler cb) { return delete_(url_match, &cb.handleRequest); } | ||
// ditto | ||
final IHttpRouter delete_(string url_match, HttpServerRequestFunction cb) { return delete_(url_match, toDelegate(cb)); } | ||
// ditto | ||
final IHttpRouter delete_(string url_match, HttpServerRequestDelegate cb) { return match(HttpMethod.DELETE, url_match, cb); } | ||
|
||
// Adds a new route for PATCH requests matching the specified pattern. | ||
final IHttpRouter patch(string url_match, IHttpServerRequestHandler cb) { return patch(url_match, &cb.handleRequest); } | ||
// ditto | ||
final IHttpRouter patch(string url_match, HttpServerRequestFunction cb) { return patch(url_match, toDelegate(cb)); } | ||
// ditto | ||
final IHttpRouter patch(string url_match, HttpServerRequestDelegate cb) { return match(HttpMethod.PATCH, url_match, cb); } | ||
|
||
// Adds a new route for requests matching the specified pattern, regardless of their HTTP verb. | ||
final IHttpRouter any(string url_match, IHttpServerRequestHandler cb) { return any(url_match, &cb.handleRequest); } | ||
// ditto | ||
final IHttpRouter any(string url_match, HttpServerRequestFunction cb) { return any(url_match, toDelegate(cb)); } | ||
// ditto | ||
final IHttpRouter any(string url_match, HttpServerRequestDelegate cb) | ||
{ | ||
return get(url_match, cb).post(url_match, cb) | ||
.put(url_match, cb).delete_(url_match, cb).patch(url_match, cb); | ||
} | ||
} | ||
|
||
/++ | ||
Routes HTTP requests based on the request method and URL. | ||
|
||
|
@@ -64,68 +126,18 @@ import std.functional; | |
} | ||
--- | ||
+/ | ||
class UrlRouter : IHttpServerRequestHandler { | ||
class UrlRouter : IHttpServerRequestHandler, IHttpRouter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realize that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just trying to be consistent :P But I'll change to HttpRouter |
||
private { | ||
Route[][HttpMethod.max+1] m_routes; | ||
} | ||
|
||
/// Adds a new route for GET requests matching the specified pattern. | ||
UrlRouter get(string url_match, IHttpServerRequestHandler cb) { addRoute(HttpMethod.GET, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter get(string url_match, HttpServerRequestFunction cb) { addRoute(HttpMethod.GET, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter get(string url_match, HttpServerRequestDelegate cb) { addRoute(HttpMethod.GET, url_match, cb); return this; } | ||
|
||
/// Adds a new route for POST requests matching the specified pattern. | ||
UrlRouter post(string url_match, IHttpServerRequestHandler cb) { addRoute(HttpMethod.POST, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter post(string url_match, HttpServerRequestFunction cb) { addRoute(HttpMethod.POST, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter post(string url_match, HttpServerRequestDelegate cb) { addRoute(HttpMethod.POST, url_match, cb); return this; } | ||
|
||
/// Adds a new route for PUT requests matching the specified pattern. | ||
UrlRouter put(string url_match, IHttpServerRequestHandler cb) { addRoute(HttpMethod.PUT, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter put(string url_match, HttpServerRequestFunction cb) { addRoute(HttpMethod.PUT, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter put(string url_match, HttpServerRequestDelegate cb) { addRoute(HttpMethod.PUT, url_match, cb); return this; } | ||
|
||
/// Adds a new route for DELETE requests matching the specified pattern. | ||
UrlRouter delete_(string url_match, IHttpServerRequestHandler cb) { addRoute(HttpMethod.DELETE, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter delete_(string url_match, HttpServerRequestFunction cb) { addRoute(HttpMethod.DELETE, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter delete_(string url_match, HttpServerRequestDelegate cb) { addRoute(HttpMethod.DELETE, url_match, cb); return this; } | ||
|
||
/// Adds a new route for PATCH requests matching the specified pattern. | ||
UrlRouter patch(string url_match, IHttpServerRequestHandler cb) { addRoute(HttpMethod.PATCH, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter patch(string url_match, HttpServerRequestFunction cb) { addRoute(HttpMethod.PATCH, url_match, cb); return this; } | ||
/// ditto | ||
UrlRouter patch(string url_match, HttpServerRequestDelegate cb) { addRoute(HttpMethod.PATCH, url_match, cb); return this; } | ||
|
||
/// Adds a new route for requests matching the specified pattern. | ||
UrlRouter any(string url_match, IHttpServerRequestHandler cb) { any(url_match, &cb.handleRequest); return this; } | ||
/// ditto | ||
UrlRouter any(string url_match, HttpServerRequestFunction cb) { any(url_match, toDelegate(cb)); return this; } | ||
/// ditto | ||
UrlRouter any(string url_match, HttpServerRequestDelegate cb) | ||
/// Adds a new route for requests matching the specified HTTP method and pattern. | ||
UrlRouter match(HttpMethod method, string path, HttpServerRequestDelegate cb) | ||
{ | ||
get(url_match, cb); | ||
post(url_match, cb); | ||
put(url_match, cb); | ||
delete_(url_match, cb); | ||
patch(url_match, cb); | ||
m_routes[method] ~= Route(path, cb); | ||
return this; | ||
} | ||
|
||
/// Adds a new route for requests matching the specified HTTP method and pattern. | ||
void match(HttpMethod method, string path, IHttpServerRequestHandler cb) { match(method, path, &cb.handleRequest); } | ||
/// ditto | ||
void match(HttpMethod method, string path, HttpServerRequestFunction cb) { match(method, path, toDelegate(cb)); } | ||
/// ditto | ||
void match(HttpMethod method, string path, HttpServerRequestDelegate cb) { m_routes[method] ~= Route(path, cb); } | ||
|
||
/// Alias for backwards compatibility | ||
alias match addRoute; | ||
|
||
|
@@ -187,4 +199,4 @@ private string skipPathNode(string str, ref size_t idx) | |
size_t start = idx; | ||
while( idx < str.length && str[idx] != '/' ) idx++; | ||
return str[start .. idx]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: would be good to have
/** .. */
style comments for consistency