Netflix / karyon

The nucleus or the base container for Applications and Services built using the NetflixOSS ecosystem
Apache License 2.0
497 stars 153 forks source link

SimpleUriRouter to KaryonRestModule ? #225

Open victuxbb opened 9 years ago

victuxbb commented 9 years ago

Hi! I would like to discuss about the question that I made here https://groups.google.com/forum/?fromgroups#!topic/karyon-users/k-nrZ9V2OoQ

Currently I working on a company (Schibsted) that gives me time to collaborate (or try it) with your amazing NetflixOSS ecosystem :)

Well, I first approach that I'm thinking could be...

1- A config file were you put the configuration about REST resources, for example:

campaigns:
  type:     rest
  resource: company.package.to.CampaignsResource

2- When Karyon starts, it reads this file and then by "reflection" we can read all classes on it and by a convention of methods, build routes that matches this methods, for example: CampaignsResource.java

public Observable<Void> getCampaignsResource(String id) {
        //Do whatever...
    }

public Observable<Void> putCampaignsResource(String id) {
        //Do whatever...
    }

public Observable<Void> getCampaignsUsersResource(String id) {
        //Do whatever...
    }

Reading this methods you can extract by convention this routes:

GET /campaigns/{id}
PUT /campaigns/{id}
GET /campaigns/{id}/users

All this development would be a module that you can load in the bootstrap application

What do you think? I'm completly open to changes or new ideas :)

Thanks!

NiteshKant commented 9 years ago

How would you put this configuration file based approach in contrast to annotations based approach like what Jersey (jax-rs) uses? What pros & cons do you see?

victuxbb commented 9 years ago

Well...this is a mythical question, annotations vs configuration :) Personally I would summarize it in :

Annotations have become more harmful than helpful these days.
We should get back to pojos and focus on keeping our code as clutterless 
and framework-agnostic as possible to make it more readable and reuseable. 
Don’t let frameworks dictate your codebases, since they should be exchangable tools.
Beware of what a class should know, and what not. Some annotations are useful, 
most aren’t.
http://www.javacodegeeks.com/2014/01/an-annotation-nightmare.html

I think that a good framework must provide the option to choose how you want to work, for example, play framework, gives you the possibility to choose between annotations in your controllers or have a routing file with all routes:

Annotations: https://www.playframework.com/modules/router-1.1/home

Configuration: https://www.playframework.com/documentation/2.0/JavaRouting

Another good example for me is in the library GSON from google, you can configure the GSONBuilder to use annotations or not.

GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()

In conclusion I think would be a good point give support both options.

How do you see?

:)

victuxbb commented 9 years ago

Hi @NiteshKant, I made a first draft version using annotations:

https://github.com/scm-spain/karyon-rest-router

I'll keep adding features and anything that could be useful.

NiteshKant commented 9 years ago

@victuxbb great! Let me know when you are ready for a PR and I can review.