From 3ec65251ef1d158dc2e3f02c9fa9b0b516bcd434 Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 13:04:55 +0100 Subject: [PATCH 01/10] add GroupRoute --- example/forum/forum.g.dart | 4 +- example/silly/main.dart | 11 ++++- example/silly/main.g.dart | 56 +++++++++++++++++----- lib/generator/hook/route_group/import.dart | 48 +++++++++++++++++++ lib/generator/phase/import.dart | 6 +-- lib/generator/writer/import.dart | 20 ++++++-- lib/src/annotations/import.dart | 6 +++ lib/src/serve/config.dart | 6 +-- lib/src/serve/server.dart | 6 +-- lib/testing/serve/serve.dart | 2 +- test/jaguar/group/main.dart | 4 +- test/jaguar/group/main.g.dart | 4 +- test/jaguar/route/route.g.dart | 4 +- test/jaguar/websocket/websocket.g.dart | 4 +- 14 files changed, 143 insertions(+), 38 deletions(-) create mode 100644 lib/generator/hook/route_group/import.dart diff --git a/example/forum/forum.g.dart b/example/forum/forum.g.dart index 8ca6039e..4568b978 100644 --- a/example/forum/forum.g.dart +++ b/example/forum/forum.g.dart @@ -7,7 +7,7 @@ part of example.forum; // Target: class ForumApi // ************************************************************************** -abstract class _$JaguarForumApi implements ApiInterface { +abstract class _$JaguarForumApi implements HandleRequestInterface { static const List _routes = const [ const Route('/user', methods: const ['GET'], @@ -58,7 +58,7 @@ abstract class _$JaguarForumApi implements ApiInterface { String decodeXwww(Map xwww); - Future handleApiRequest(HttpRequest request) async { + Future handleRequest(HttpRequest request) async { PathParams pathParams = new PathParams(); QueryParams queryParams = new QueryParams(request.uri.queryParameters); bool match = false; diff --git a/example/silly/main.dart b/example/silly/main.dart index 4146bb4b..769bba0a 100644 --- a/example/silly/main.dart +++ b/example/silly/main.dart @@ -7,8 +7,14 @@ import 'package:jaguar/jaguar.dart'; part 'main.g.dart'; +@RouteGroup(path: '/myGroup') +class MyGroup extends _$JaguarMyGroup { + @Get('/') + String get() => "get my group"; +} + /// Example of basic API class -@Api(path: '/api') +@RouteGroup() class ExampleApi extends _$JaguarExampleApi { int _pingCount = 0; @@ -32,6 +38,9 @@ class ExampleApi extends _$JaguarExampleApi { Future websocket(WebSocket ws) async { ws.listen((data) => ws.add(data)); } + + @Group(path: '/myGroup') + MyGroup myGroup = new MyGroup(); } Future main(List args) async { diff --git a/example/silly/main.g.dart b/example/silly/main.g.dart index be327f3c..4ab5d14d 100644 --- a/example/silly/main.g.dart +++ b/example/silly/main.g.dart @@ -3,11 +3,40 @@ part of jaguar.example.silly; // ************************************************************************** -// Generator: ApiGenerator +// Generator: RouteGroupGenerator +// Target: class MyGroup +// ************************************************************************** + +abstract class _$JaguarMyGroup implements HandleRequestInterface { + static const List _routes = const [const Get('/')]; + + String get(); + + Future handleRequest(HttpRequest request) async { + PathParams pathParams = new PathParams(); + bool match = false; + + match = _routes[0] + .match(request.uri.path, request.method, '/myGroup', pathParams); + if (match) { + String rRouteResponse; + rRouteResponse = get(); + request.response.statusCode = 200; + request.response.write(rRouteResponse.toString()); + await request.response.close(); + return true; + } + + return false; + } +} + +// ************************************************************************** +// Generator: RouteGroupGenerator // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements ApiInterface { +abstract class _$JaguarExampleApi implements HandleRequestInterface { static const List _routes = const [ const Route('/ping', methods: const ['GET']), const Put('/pong', @@ -17,6 +46,8 @@ abstract class _$JaguarExampleApi implements ApiInterface { const Ws('/ws') ]; + MyGroup get myGroup; + String ping(); String pong(); @@ -27,13 +58,12 @@ abstract class _$JaguarExampleApi implements ApiInterface { Future websocket(WebSocket ws); - Future handleApiRequest(HttpRequest request) async { + Future handleRequest(HttpRequest request) async { PathParams pathParams = new PathParams(); QueryParams queryParams = new QueryParams(request.uri.queryParameters); bool match = false; - match = - _routes[0].match(request.uri.path, request.method, '/api', pathParams); + match = _routes[0].match(request.uri.path, request.method, '', pathParams); if (match) { String rRouteResponse; rRouteResponse = ping(); @@ -43,8 +73,7 @@ abstract class _$JaguarExampleApi implements ApiInterface { return true; } - match = - _routes[1].match(request.uri.path, request.method, '/api', pathParams); + match = _routes[1].match(request.uri.path, request.method, '', pathParams); if (match) { String rRouteResponse; rRouteResponse = pong(); @@ -55,8 +84,7 @@ abstract class _$JaguarExampleApi implements ApiInterface { return true; } - match = - _routes[2].match(request.uri.path, request.method, '/api', pathParams); + match = _routes[2].match(request.uri.path, request.method, '', pathParams); if (match) { String rRouteResponse; rRouteResponse = echoPathParam( @@ -68,8 +96,7 @@ abstract class _$JaguarExampleApi implements ApiInterface { return true; } - match = - _routes[3].match(request.uri.path, request.method, '/api', pathParams); + match = _routes[3].match(request.uri.path, request.method, '', pathParams); if (match) { String rRouteResponse; rRouteResponse = echoQueryParam( @@ -81,8 +108,7 @@ abstract class _$JaguarExampleApi implements ApiInterface { return true; } - match = - _routes[4].match(request.uri.path, request.method, '/api', pathParams); + match = _routes[4].match(request.uri.path, request.method, '', pathParams); if (match) { dynamic rRouteResponse; WebSocket ws = await WebSocketTransformer.upgrade(request); @@ -92,6 +118,10 @@ abstract class _$JaguarExampleApi implements ApiInterface { return true; } + bool groupeResult; + groupeResult = await myGroup.handleRequest(request); + if (groupeResult) return true; + return false; } } diff --git a/lib/generator/hook/route_group/import.dart b/lib/generator/hook/route_group/import.dart new file mode 100644 index 00000000..64009211 --- /dev/null +++ b/lib/generator/hook/route_group/import.dart @@ -0,0 +1,48 @@ +library jaguar.generator.hook.route_group; + +import 'dart:async'; + +import 'package:analyzer/dart/element/element.dart'; +import 'package:build/src/builder/build_step.dart'; +import 'package:source_gen/source_gen.dart'; + +import 'package:jaguar/generator/writer/import.dart'; + +import 'package:jaguar/src/annotations/import.dart' as ant; + +import 'package:jaguar/generator/parser/import.dart'; + +class RouteGroupGenerator extends GeneratorForAnnotation { + const RouteGroupGenerator(); + + /// Generator + @override + Future generateForAnnotatedElement( + Element element, ant.RouteGroup routeGroup, BuildStep buildStep) async { + if (element is! ClassElement) { + throw new Exception("Api annotation can only be defined on a class."); + } + + ClassElement classElement = element; + String className = classElement.name; + + print("Generating for RouteGroup class $className ..."); + + Writer writer = new Writer(className, forGroupRoute: true); + + final String prefix = routeGroup.path; + + List interceptors = parseInterceptor(element); + + List exceptions = collectExceptionHandlers(element); + + List routes = + collectRoutes(classElement, prefix, interceptors, exceptions, []); + + writer.addGroups(collectGroups(classElement)); + + writer.addAllRoutes(routes); + + return writer.toString(); + } +} diff --git a/lib/generator/phase/import.dart b/lib/generator/phase/import.dart index 74f11153..c57bb0ea 100644 --- a/lib/generator/phase/import.dart +++ b/lib/generator/phase/import.dart @@ -7,6 +7,7 @@ import 'package:source_gen/source_gen.dart'; import 'package:yaml/yaml.dart'; import 'package:jaguar/generator/hook/api/import.dart'; +import 'package:jaguar/generator/hook/route_group/import.dart'; String getProjectName() { File pubspec = new File('./pubspec.yaml'); @@ -26,9 +27,8 @@ List getAnnotations() { Phase apisPhase(String projectName, List apis) { return new Phase() ..addAction( - new GeneratorBuilder(const [ - const ApiGenerator(), - ]), + new GeneratorBuilder( + const [const ApiGenerator(), const RouteGroupGenerator()]), new InputSet(projectName, apis)); } diff --git a/lib/generator/writer/import.dart b/lib/generator/writer/import.dart index 722ef788..feea6039 100644 --- a/lib/generator/writer/import.dart +++ b/lib/generator/writer/import.dart @@ -13,6 +13,7 @@ part 'default_response.dart'; class Writer { final String className; + final bool forGroupRoute; StringBuffer sb = new StringBuffer(); @@ -20,7 +21,7 @@ class Writer { final List groups = new List(); - Writer(this.className); + Writer(this.className, {this.forGroupRoute: false}); void addAllRoutes(List newRoutes) { routes.addAll(newRoutes); @@ -29,7 +30,8 @@ class Writer { void addGroups(List groupList) => groups.addAll(groupList); void generateClass() { - sb.writeln("abstract class _\$Jaguar$className implements ApiInterface {"); + sb.writeln( + "abstract class _\$Jaguar$className implements HandleRequestInterface {"); _writeRouteList(); sb.writeln(''); @@ -71,7 +73,7 @@ class Writer { } void _writeRequestHandler() { - sb.writeln("Future handleApiRequest(HttpRequest request) async {"); + sb.writeln("Future handleRequest(HttpRequest request) async {"); sb.writeln("PathParams pathParams = new PathParams();"); if (routes.any((RouteInfo route) => route.shouldKeepQueryParam)) { sb.writeln( @@ -96,6 +98,18 @@ class Writer { sb.writeln(""); } + if (forGroupRoute) { + if (groups.isNotEmpty) { + sb.write("bool groupeResult;"); + } + groups.forEach((GroupInfo groupeInfo) { + sb.write( + "groupeResult = await ${groupeInfo.name}.handleRequest(request);"); + sb.writeln("if (groupeResult) return true;"); + sb.writeln(""); + }); + } + sb.writeln("return false;"); sb.writeln("}"); sb.writeln(""); diff --git a/lib/src/annotations/import.dart b/lib/src/annotations/import.dart index e5bd2508..52ef012b 100644 --- a/lib/src/annotations/import.dart +++ b/lib/src/annotations/import.dart @@ -23,6 +23,12 @@ class Api { } } +class RouteGroup { + final String path; + + const RouteGroup({this.path: ''}); +} + ///An annotation to define an API group in API class class Group { ///Path prefix to the group diff --git a/lib/src/serve/config.dart b/lib/src/serve/config.dart index f96eaba0..7bc3ef4e 100644 --- a/lib/src/serve/config.dart +++ b/lib/src/serve/config.dart @@ -7,7 +7,7 @@ class Configuration { final SecurityContext securityContext; final bool multiThread; final bool log; - List apis = []; + List apis = []; Configuration( {this.address: "0.0.0.0", @@ -16,8 +16,8 @@ class Configuration { this.securityContext: null, this.log: false}); - void addApi(ApiInterface apiClass) { - apis.add(apiClass); + void addApi(HandleRequestInterface clazz) { + apis.add(clazz); } String get protocolStr => securityContext == null ? 'http' : 'https'; diff --git a/lib/src/serve/server.dart b/lib/src/serve/server.dart index 7a09aa4e..e20a2caf 100644 --- a/lib/src/serve/server.dart +++ b/lib/src/serve/server.dart @@ -1,7 +1,7 @@ part of jaguar.src.serve; -abstract class ApiInterface { - Future handleApiRequest(HttpRequest request); +abstract class HandleRequestInterface { + Future handleRequest(HttpRequest request); } // TODO(kleak): add doc @@ -34,7 +34,7 @@ class Jaguar { try { for (int i = 0; i < configuration.apis.length; i++) { var apiClass = configuration.apis[i]; - bool result = await apiClass.handleApiRequest(request); + bool result = await apiClass.handleRequest(request); if (result) break; } } catch (e, stack) { diff --git a/lib/testing/serve/serve.dart b/lib/testing/serve/serve.dart index cb2e34af..fa8d8324 100644 --- a/lib/testing/serve/serve.dart +++ b/lib/testing/serve/serve.dart @@ -17,7 +17,7 @@ class JaguarMock { try { for (int i = 0; i < config.apis.length; i++) { var apiClass = config.apis[i]; - bool result = await apiClass.handleApiRequest(request); + bool result = await apiClass.handleRequest(request); if (result) break; } } catch (e, stack) { diff --git a/test/jaguar/group/main.dart b/test/jaguar/group/main.dart index ce24583a..f94d4acc 100644 --- a/test/jaguar/group/main.dart +++ b/test/jaguar/group/main.dart @@ -19,9 +19,7 @@ class UserApi { } @Api(path: '/api') -class ExampleApi extends Object - with _$JaguarExampleApi - implements ApiInterface { +class ExampleApi extends Object with _$JaguarExampleApi { @Group(path: '/user') UserApi user = new UserApi(); diff --git a/test/jaguar/group/main.g.dart b/test/jaguar/group/main.g.dart index 81bce733..09ebe8bb 100644 --- a/test/jaguar/group/main.g.dart +++ b/test/jaguar/group/main.g.dart @@ -7,7 +7,7 @@ part of test.jaguar.group; // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements ApiInterface { +abstract class _$JaguarExampleApi implements HandleRequestInterface { static const List _routes = const [ const Route('/version', methods: const ['GET']), const Route('', methods: const ['GET']), @@ -21,7 +21,7 @@ abstract class _$JaguarExampleApi implements ApiInterface { String statusCode(); - Future handleApiRequest(HttpRequest request) async { + Future handleRequest(HttpRequest request) async { PathParams pathParams = new PathParams(); bool match = false; diff --git a/test/jaguar/route/route.g.dart b/test/jaguar/route/route.g.dart index 45754c27..3bffdd07 100644 --- a/test/jaguar/route/route.g.dart +++ b/test/jaguar/route/route.g.dart @@ -7,7 +7,7 @@ part of test.jaguar.route; // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements ApiInterface { +abstract class _$JaguarExampleApi implements HandleRequestInterface { static const List _routes = const [ const Route('/user', methods: const ['GET']), const Route('/statuscode', methods: const ['GET'], statusCode: 201), @@ -32,7 +32,7 @@ abstract class _$JaguarExampleApi implements ApiInterface { String inputCookies(List cookies); - Future handleApiRequest(HttpRequest request) async { + Future handleRequest(HttpRequest request) async { PathParams pathParams = new PathParams(); QueryParams queryParams = new QueryParams(request.uri.queryParameters); bool match = false; diff --git a/test/jaguar/websocket/websocket.g.dart b/test/jaguar/websocket/websocket.g.dart index f66b3ce8..bd196a99 100644 --- a/test/jaguar/websocket/websocket.g.dart +++ b/test/jaguar/websocket/websocket.g.dart @@ -7,12 +7,12 @@ part of test.jaguar.websocket; // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements ApiInterface { +abstract class _$JaguarExampleApi implements HandleRequestInterface { static const List _routes = const [const Ws('/ws')]; Future websocket(WebSocket ws); - Future handleApiRequest(HttpRequest request) async { + Future handleRequest(HttpRequest request) async { PathParams pathParams = new PathParams(); bool match = false; From c17d7c8ee5493be4dda4a621718f65c77e320831 Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 13:26:50 +0100 Subject: [PATCH 02/10] remove useless path param and refactor generate code --- example/silly/main.dart | 13 +++++++++++-- example/silly/main.g.dart | 33 ++++++++++++++++++++++++++++++++ lib/generator/writer/import.dart | 9 +++------ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/example/silly/main.dart b/example/silly/main.dart index 769bba0a..652bfca1 100644 --- a/example/silly/main.dart +++ b/example/silly/main.dart @@ -13,6 +13,12 @@ class MyGroup extends _$JaguarMyGroup { String get() => "get my group"; } +@RouteGroup(path: '/mySecondGroup') +class MySecondGroup extends _$JaguarMySecondGroup { + @Get('/') + String get() => "get mys second group"; +} + /// Example of basic API class @RouteGroup() class ExampleApi extends _$JaguarExampleApi { @@ -22,6 +28,9 @@ class ExampleApi extends _$JaguarExampleApi { @Route('/ping', methods: const ['GET']) String ping() => "You pinged me ${++_pingCount} times, silly!"; + @Group() + MyGroup myGroup = new MyGroup(); + /// Example of setting default status code and headers in response @Put('/pong', statusCode: 201, headers: const {"pong-header": "silly-pong"}) String pong() => "Your silly pongs have no effect on me!"; @@ -39,8 +48,8 @@ class ExampleApi extends _$JaguarExampleApi { ws.listen((data) => ws.add(data)); } - @Group(path: '/myGroup') - MyGroup myGroup = new MyGroup(); + @Group() + MySecondGroup mysGroup = new MySecondGroup(); } Future main(List args) async { diff --git a/example/silly/main.g.dart b/example/silly/main.g.dart index 4ab5d14d..eb7423ac 100644 --- a/example/silly/main.g.dart +++ b/example/silly/main.g.dart @@ -31,6 +31,35 @@ abstract class _$JaguarMyGroup implements HandleRequestInterface { } } +// ************************************************************************** +// Generator: RouteGroupGenerator +// Target: class MySecondGroup +// ************************************************************************** + +abstract class _$JaguarMySecondGroup implements HandleRequestInterface { + static const List _routes = const [const Get('/')]; + + String get(); + + Future handleRequest(HttpRequest request) async { + PathParams pathParams = new PathParams(); + bool match = false; + + match = _routes[0] + .match(request.uri.path, request.method, '/mySecondGroup', pathParams); + if (match) { + String rRouteResponse; + rRouteResponse = get(); + request.response.statusCode = 200; + request.response.write(rRouteResponse.toString()); + await request.response.close(); + return true; + } + + return false; + } +} + // ************************************************************************** // Generator: RouteGroupGenerator // Target: class ExampleApi @@ -47,6 +76,7 @@ abstract class _$JaguarExampleApi implements HandleRequestInterface { ]; MyGroup get myGroup; + MySecondGroup get mysGroup; String ping(); @@ -122,6 +152,9 @@ abstract class _$JaguarExampleApi implements HandleRequestInterface { groupeResult = await myGroup.handleRequest(request); if (groupeResult) return true; + groupeResult = await mysGroup.handleRequest(request); + if (groupeResult) return true; + return false; } } diff --git a/lib/generator/writer/import.dart b/lib/generator/writer/import.dart index feea6039..99ac498b 100644 --- a/lib/generator/writer/import.dart +++ b/lib/generator/writer/import.dart @@ -99,13 +99,10 @@ class Writer { } if (forGroupRoute) { - if (groups.isNotEmpty) { - sb.write("bool groupeResult;"); - } groups.forEach((GroupInfo groupeInfo) { - sb.write( - "groupeResult = await ${groupeInfo.name}.handleRequest(request);"); - sb.writeln("if (groupeResult) return true;"); + sb.writeln("if (await ${groupeInfo.name}.handleRequest(request)) {"); + sb.writeln("return true;"); + sb.writeln("}"); sb.writeln(""); }); } From 8035c8266e1f545d41e98823627f175325de6928 Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 13:28:11 +0100 Subject: [PATCH 03/10] add good generated code --- example/silly/main.g.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/example/silly/main.g.dart b/example/silly/main.g.dart index eb7423ac..2408db29 100644 --- a/example/silly/main.g.dart +++ b/example/silly/main.g.dart @@ -148,12 +148,13 @@ abstract class _$JaguarExampleApi implements HandleRequestInterface { return true; } - bool groupeResult; - groupeResult = await myGroup.handleRequest(request); - if (groupeResult) return true; + if (await myGroup.handleRequest(request)) { + return true; + } - groupeResult = await mysGroup.handleRequest(request); - if (groupeResult) return true; + if (await mysGroup.handleRequest(request)) { + return true; + } return false; } From 04ae6d4ff2acf956bc88567a4331cfba34b15704 Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 13:43:24 +0100 Subject: [PATCH 04/10] rebuild test/interceptor/param --- test/interceptor/param/param.g.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/interceptor/param/param.g.dart b/test/interceptor/param/param.g.dart index fbd1029c..16d4db54 100644 --- a/test/interceptor/param/param.g.dart +++ b/test/interceptor/param/param.g.dart @@ -7,14 +7,14 @@ part of test.interceptor.param; // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements ApiInterface { +abstract class _$JaguarExampleApi implements HandleRequestInterface { static const List _routes = const [ const Route('/user', methods: const ['GET']) ]; String getUser(String who); - Future handleApiRequest(HttpRequest request) async { + Future handleRequest(HttpRequest request) async { PathParams pathParams = new PathParams(); bool match = false; From 898aaf6d016c55da01f8e3ca5605d10480d2c44c Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 13:45:20 +0100 Subject: [PATCH 05/10] remove comment type --- lib/src/interceptors/import.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/interceptors/import.dart b/lib/src/interceptors/import.dart index d3c78334..211dc96b 100644 --- a/lib/src/interceptors/import.dart +++ b/lib/src/interceptors/import.dart @@ -113,7 +113,7 @@ class DecodeJson extends Interceptor { const DecodeJson({this.encoding: UTF8}); - Future pre /*Map|List*/ (HttpRequest request) async { + Future pre(HttpRequest request) async { String data = await getStringFromBody(request, encoding); return JSON.decode(data); } From 6d3a2a50805672b72221893ae84e08bbfdf907c1 Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 14:13:22 +0100 Subject: [PATCH 06/10] rename HandleRequestInterface and handleRequest method --- example/forum/forum.g.dart | 4 ++-- example/silly/main.dart | 8 ++++---- example/silly/main.g.dart | 16 ++++++++-------- lib/generator/writer/import.dart | 4 ++-- lib/src/serve/config.dart | 4 ++-- lib/src/serve/server.dart | 6 +++--- lib/testing/serve/serve.dart | 2 +- test/interceptor/param/param.g.dart | 4 ++-- test/jaguar/group/main.g.dart | 4 ++-- test/jaguar/route/route.g.dart | 4 ++-- test/jaguar/websocket/websocket.g.dart | 4 ++-- 11 files changed, 30 insertions(+), 30 deletions(-) diff --git a/example/forum/forum.g.dart b/example/forum/forum.g.dart index 890f31e1..e5a4b2c9 100644 --- a/example/forum/forum.g.dart +++ b/example/forum/forum.g.dart @@ -7,7 +7,7 @@ part of example.forum; // Target: class ForumApi // ************************************************************************** -abstract class _$JaguarForumApi implements HandleRequestInterface { +abstract class _$JaguarForumApi implements RequestHandler { static const List _routes = const [ const Route('/user', methods: const ['GET'], @@ -58,7 +58,7 @@ abstract class _$JaguarForumApi implements HandleRequestInterface { String decodeXwww(Map xwww); - Future handleRequest(HttpRequest request) async { + Future requestHandler(HttpRequest request) async { PathParams pathParams = new PathParams(); QueryParams queryParams = new QueryParams(request.uri.queryParameters); bool match = false; diff --git a/example/silly/main.dart b/example/silly/main.dart index 652bfca1..03b09f74 100644 --- a/example/silly/main.dart +++ b/example/silly/main.dart @@ -28,9 +28,6 @@ class ExampleApi extends _$JaguarExampleApi { @Route('/ping', methods: const ['GET']) String ping() => "You pinged me ${++_pingCount} times, silly!"; - @Group() - MyGroup myGroup = new MyGroup(); - /// Example of setting default status code and headers in response @Put('/pong', statusCode: 201, headers: const {"pong-header": "silly-pong"}) String pong() => "Your silly pongs have no effect on me!"; @@ -49,7 +46,10 @@ class ExampleApi extends _$JaguarExampleApi { } @Group() - MySecondGroup mysGroup = new MySecondGroup(); + MyGroup myGroup = new MyGroup(); + + @Group() + MySecondGroup mySecondGroup = new MySecondGroup(); } Future main(List args) async { diff --git a/example/silly/main.g.dart b/example/silly/main.g.dart index 3ba65466..3aa9cd50 100644 --- a/example/silly/main.g.dart +++ b/example/silly/main.g.dart @@ -7,12 +7,12 @@ part of jaguar.example.silly; // Target: class MyGroup // ************************************************************************** -abstract class _$JaguarMyGroup implements HandleRequestInterface { +abstract class _$JaguarMyGroup implements RequestHandler { static const List _routes = const [const Get('/')]; String get(); - Future handleRequest(HttpRequest request) async { + Future requestHandler(HttpRequest request) async { PathParams pathParams = new PathParams(); bool match = false; @@ -36,12 +36,12 @@ abstract class _$JaguarMyGroup implements HandleRequestInterface { // Target: class MySecondGroup // ************************************************************************** -abstract class _$JaguarMySecondGroup implements HandleRequestInterface { +abstract class _$JaguarMySecondGroup implements RequestHandler { static const List _routes = const [const Get('/')]; String get(); - Future handleRequest(HttpRequest request) async { + Future requestHandler(HttpRequest request) async { PathParams pathParams = new PathParams(); bool match = false; @@ -65,7 +65,7 @@ abstract class _$JaguarMySecondGroup implements HandleRequestInterface { // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements HandleRequestInterface { +abstract class _$JaguarExampleApi implements RequestHandler { static const List _routes = const [ const Route('/ping', methods: const ['GET']), const Put('/pong', @@ -76,7 +76,7 @@ abstract class _$JaguarExampleApi implements HandleRequestInterface { ]; MyGroup get myGroup; - MySecondGroup get mysGroup; + MySecondGroup get mySecondGroup; String ping(); @@ -88,7 +88,7 @@ abstract class _$JaguarExampleApi implements HandleRequestInterface { Future websocket(WebSocket ws); - Future handleRequest(HttpRequest request) async { + Future requestHandler(HttpRequest request) async { PathParams pathParams = new PathParams(); QueryParams queryParams = new QueryParams(request.uri.queryParameters); bool match = false; @@ -151,7 +151,7 @@ abstract class _$JaguarExampleApi implements HandleRequestInterface { return true; } - if (await mysGroup.handleRequest(request)) { + if (await mySecondGroup.handleRequest(request)) { return true; } diff --git a/lib/generator/writer/import.dart b/lib/generator/writer/import.dart index 8f179610..01cc7269 100644 --- a/lib/generator/writer/import.dart +++ b/lib/generator/writer/import.dart @@ -35,7 +35,7 @@ class Writer { void generateClass() { sb.writeln( - "abstract class _\$Jaguar$className implements HandleRequestInterface {"); + "abstract class _\$Jaguar$className implements RequestHandler {"); _writeRouteList(); sb.writeln(''); @@ -77,7 +77,7 @@ class Writer { } void _writeRequestHandler() { - sb.writeln("Future handleRequest(HttpRequest request) async {"); + sb.writeln("Future requestHandler(HttpRequest request) async {"); sb.writeln("PathParams pathParams = new PathParams();"); if (routes.any((RouteInfo route) => route.shouldKeepQueryParam)) { sb.writeln( diff --git a/lib/src/serve/config.dart b/lib/src/serve/config.dart index 7bc3ef4e..1edea550 100644 --- a/lib/src/serve/config.dart +++ b/lib/src/serve/config.dart @@ -7,7 +7,7 @@ class Configuration { final SecurityContext securityContext; final bool multiThread; final bool log; - List apis = []; + List apis = []; Configuration( {this.address: "0.0.0.0", @@ -16,7 +16,7 @@ class Configuration { this.securityContext: null, this.log: false}); - void addApi(HandleRequestInterface clazz) { + void addApi(RequestHandler clazz) { apis.add(clazz); } diff --git a/lib/src/serve/server.dart b/lib/src/serve/server.dart index e20a2caf..0b62c41a 100644 --- a/lib/src/serve/server.dart +++ b/lib/src/serve/server.dart @@ -1,7 +1,7 @@ part of jaguar.src.serve; -abstract class HandleRequestInterface { - Future handleRequest(HttpRequest request); +abstract class RequestHandler { + Future requestHandler(HttpRequest request); } // TODO(kleak): add doc @@ -34,7 +34,7 @@ class Jaguar { try { for (int i = 0; i < configuration.apis.length; i++) { var apiClass = configuration.apis[i]; - bool result = await apiClass.handleRequest(request); + bool result = await apiClass.requestHandler(request); if (result) break; } } catch (e, stack) { diff --git a/lib/testing/serve/serve.dart b/lib/testing/serve/serve.dart index fa8d8324..bf280225 100644 --- a/lib/testing/serve/serve.dart +++ b/lib/testing/serve/serve.dart @@ -17,7 +17,7 @@ class JaguarMock { try { for (int i = 0; i < config.apis.length; i++) { var apiClass = config.apis[i]; - bool result = await apiClass.handleRequest(request); + bool result = await apiClass.requestHandler(request); if (result) break; } } catch (e, stack) { diff --git a/test/interceptor/param/param.g.dart b/test/interceptor/param/param.g.dart index 16d4db54..336a4ca7 100644 --- a/test/interceptor/param/param.g.dart +++ b/test/interceptor/param/param.g.dart @@ -7,14 +7,14 @@ part of test.interceptor.param; // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements HandleRequestInterface { +abstract class _$JaguarExampleApi implements RequestHandler { static const List _routes = const [ const Route('/user', methods: const ['GET']) ]; String getUser(String who); - Future handleRequest(HttpRequest request) async { + Future requestHandler(HttpRequest request) async { PathParams pathParams = new PathParams(); bool match = false; diff --git a/test/jaguar/group/main.g.dart b/test/jaguar/group/main.g.dart index 09ebe8bb..fc55b42d 100644 --- a/test/jaguar/group/main.g.dart +++ b/test/jaguar/group/main.g.dart @@ -7,7 +7,7 @@ part of test.jaguar.group; // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements HandleRequestInterface { +abstract class _$JaguarExampleApi implements RequestHandler { static const List _routes = const [ const Route('/version', methods: const ['GET']), const Route('', methods: const ['GET']), @@ -21,7 +21,7 @@ abstract class _$JaguarExampleApi implements HandleRequestInterface { String statusCode(); - Future handleRequest(HttpRequest request) async { + Future requestHandler(HttpRequest request) async { PathParams pathParams = new PathParams(); bool match = false; diff --git a/test/jaguar/route/route.g.dart b/test/jaguar/route/route.g.dart index 3bffdd07..a5858027 100644 --- a/test/jaguar/route/route.g.dart +++ b/test/jaguar/route/route.g.dart @@ -7,7 +7,7 @@ part of test.jaguar.route; // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements HandleRequestInterface { +abstract class _$JaguarExampleApi implements RequestHandler { static const List _routes = const [ const Route('/user', methods: const ['GET']), const Route('/statuscode', methods: const ['GET'], statusCode: 201), @@ -32,7 +32,7 @@ abstract class _$JaguarExampleApi implements HandleRequestInterface { String inputCookies(List cookies); - Future handleRequest(HttpRequest request) async { + Future requestHandler(HttpRequest request) async { PathParams pathParams = new PathParams(); QueryParams queryParams = new QueryParams(request.uri.queryParameters); bool match = false; diff --git a/test/jaguar/websocket/websocket.g.dart b/test/jaguar/websocket/websocket.g.dart index 232c7770..f4b91bac 100644 --- a/test/jaguar/websocket/websocket.g.dart +++ b/test/jaguar/websocket/websocket.g.dart @@ -7,12 +7,12 @@ part of test.jaguar.websocket; // Target: class ExampleApi // ************************************************************************** -abstract class _$JaguarExampleApi implements HandleRequestInterface { +abstract class _$JaguarExampleApi implements RequestHandler { static const List _routes = const [const Ws('/ws')]; Future websocket(WebSocket ws); - Future handleRequest(HttpRequest request) async { + Future requestHandler(HttpRequest request) async { PathParams pathParams = new PathParams(); bool match = false; From 286e27f9d049414f4cf1c3b948c3b7b94d7ba9ce Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 18:02:22 +0100 Subject: [PATCH 07/10] same behavior for Group for Api and RouteGroup --- example/forum/forum.g.dart | 27 ++++++------ example/silly/main.dart | 8 ++-- example/silly/main.g.dart | 36 +++++++++------- lib/generator/hook/api/import.dart | 2 +- lib/generator/writer/import.dart | 9 ++-- lib/src/annotations/import.dart | 2 +- lib/src/serve/server.dart | 2 +- test/interceptor/param/param.g.dart | 5 ++- test/jaguar/group/main.g.dart | 57 ++------------------------ test/jaguar/route/route.g.dart | 17 ++++---- test/jaguar/websocket/websocket.g.dart | 5 ++- 11 files changed, 68 insertions(+), 102 deletions(-) diff --git a/example/forum/forum.g.dart b/example/forum/forum.g.dart index e5a4b2c9..1a89e5f5 100644 --- a/example/forum/forum.g.dart +++ b/example/forum/forum.g.dart @@ -58,13 +58,14 @@ abstract class _$JaguarForumApi implements RequestHandler { String decodeXwww(Map xwww); - Future requestHandler(HttpRequest request) async { + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += '/api'; PathParams pathParams = new PathParams(); QueryParams queryParams = new QueryParams(request.uri.queryParameters); bool match = false; match = - _routes[0].match(request.uri.path, request.method, '/api', pathParams); + _routes[0].match(request.uri.path, request.method, prefix, pathParams); if (match) { MongoDb iMongoDbTest = new MongoDb( 'test', @@ -94,7 +95,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[1].match(request.uri.path, request.method, '/api', pathParams); + _routes[1].match(request.uri.path, request.method, prefix, pathParams); if (match) { MongoDb iMongoDbAdmin = new MongoDb('admin', id: 'Admin', state: MongoDb.createState()); @@ -113,7 +114,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[2].match(request.uri.path, request.method, '/api', pathParams); + _routes[2].match(request.uri.path, request.method, prefix, pathParams); if (match) { MongoDb iMongoDbAdmin = new MongoDb('admin', id: 'Admin', state: MongoDb.createState()); @@ -139,7 +140,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[3].match(request.uri.path, request.method, '/api', pathParams); + _routes[3].match(request.uri.path, request.method, prefix, pathParams); if (match) { MongoDb iMongoDbAdmin = new MongoDb('admin', id: 'Admin', state: MongoDb.createState()); @@ -164,7 +165,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[4].match(request.uri.path, request.method, '/api', pathParams); + _routes[4].match(request.uri.path, request.method, prefix, pathParams); if (match) { MongoDb iMongoDbAdmin = new MongoDb('admin', id: 'Admin', state: MongoDb.createState()); @@ -196,7 +197,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[5].match(request.uri.path, request.method, '/api', pathParams); + _routes[5].match(request.uri.path, request.method, prefix, pathParams); if (match) { MongoDb iMongoDbAdmin = new MongoDb('admin', id: 'Admin', state: MongoDb.createState()); @@ -235,7 +236,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[6].match(request.uri.path, request.method, '/api', pathParams); + _routes[6].match(request.uri.path, request.method, prefix, pathParams); if (match) { MongoDb iMongoDbAdmin = new MongoDb('admin', id: 'Admin', state: MongoDb.createState()); @@ -270,7 +271,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[7].match(request.uri.path, request.method, '/api', pathParams); + _routes[7].match(request.uri.path, request.method, prefix, pathParams); if (match) { MongoDb iMongoDbAdmin = new MongoDb('admin', id: 'Admin', state: MongoDb.createState()); @@ -293,7 +294,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[8].match(request.uri.path, request.method, '/api', pathParams); + _routes[8].match(request.uri.path, request.method, prefix, pathParams); if (match) { MongoDb iMongoDbAdmin = new MongoDb('admin', id: 'Admin', state: MongoDb.createState()); @@ -316,7 +317,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[9].match(request.uri.path, request.method, '/api', pathParams); + _routes[9].match(request.uri.path, request.method, prefix, pathParams); if (match) { DecodeJson iDecodeJson = new DecodeJson(); dynamic rDecodeJson = await iDecodeJson.pre( @@ -333,7 +334,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[10].match(request.uri.path, request.method, '/api', pathParams); + _routes[10].match(request.uri.path, request.method, prefix, pathParams); if (match) { DecodeFormData iDecodeFormData = new DecodeFormData(); Map rDecodeFormData = await iDecodeFormData.pre( @@ -350,7 +351,7 @@ abstract class _$JaguarForumApi implements RequestHandler { } match = - _routes[11].match(request.uri.path, request.method, '/api', pathParams); + _routes[11].match(request.uri.path, request.method, prefix, pathParams); if (match) { DecodeUrlEncodedForm iDecodeUrlEncodedForm = new DecodeUrlEncodedForm(); Map rDecodeUrlEncodedForm = diff --git a/example/silly/main.dart b/example/silly/main.dart index 03b09f74..42b8ab45 100644 --- a/example/silly/main.dart +++ b/example/silly/main.dart @@ -8,20 +8,20 @@ import 'package:jaguar/jaguar.dart'; part 'main.g.dart'; @RouteGroup(path: '/myGroup') -class MyGroup extends _$JaguarMyGroup { +class MyGroup extends Object with _$JaguarMyGroup { @Get('/') String get() => "get my group"; } @RouteGroup(path: '/mySecondGroup') -class MySecondGroup extends _$JaguarMySecondGroup { +class MySecondGroup extends Object with _$JaguarMySecondGroup { @Get('/') String get() => "get mys second group"; } /// Example of basic API class -@RouteGroup() -class ExampleApi extends _$JaguarExampleApi { +@RouteGroup(path: '/api') +class ExampleApi extends Object with _$JaguarExampleApi { int _pingCount = 0; /// Example of basic route diff --git a/example/silly/main.g.dart b/example/silly/main.g.dart index 3aa9cd50..d9ef1901 100644 --- a/example/silly/main.g.dart +++ b/example/silly/main.g.dart @@ -12,12 +12,13 @@ abstract class _$JaguarMyGroup implements RequestHandler { String get(); - Future requestHandler(HttpRequest request) async { + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += '/myGroup'; PathParams pathParams = new PathParams(); bool match = false; - match = _routes[0] - .match(request.uri.path, request.method, '/myGroup', pathParams); + match = + _routes[0].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = get(); @@ -41,12 +42,13 @@ abstract class _$JaguarMySecondGroup implements RequestHandler { String get(); - Future requestHandler(HttpRequest request) async { + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += '/mySecondGroup'; PathParams pathParams = new PathParams(); bool match = false; - match = _routes[0] - .match(request.uri.path, request.method, '/mySecondGroup', pathParams); + match = + _routes[0].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = get(); @@ -88,12 +90,14 @@ abstract class _$JaguarExampleApi implements RequestHandler { Future websocket(WebSocket ws); - Future requestHandler(HttpRequest request) async { + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += '/api'; PathParams pathParams = new PathParams(); QueryParams queryParams = new QueryParams(request.uri.queryParameters); bool match = false; - match = _routes[0].match(request.uri.path, request.method, '', pathParams); + match = + _routes[0].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = ping(); @@ -103,7 +107,8 @@ abstract class _$JaguarExampleApi implements RequestHandler { return true; } - match = _routes[1].match(request.uri.path, request.method, '', pathParams); + match = + _routes[1].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = pong(); @@ -114,7 +119,8 @@ abstract class _$JaguarExampleApi implements RequestHandler { return true; } - match = _routes[2].match(request.uri.path, request.method, '', pathParams); + match = + _routes[2].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = echoPathParam( @@ -126,7 +132,8 @@ abstract class _$JaguarExampleApi implements RequestHandler { return true; } - match = _routes[3].match(request.uri.path, request.method, '', pathParams); + match = + _routes[3].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = echoQueryParam( @@ -138,7 +145,8 @@ abstract class _$JaguarExampleApi implements RequestHandler { return true; } - match = _routes[4].match(request.uri.path, request.method, '', pathParams); + match = + _routes[4].match(request.uri.path, request.method, prefix, pathParams); if (match) { WebSocket ws = await WebSocketTransformer.upgrade(request); await websocket( @@ -147,11 +155,11 @@ abstract class _$JaguarExampleApi implements RequestHandler { return true; } - if (await myGroup.handleRequest(request)) { + if (await myGroup.requestHandler(request, prefix: prefix + '')) { return true; } - if (await mySecondGroup.handleRequest(request)) { + if (await mySecondGroup.requestHandler(request, prefix: prefix + '')) { return true; } diff --git a/lib/generator/hook/api/import.dart b/lib/generator/hook/api/import.dart index a1859346..b3ef292b 100644 --- a/lib/generator/hook/api/import.dart +++ b/lib/generator/hook/api/import.dart @@ -37,7 +37,7 @@ class ApiGenerator extends GeneratorForAnnotation { List exceptions = collectExceptionHandlers(element); List routes = - collectAllRoutes(classElement, prefix, interceptors, exceptions, []); + collectRoutes(classElement, prefix, interceptors, exceptions, []); writer.addGroups(collectGroups(classElement)); diff --git a/lib/generator/writer/import.dart b/lib/generator/writer/import.dart index 01cc7269..475a74d0 100644 --- a/lib/generator/writer/import.dart +++ b/lib/generator/writer/import.dart @@ -77,7 +77,9 @@ class Writer { } void _writeRequestHandler() { - sb.writeln("Future requestHandler(HttpRequest request) async {"); + sb.writeln( + "Future requestHandler(HttpRequest request, {String prefix: ''}) async {"); + sb.write("prefix += '${routes.first.pathPrefix}';"); sb.writeln("PathParams pathParams = new PathParams();"); if (routes.any((RouteInfo route) => route.shouldKeepQueryParam)) { sb.writeln( @@ -88,7 +90,7 @@ class Writer { for (int i = 0; i < routes.length; i++) { sb.writeln( - "match = _routes[$i].match(request.uri.path, request.method, '${routes[i].pathPrefix}',pathParams);"); + "match = _routes[$i].match(request.uri.path, request.method, prefix, pathParams);"); sb.writeln("if (match) {"); _writePreInterceptors(routes[i]); @@ -104,7 +106,8 @@ class Writer { if (forGroupRoute) { groups.forEach((GroupInfo groupeInfo) { - sb.writeln("if (await ${groupeInfo.name}.handleRequest(request)) {"); + sb.writeln( + "if (await ${groupeInfo.name}.requestHandler(request, prefix: prefix + '${groupeInfo.group.path}')) {"); sb.writeln("return true;"); sb.writeln("}"); sb.writeln(""); diff --git a/lib/src/annotations/import.dart b/lib/src/annotations/import.dart index 52ef012b..f6f78b4a 100644 --- a/lib/src/annotations/import.dart +++ b/lib/src/annotations/import.dart @@ -34,7 +34,7 @@ class Group { ///Path prefix to the group final String path; - const Group({this.path}); + const Group({this.path: ''}); } class ExceptionHandler { diff --git a/lib/src/serve/server.dart b/lib/src/serve/server.dart index 0b62c41a..56be76a1 100644 --- a/lib/src/serve/server.dart +++ b/lib/src/serve/server.dart @@ -1,7 +1,7 @@ part of jaguar.src.serve; abstract class RequestHandler { - Future requestHandler(HttpRequest request); + Future requestHandler(HttpRequest request, {String prefix}); } // TODO(kleak): add doc diff --git a/test/interceptor/param/param.g.dart b/test/interceptor/param/param.g.dart index 336a4ca7..ab9476d0 100644 --- a/test/interceptor/param/param.g.dart +++ b/test/interceptor/param/param.g.dart @@ -14,12 +14,13 @@ abstract class _$JaguarExampleApi implements RequestHandler { String getUser(String who); - Future requestHandler(HttpRequest request) async { + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += '/api'; PathParams pathParams = new PathParams(); bool match = false; match = - _routes[0].match(request.uri.path, request.method, '/api', pathParams); + _routes[0].match(request.uri.path, request.method, prefix, pathParams); if (match) { WithParam iWithParam = new WithParam( params: const {#checker: CheckerImpl}, diff --git a/test/jaguar/group/main.g.dart b/test/jaguar/group/main.g.dart index fc55b42d..3a369256 100644 --- a/test/jaguar/group/main.g.dart +++ b/test/jaguar/group/main.g.dart @@ -9,11 +9,7 @@ part of test.jaguar.group; abstract class _$JaguarExampleApi implements RequestHandler { static const List _routes = const [ - const Route('/version', methods: const ['GET']), - const Route('', methods: const ['GET']), - const Route('/statuscode', methods: const ['GET'], statusCode: 201), - const Route('', methods: const ['GET']), - const Route('/some/:param1', methods: const ['POST']) + const Route('/version', methods: const ['GET']) ]; UserApi get user; @@ -21,12 +17,13 @@ abstract class _$JaguarExampleApi implements RequestHandler { String statusCode(); - Future requestHandler(HttpRequest request) async { + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += '/api'; PathParams pathParams = new PathParams(); bool match = false; match = - _routes[0].match(request.uri.path, request.method, '/api', pathParams); + _routes[0].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = statusCode(); @@ -36,52 +33,6 @@ abstract class _$JaguarExampleApi implements RequestHandler { return true; } - match = _routes[1] - .match(request.uri.path, request.method, '/api/user', pathParams); - if (match) { - String rRouteResponse; - rRouteResponse = user.getUser(); - request.response.statusCode = 200; - request.response.write(rRouteResponse.toString()); - await request.response.close(); - return true; - } - - match = _routes[2] - .match(request.uri.path, request.method, '/api/user', pathParams); - if (match) { - String rRouteResponse; - rRouteResponse = user.statusCode(); - request.response.statusCode = 201; - request.response.write(rRouteResponse.toString()); - await request.response.close(); - return true; - } - - match = _routes[3] - .match(request.uri.path, request.method, '/api/book', pathParams); - if (match) { - String rRouteResponse; - rRouteResponse = book.getBook(); - request.response.statusCode = 200; - request.response.write(rRouteResponse.toString()); - await request.response.close(); - return true; - } - - match = _routes[4] - .match(request.uri.path, request.method, '/api/book', pathParams); - if (match) { - String rRouteResponse; - rRouteResponse = book.some( - (pathParams.getField('param1')), - ); - request.response.statusCode = 200; - request.response.write(rRouteResponse.toString()); - await request.response.close(); - return true; - } - return false; } } diff --git a/test/jaguar/route/route.g.dart b/test/jaguar/route/route.g.dart index a5858027..f3917eff 100644 --- a/test/jaguar/route/route.g.dart +++ b/test/jaguar/route/route.g.dart @@ -32,13 +32,14 @@ abstract class _$JaguarExampleApi implements RequestHandler { String inputCookies(List cookies); - Future requestHandler(HttpRequest request) async { + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += '/api'; PathParams pathParams = new PathParams(); QueryParams queryParams = new QueryParams(request.uri.queryParameters); bool match = false; match = - _routes[0].match(request.uri.path, request.method, '/api', pathParams); + _routes[0].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = getUser(); @@ -49,7 +50,7 @@ abstract class _$JaguarExampleApi implements RequestHandler { } match = - _routes[1].match(request.uri.path, request.method, '/api', pathParams); + _routes[1].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = statusCode(); @@ -60,7 +61,7 @@ abstract class _$JaguarExampleApi implements RequestHandler { } match = - _routes[2].match(request.uri.path, request.method, '/api', pathParams); + _routes[2].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = paramAndQuery( @@ -74,7 +75,7 @@ abstract class _$JaguarExampleApi implements RequestHandler { } match = - _routes[3].match(request.uri.path, request.method, '/api', pathParams); + _routes[3].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = inputHeader( @@ -87,7 +88,7 @@ abstract class _$JaguarExampleApi implements RequestHandler { } match = - _routes[4].match(request.uri.path, request.method, '/api', pathParams); + _routes[4].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = inputHeaders( @@ -100,7 +101,7 @@ abstract class _$JaguarExampleApi implements RequestHandler { } match = - _routes[5].match(request.uri.path, request.method, '/api', pathParams); + _routes[5].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = inputCookie( @@ -115,7 +116,7 @@ abstract class _$JaguarExampleApi implements RequestHandler { } match = - _routes[6].match(request.uri.path, request.method, '/api', pathParams); + _routes[6].match(request.uri.path, request.method, prefix, pathParams); if (match) { String rRouteResponse; rRouteResponse = inputCookies( diff --git a/test/jaguar/websocket/websocket.g.dart b/test/jaguar/websocket/websocket.g.dart index f4b91bac..dca75699 100644 --- a/test/jaguar/websocket/websocket.g.dart +++ b/test/jaguar/websocket/websocket.g.dart @@ -12,12 +12,13 @@ abstract class _$JaguarExampleApi implements RequestHandler { Future websocket(WebSocket ws); - Future requestHandler(HttpRequest request) async { + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += '/api'; PathParams pathParams = new PathParams(); bool match = false; match = - _routes[0].match(request.uri.path, request.method, '/api', pathParams); + _routes[0].match(request.uri.path, request.method, prefix, pathParams); if (match) { WebSocket ws = await WebSocketTransformer.upgrade(request); await websocket( From 9060aff043394fe325bd8d250c2865188852ec34 Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 18:08:47 +0100 Subject: [PATCH 08/10] fix test --- jaguar.yaml | 1 + lib/generator/writer/import.dart | 18 +++++------ test/jaguar/group/book.dart | 5 ++- test/jaguar/group/book.g.dart | 51 ++++++++++++++++++++++++++++++ test/jaguar/group/main.dart | 3 +- test/jaguar/group/main.g.dart | 54 ++++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 test/jaguar/group/book.g.dart diff --git a/jaguar.yaml b/jaguar.yaml index 661998da..4c9c2b55 100644 --- a/jaguar.yaml +++ b/jaguar.yaml @@ -3,5 +3,6 @@ apis: - 'example/silly/main.dart' - 'test/jaguar/route/route.dart' - 'test/jaguar/group/main.dart' +- 'test/jaguar/group/book.dart' - 'test/jaguar/websocket/websocket.dart' - 'test/interceptor/param/param.dart' diff --git a/lib/generator/writer/import.dart b/lib/generator/writer/import.dart index 475a74d0..b3566264 100644 --- a/lib/generator/writer/import.dart +++ b/lib/generator/writer/import.dart @@ -104,15 +104,15 @@ class Writer { sb.writeln(""); } - if (forGroupRoute) { - groups.forEach((GroupInfo groupeInfo) { - sb.writeln( - "if (await ${groupeInfo.name}.requestHandler(request, prefix: prefix + '${groupeInfo.group.path}')) {"); - sb.writeln("return true;"); - sb.writeln("}"); - sb.writeln(""); - }); - } + // if (forGroupRoute) { + groups.forEach((GroupInfo groupeInfo) { + sb.writeln( + "if (await ${groupeInfo.name}.requestHandler(request, prefix: prefix + '${groupeInfo.group.path}')) {"); + sb.writeln("return true;"); + sb.writeln("}"); + sb.writeln(""); + }); + // } sb.writeln("return false;"); sb.writeln("}"); diff --git a/test/jaguar/group/book.dart b/test/jaguar/group/book.dart index 5e877756..a1dd4572 100644 --- a/test/jaguar/group/book.dart +++ b/test/jaguar/group/book.dart @@ -2,7 +2,10 @@ library test.jaguar.group.normal.book; import 'package:jaguar/jaguar.dart'; -class BookApi { +part 'book.g.dart'; + +@RouteGroup() +class BookApi extends Object with _$JaguarBookApi { @Route('', methods: const ['GET']) String getBook() => 'Get book'; diff --git a/test/jaguar/group/book.g.dart b/test/jaguar/group/book.g.dart new file mode 100644 index 00000000..94472ca7 --- /dev/null +++ b/test/jaguar/group/book.g.dart @@ -0,0 +1,51 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of test.jaguar.group.normal.book; + +// ************************************************************************** +// Generator: RouteGroupGenerator +// Target: class BookApi +// ************************************************************************** + +abstract class _$JaguarBookApi implements RequestHandler { + static const List _routes = const [ + const Route('', methods: const ['GET']), + const Route('/some/:param1', methods: const ['POST']) + ]; + + String getBook(); + + String some(String param1); + + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += ''; + PathParams pathParams = new PathParams(); + bool match = false; + + match = + _routes[0].match(request.uri.path, request.method, prefix, pathParams); + if (match) { + String rRouteResponse; + rRouteResponse = getBook(); + request.response.statusCode = 200; + request.response.write(rRouteResponse.toString()); + await request.response.close(); + return true; + } + + match = + _routes[1].match(request.uri.path, request.method, prefix, pathParams); + if (match) { + String rRouteResponse; + rRouteResponse = some( + (pathParams.getField('param1')), + ); + request.response.statusCode = 200; + request.response.write(rRouteResponse.toString()); + await request.response.close(); + return true; + } + + return false; + } +} diff --git a/test/jaguar/group/main.dart b/test/jaguar/group/main.dart index 3718e7c1..106d083b 100644 --- a/test/jaguar/group/main.dart +++ b/test/jaguar/group/main.dart @@ -10,7 +10,8 @@ import 'book.dart'; part 'main.g.dart'; -class UserApi { +@RouteGroup() +class UserApi extends Object with _$JaguarUserApi { @Route('', methods: const ['GET']) String getUser() => 'Get user'; diff --git a/test/jaguar/group/main.g.dart b/test/jaguar/group/main.g.dart index 3a369256..d6451d0d 100644 --- a/test/jaguar/group/main.g.dart +++ b/test/jaguar/group/main.g.dart @@ -2,6 +2,52 @@ part of test.jaguar.group; +// ************************************************************************** +// Generator: RouteGroupGenerator +// Target: class UserApi +// ************************************************************************** + +abstract class _$JaguarUserApi implements RequestHandler { + static const List _routes = const [ + const Route('', methods: const ['GET']), + const Route('/statuscode', methods: const ['GET'], statusCode: 201) + ]; + + String getUser(); + + String statusCode(); + + Future requestHandler(HttpRequest request, {String prefix: ''}) async { + prefix += ''; + PathParams pathParams = new PathParams(); + bool match = false; + + match = + _routes[0].match(request.uri.path, request.method, prefix, pathParams); + if (match) { + String rRouteResponse; + rRouteResponse = getUser(); + request.response.statusCode = 200; + request.response.write(rRouteResponse.toString()); + await request.response.close(); + return true; + } + + match = + _routes[1].match(request.uri.path, request.method, prefix, pathParams); + if (match) { + String rRouteResponse; + rRouteResponse = statusCode(); + request.response.statusCode = 201; + request.response.write(rRouteResponse.toString()); + await request.response.close(); + return true; + } + + return false; + } +} + // ************************************************************************** // Generator: ApiGenerator // Target: class ExampleApi @@ -33,6 +79,14 @@ abstract class _$JaguarExampleApi implements RequestHandler { return true; } + if (await user.requestHandler(request, prefix: prefix + '/user')) { + return true; + } + + if (await book.requestHandler(request, prefix: prefix + '/book')) { + return true; + } + return false; } } From ae954bf56c2638045f95a5b9b442a61768bbc788 Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 18:13:03 +0100 Subject: [PATCH 09/10] fix prefix generation --- example/silly/main.g.dart | 4 ++-- lib/generator/writer/import.dart | 11 ++++++++--- test/jaguar/group/book.g.dart | 1 - test/jaguar/group/main.g.dart | 1 - 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/example/silly/main.g.dart b/example/silly/main.g.dart index d9ef1901..60c19661 100644 --- a/example/silly/main.g.dart +++ b/example/silly/main.g.dart @@ -155,11 +155,11 @@ abstract class _$JaguarExampleApi implements RequestHandler { return true; } - if (await myGroup.requestHandler(request, prefix: prefix + '')) { + if (await myGroup.requestHandler(request)) { return true; } - if (await mySecondGroup.requestHandler(request, prefix: prefix + '')) { + if (await mySecondGroup.requestHandler(request)) { return true; } diff --git a/lib/generator/writer/import.dart b/lib/generator/writer/import.dart index b3566264..9d0d2412 100644 --- a/lib/generator/writer/import.dart +++ b/lib/generator/writer/import.dart @@ -79,7 +79,9 @@ class Writer { void _writeRequestHandler() { sb.writeln( "Future requestHandler(HttpRequest request, {String prefix: ''}) async {"); - sb.write("prefix += '${routes.first.pathPrefix}';"); + if (routes.first.pathPrefix.isNotEmpty) { + sb.write("prefix += '${routes.first.pathPrefix}';"); + } sb.writeln("PathParams pathParams = new PathParams();"); if (routes.any((RouteInfo route) => route.shouldKeepQueryParam)) { sb.writeln( @@ -106,8 +108,11 @@ class Writer { // if (forGroupRoute) { groups.forEach((GroupInfo groupeInfo) { - sb.writeln( - "if (await ${groupeInfo.name}.requestHandler(request, prefix: prefix + '${groupeInfo.group.path}')) {"); + sb.write("if (await ${groupeInfo.name}.requestHandler(request"); + if (groupeInfo.group.path.isNotEmpty) { + sb.write(",prefix: prefix + '${groupeInfo.group.path}'"); + } + sb.write(")) {"); sb.writeln("return true;"); sb.writeln("}"); sb.writeln(""); diff --git a/test/jaguar/group/book.g.dart b/test/jaguar/group/book.g.dart index 94472ca7..be49616a 100644 --- a/test/jaguar/group/book.g.dart +++ b/test/jaguar/group/book.g.dart @@ -18,7 +18,6 @@ abstract class _$JaguarBookApi implements RequestHandler { String some(String param1); Future requestHandler(HttpRequest request, {String prefix: ''}) async { - prefix += ''; PathParams pathParams = new PathParams(); bool match = false; diff --git a/test/jaguar/group/main.g.dart b/test/jaguar/group/main.g.dart index d6451d0d..3c025166 100644 --- a/test/jaguar/group/main.g.dart +++ b/test/jaguar/group/main.g.dart @@ -18,7 +18,6 @@ abstract class _$JaguarUserApi implements RequestHandler { String statusCode(); Future requestHandler(HttpRequest request, {String prefix: ''}) async { - prefix += ''; PathParams pathParams = new PathParams(); bool match = false; From 76e100229a57bbf66272e30d98cd73f47963911c Mon Sep 17 00:00:00 2001 From: Kleak Date: Wed, 9 Nov 2016 18:15:09 +0100 Subject: [PATCH 10/10] remove useless comment --- lib/generator/writer/import.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/generator/writer/import.dart b/lib/generator/writer/import.dart index 9d0d2412..d79499d9 100644 --- a/lib/generator/writer/import.dart +++ b/lib/generator/writer/import.dart @@ -106,7 +106,6 @@ class Writer { sb.writeln(""); } - // if (forGroupRoute) { groups.forEach((GroupInfo groupeInfo) { sb.write("if (await ${groupeInfo.name}.requestHandler(request"); if (groupeInfo.group.path.isNotEmpty) { @@ -117,7 +116,6 @@ class Writer { sb.writeln("}"); sb.writeln(""); }); - // } sb.writeln("return false;"); sb.writeln("}");