Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
Updated dependencies; Added the autoCompress parameter to the start()…
Browse files Browse the repository at this point in the history
… function; Added Route.statusCode and DefaultRoute.statusCode parameters.
  • Loading branch information
luizmineo committed Oct 12, 2014
1 parent e545b70 commit 748210f
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.project
.buildlog
.idea
.pub
*.precompiled.js
*.js_
*.js.deps
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## v0.5.17
* Updated dependencies.
* Added the `autoCompress` parameter to the `start()` function.
* Added `Route.statusCode` and `DefaultRoute.statusCode` parameters.

**Note:** this version requires Dart 1.7 or above

## v0.5.16
* Fix: Setting a new value in a `QueryMap` with the dot notation is not working properly.

Expand Down
7 changes: 6 additions & 1 deletion lib/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,14 @@ void setShelfHandler(shelf.Handler handler) {
*
* The [address] can be a [String] or an [InternetAddress].
*
* If [autoCompress] is true, the server will use gzip to compress the content
* when possible.
*
* When [secureOptions] is specified the server will use a secure https connection.
* [secureOptions] is a map of named arguments forwarded to [HttpServer.bindSecure].
*/
Future<HttpServer> start({address: _DEFAULT_ADDRESS, int port: _DEFAULT_PORT,
bool autoCompress: false,
Map<Symbol, dynamic> secureOptions}) {
return new Future(() {

Expand All @@ -400,7 +404,8 @@ Future<HttpServer> start({address: _DEFAULT_ADDRESS, int port: _DEFAULT_PORT,
serverFuture = Function.apply(HttpServer.bindSecure, [address, port], secureOptions);
}

return serverFuture.then((server) {
return serverFuture.then((HttpServer server) {
server.autoCompress = autoCompress;
server.listen((HttpRequest req) {
_logger.fine("Received request for: ${req.uri}");
_dispatchRequest(new _RequestImpl(req)).catchError((e, s) {
Expand Down
19 changes: 15 additions & 4 deletions lib/src/metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ part of redstone_server;
* [urlTemplate] is the url of the target, and can contains arguments prefixed with ':'.
* [methods] are the HTTP methods accepted by this target, and defaults to GET.
* The [responseType] is the content type of the response. If it's not provided,
* the framework will generate it based on the value returned by the method;
* If [allowMultipartRequest] is true, then the Route is allowed to receive
* multipart requests (file upload).
* the framework will generate it based on the value returned by the method.
* The [statusCode] is the default status of the response, and if not provided,
* the 200 status code will be used. If [allowMultipartRequest] is true,
* then the Route is allowed to receive multipart requests (file upload).
*
* Example:
*
Expand All @@ -24,19 +25,23 @@ class Route {

final String responseType;

final int statusCode;

final bool allowMultipartRequest;

final bool matchSubPaths;

const Route(this.urlTemplate,
{this.methods: const [GET],
this.responseType,
this.statusCode: 200,
this.allowMultipartRequest: false,
this.matchSubPaths: false});

Route.conf(this.urlTemplate,
{this.methods: const [GET],
this.responseType,
this.statusCode: 200,
this.allowMultipartRequest: false,
this.matchSubPaths: false});

Expand Down Expand Up @@ -212,6 +217,8 @@ class Group {
* the suffix will be handled. [methods] are the HTTP
* methods accepted by this target, and defaults to GET.
* The [responseType] is the content type of the response.
* The [statusCode] is the default status of the response,
* and if not provided, the 200 status code will be used.
* If it's not provided, the framework will generate it based on
* the value returned by the method; If [allowMultipartRequest]
* is true, then the Route is allowed to receive multipart
Expand All @@ -226,13 +233,17 @@ class DefaultRoute {

final String responseType;

final int statusCode;

final bool allowMultipartRequest;

final bool matchSubPaths;

const DefaultRoute({this.pathSuffix,
this.methods: const [GET],
this.responseType, this.allowMultipartRequest: false,
this.responseType,
this.statusCode: 200,
this.allowMultipartRequest: false,
this.matchSubPaths: false});
}

Expand Down
4 changes: 4 additions & 0 deletions lib/src/server_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ Future _writeResponse(respValue, String responseType, {int statusCode: 200,
respValue = processors.fold(respValue, (v, p) =>
p.processor(p.metadata, p.handlerName, v, _injector));
_writeResponse(respValue, responseType,
statusCode: statusCode,
abortIfChainInterrupted: abortIfChainInterrupted).then((_) =>
completer.complete());
} else {
Expand All @@ -446,12 +447,14 @@ Future _writeResponse(respValue, String responseType, {int statusCode: 200,

respValue.then((fValue) {
return _writeResponse(fValue, responseType,
statusCode: statusCode,
processors: processors,
abortIfChainInterrupted: abortIfChainInterrupted);
}).then((_) {
completer.complete();
}).catchError((e) {
return _writeResponse(e, responseType,
statusCode: statusCode,
processors: processors,
abortIfChainInterrupted: abortIfChainInterrupted);
}, test: (e) => e is ErrorResponse)
Expand All @@ -464,6 +467,7 @@ Future _writeResponse(respValue, String responseType, {int statusCode: 200,
respValue = processors.fold(respValue, (v, p) =>
p.processor(p.metadata, p.handlerName, v, _injector));
_writeResponse(respValue, responseType,
statusCode: statusCode,
abortIfChainInterrupted: abortIfChainInterrupted).then((_) =>
completer.complete());

Expand Down
6 changes: 4 additions & 2 deletions lib/src/setup_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ void _configureGroup(_ServerMetadataImpl serverMetadata,
}
Route route = new Route.conf(url, methods: info.methods,
responseType: info.responseType,
statusCode: info.statusCode,
allowMultipartRequest: info.allowMultipartRequest,
matchSubPaths: info.matchSubPaths);

Expand Down Expand Up @@ -800,6 +801,7 @@ void _configureTarget(_ServerMetadataImpl serverMetadata,

_logger.finer("Writing response for target $handlerName");
return _writeResponse(respValue, route.responseType,
statusCode: route.statusCode,
abortIfChainInterrupted: true,
processors: responseProcessors)
.then((_) {
Expand Down Expand Up @@ -829,8 +831,8 @@ void _configureTarget(_ServerMetadataImpl serverMetadata,

UrlTemplate urlTemplate = new UrlTemplate(url);

if (route.matchSubPaths && urlTemplate.urlParameterNames().isNotEmpty) {
var lastParam = urlTemplate.urlParameterNames().last;
if (route.matchSubPaths && urlTemplate.urlParameterNames.isNotEmpty) {
var lastParam = urlTemplate.urlParameterNames.last;
if (originalUrl.endsWith(":$lastParam*")) {
specialPathParam = lastParam;
}
Expand Down
14 changes: 7 additions & 7 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
name: redstone
version: 0.5.16
version: 0.5.17
author: Luiz Mineo <[email protected]>
description: A metadata driven microframework for Dart
homepage: http://redstonedart.org
environment:
sdk: '>=1.4.0 <2.0.0'
sdk: '>=1.7.0-dev.4.5 <2.0.0'
dependencies:
collection: '>=0.9.3+1 <0.10.0'
collection: '>=0.9.4 <2.0.0'
crypto: '>=0.9.0 <0.10.0'
grinder: '>=0.5.2 <0.6.0'
route_hierarchical: '>=0.4.22 <0.5.0'
di: ">=2.0.1 <3.0.0"
shelf: ">=0.5.4+2 <0.6.0"
route_hierarchical: '>=0.5.0 <0.6.0'
di: ">=3.3.1 <4.0.0"
shelf: ">=0.5.5 <0.6.0"
mime: ">=0.9.0+1 <0.10.0"
http: ">=0.11.1+1 <0.12.0"
stack_trace: ">=0.9.1 <2.0.0"
stack_trace: ">=1.1.1 <2.0.0"
dev_dependencies:
unittest: '>=0.11.0+3 <0.12.0'
8 changes: 8 additions & 0 deletions test/server_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ main() {
var req3 = new MockRequest("/paths");
var req4 = new MockRequest("/path2/sub/path");
var req5 = new MockRequest("/path3/sub/path");
var req6 = new MockRequest("/change_status_code");

return app.dispatch(req).then((resp) {
expect(resp.mockContent, equals("sub_route"));
Expand All @@ -49,6 +50,9 @@ main() {
expect(resp.mockContent, equals("sub"));
}).then((_) => app.dispatch(req5)).then((resp) {
expect(resp.mockContent, equals("sub/path"));
}).then((_) => app.dispatch(req6)).then((resp) {
expect(resp.statusCode, equals(201));
expect(resp.mockContent, equals("response"));
});
});

Expand All @@ -60,6 +64,7 @@ main() {
var req5 = new MockRequest("/group");
var req6 = new MockRequest("/group.json");
var req7 = new MockRequest("/group", method: app.POST);
var req8 = new MockRequest("/group/change_status_code");

return app.dispatch(req).then((resp) {
expect(resp.mockContent, equals("interceptor sub_route"));
Expand All @@ -75,6 +80,9 @@ main() {
expect(resp.mockContent, equals("default_route_json"));
}).then((_) => app.dispatch(req7)).then((resp) {
expect(resp.mockContent, equals("default_route_post"));
}).then((_) => app.dispatch(req8)).then((resp) {
expect(resp.statusCode, equals(201));
expect(resp.mockContent, equals("response"));
});
});

Expand Down
6 changes: 6 additions & 0 deletions test/services/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ getHandler() => "get_handler";
@app.Route("/handler_by_method", methods: const[app.POST])
postHandler() => "post_handler";

@app.Route("/change_status_code", statusCode: 201)
changeStatusCode() => "response";

@app.Group("/group")
class Group {

Expand All @@ -46,5 +49,8 @@ class Group {

@app.Route("/path/subpath")
subRoute() => "sub_route";

@app.Route("/change_status_code", statusCode: 201)
changeStatusCode() => "response";

}

0 comments on commit 748210f

Please sign in to comment.