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

Only first group's routes registered #185

Open
mnordine opened this issue Aug 5, 2016 · 11 comments
Open

Only first group's routes registered #185

mnordine opened this issue Aug 5, 2016 · 11 comments

Comments

@mnordine
Copy link

mnordine commented Aug 5, 2016

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
Copy link
Contributor

Pacane commented Aug 5, 2016

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

@indiealexh
Copy link
Contributor

indiealexh commented Aug 22, 2016

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
Copy link
Contributor

Pacane commented Aug 22, 2016

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
Copy link
Contributor

indiealexh commented Aug 22, 2016

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

Edit: It worked.

@Pacane
Copy link
Contributor

Pacane commented Aug 22, 2016

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
Copy link
Contributor

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
Copy link
Contributor

Pacane commented Aug 22, 2016

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

@Pacane
Copy link
Contributor

Pacane commented Aug 22, 2016

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

@indiealexh
Copy link
Contributor

@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
Copy link
Author

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
Copy link
Contributor

Pacane commented Aug 23, 2016

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants