redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

Only first group's routes registered #185

Open mnordine opened 7 years ago

mnordine commented 7 years ago

See https://gitlab.com/mark-nordine/redstone-bug

server.dart:

import 'package:redstone/redstone.dart';
import '../lib/my_lib.dart'; // ignore: unused_import

main()
{
  showErrorPage = false;

  redstoneSetUp([#my_lib]);
  setupConsoleLog();
  start(port: 4002);
}

my_lib.dart:

library my_lib;

import 'user_service.dart';
export 'user_service.dart';

import 'api.dart';
export 'api.dart';

user_service.dart:

import 'package:redstone/redstone.dart';

@Group('/user')
class UserService
{
  @Route('/login')
  String sayHi() => 'hi';
}

api.dart:

import 'package:redstone/redstone.dart';

@Group('/api')
class Api
{
  @Route('blah')
  String blah() => 'blah';
}

When run, only routes from user_service.dart are registered.

Pacane commented 7 years ago

In fact, only the first lib to be imported is registered.

indiealexh commented 7 years ago

You don't need the export.

You can simply add the services as sub librarys of my_lib... Redstone will then traverse the library.

e.g. my_lib.dart:

library my_lib;

import 'user_service.dart';
import 'api.dart';

user_service.dart:

library my_lib.user_service;

import 'package:redstone/redstone.dart';

@Group('/user')
class UserService
{
  @Route('/login')
  String sayHi() => 'hi';
}

api.dart:

library my_lib.api;

import 'package:redstone/redstone.dart';

@Group('/api')
class Api
{
  @Route('blah')
  String blah() => 'blah';
}

EDIT: Removed parts example

Pacane commented 7 years ago

The point of this was to avoid parts. And I don't think your first example without parts actually works.

It could though. But last time I checked, there was something preventing this from working. It'd only register the first imported library.

indiealexh commented 7 years ago

Hmm Ill need to double check, but I thought I have it working with imports. Ill come back to this.

Edit: It worked.

Pacane commented 7 years ago

Minimal reproductible code

// main.dart
import 'package:redstone/redstone.dart' as app;
import 'package:redstone_sample/rest_api.dart';

main() {
  app.redstoneSetUp([#rest_api]);
  app.setupConsoleLog();
  app.showErrorPage = false;
  app.start(port: 8084);
}
// lib/rest_api.dart
library rest_api;

import 'group1.dart';
import 'group2.dart';
// lib/group1.dart
import 'package:redstone/redstone.dart' as app;
import 'package:shelf/shelf.dart' as shelf;

@app.Group("/hello1")
class MyGroup {
  @app.DefaultRoute()
  hello() async {
    var a = 12;
    return new shelf.Response.ok('hello');
  }

  @app.Route('/user')
  user() => new shelf.Response.ok('user');
}
// lib/group2.dart
import 'package:redstone/redstone.dart' as app;
import 'package:shelf/shelf.dart' as shelf;

@app.Group("/hello2")
class MyGroup {
  @app.DefaultRoute()
  hello() async {
    var a = 12;
    return new shelf.Response.ok('hello');
  }

  @app.Route('/user')
  user() => new shelf.Response.ok('user');
}
// output
INFO: 2016-08-22 09:27:09.264545: Configured target for /hello1 [GET]: .MyGroup.hello (group: .MyGroup)
INFO: 2016-08-22 09:27:09.269159: Configured target for /hello1/user [GET]: .MyGroup.user (group: .MyGroup)
INFO: 2016-08-22 09:27:09.274478: Configured target for /hello1 [GET]: .MyGroup.hello (group: .MyGroup)
INFO: 2016-08-22 09:27:09.274639: Configured target for /hello1/user [GET]: .MyGroup.user (group: .MyGroup)
INFO: 2016-08-22 09:27:09.294048: Running on 0.0.0.0:8084

Ignore the "printing twice the same debug info" bug.

indiealexh commented 7 years ago

Try this (it works):

// lib/rest_api.dart
library rest_api;

import 'group1.dart';
import 'group2.dart';
// lib/group1.dart
library  rest_api.group1;
...
// lib/group2.dart
library  rest_api.group2;
...
Pacane commented 7 years ago

Indeed. @mnordine this could in interest you as a temporary fix. ^

Pacane commented 7 years ago

Still @indiealexh, this shouldn't be necessary, unless I misunderstand a concept with libraries?

indiealexh commented 7 years ago

@Pacane Tbh, I'm not sure how it should work, it just made sense for me to place my "Controllers" or "Services" as sub libraries and just import that at start up.

mnordine commented 7 years ago

That does indeed work, thx for the mention. I don't like the extra step of having to declare a library per-file though.

Pacane commented 7 years ago

Me neither. But it's a step forward removing part/part of if you don't like them. Eventually we might come up with a fix for this though.

I'd really like to have one lol