项目作者: jaguar-examples

项目描述 :
Boilerplate for jaguar_reflect
高级语言: Dart
项目地址: git://github.com/jaguar-examples/boilerplate.git
创建时间: 2017-02-07T21:06:34Z
项目社区:https://github.com/jaguar-examples/boilerplate

开源协议:BSD 3-Clause "New" or "Revised" License

下载


boilerplate for Jaguar projects

This project contains a simple example shows how to add routes to Jaguar using Controller classes.

The repository can be found at: https://github.com/jaguar-examples/boilerplate.
One would typically want to clone it and edit it in IntelliJ IDE.

Server description

It exposes four routes for demonstration purposes:

  1. A simple GET route
  2. A simple POST route that uses query parameters
  3. A simple JSON GET route
  4. A simple JSON POST route that has JSON body
  1. @Controller(path: '/api')
  2. class ExampleApi {
  3. // A simple get route
  4. @Get(path: '/version')
  5. double version(_) => 0.1;
  6. // A simple post route
  7. @Post(path: '/add')
  8. int add(Context ctx) => ctx.query.getInt('a') + ctx.query.getInt('b');
  9. // A simple get JSON route
  10. @GetJson(path: '/info')
  11. Map info(_) => {
  12. 'server': 'Jaguar',
  13. 'motto': 'Simple. Fast. Flexible. Extensible.',
  14. };
  15. // A simple post JSON route
  16. @PostJson(path: '/sub')
  17. Future<Map> sub(Context ctx) async {
  18. Map body = await ctx.bodyAsJsonMap();
  19. return {'result': body['a'] - body['b']};
  20. }
  21. }

Client description

Contains code to access the example server.

  1. Future<Null> execVersion() async {
  2. String url = "http://$kHostname:$kPort/api/version";
  3. http.Response resp = await _client.get(url);
  4. print(resp.body);
  5. }
  6. Future<Null> execAdd() async {
  7. http.Response resp = await _client.post(
  8. new Uri.http('$kHostname:$kPort', '/api/add', {'a': '5', 'b': '20'}));
  9. print(resp.body);
  10. }
  11. Future<Null> execInfo() async {
  12. String url = "http://$kHostname:$kPort/api/info";
  13. http.Response resp = await _client.get(url);
  14. print(resp.body);
  15. }
  16. Future<Null> execSubtract() async {
  17. String url = "http://$kHostname:$kPort/api/sub";
  18. http.Response resp = await _client.post(url, body: '{"a": 10, "b": 5}');
  19. print(resp.body);
  20. }
  21. main() async {
  22. await execVersion();
  23. await execAdd();
  24. await execInfo();
  25. await execSubtract();
  26. }