ninja icon indicating copy to clipboard operation
ninja copied to clipboard

Ninja as a an embeddable microframework

Open noguespi opened this issue 10 years ago • 3 comments

Client-side framework like angularjs and reactjs keep growing. Every days, new developper are switching to this model of dev (like me recently). With these frameworks, their are a lot of features which may not rely on the webserver, the view obvisously but sometimes the routes too and probably other stuff.

Lately I started a new project, it had DB interactions, some threads and task doing stuff, classic project. At some point, I just needed to add a REST rest endpoint to this project for my reactjs frontend.

So I naturally thought to ninjaframework but it didn't make it. It was to heavy to integrate. I had to start a brand new maven archetype while I just wanted to add the webframework to my project and start it manually in a thread, by example :

// in my StartWebServer thread ...
NinjaJetty ninja = new NinjaJetty();
ninja.configure(...); // better if optional
ninja.routes().POST().("/rest").with(Controller.class, "list");
// don't configure view, I don't need it
ninja.start();
ninja.join();

So maybe it is already possible to do that ? If yes can you update the documentation, if no, would it be possible ? I think it is really a needed feature if ninja want to survive among the REST framework and the lightweight framework like Spark and the likes.

Ninja has really powerfull feature I'm used too and I may not find in other java micro webframework, I'm sure ninja could be used as a microwebframework.

noguespi avatar Mar 10 '16 08:03 noguespi

So what do you think about this feature ?

It looks like 5.4.0 and NinjaClassicModule with the ability to disable some Ninja features is leaning towards this way.

However, there are still hardcoded dependencies which prevent to just embbed ninja in a project like mandatory conf/application.conf in a specific non customisable path (only customisable via System.property) and with some mandatory properties (cookie.prefix ...).

noguespi avatar Mar 25 '16 10:03 noguespi

I could see the even lighter weight, embeddable version to be useful. We're probably not that far off with the FrameworkModule feature in v5.4.0. That's what'll get you the minimal dependencies required by Ninja. You basically want to bootstrap the configuration via code (likely in AbstractStandalone) vs. searching for conf.Module, conf/application.conf, etc? Remember that NinjaJetty uses the builder pattern already, so something like below would work today.

NinjaJetty ninja = new NinjaJetty()
    .start()      // already calls configure() if not yet called
    .join();

Let's say we wanted to let you programmatically setup routing. I think you'd want to merely supply this via code and then have ninja's Bootstrap not search for conf.Routes when it starts. I think its critical this is done in nearly an identical way to having a conf.Routes class. Java 8 makes the syntax look nice. Here's one idea:

NinjaJetty ninja = new NinjaJetty()
    .routes((router) -> {
        router.GET().route("/index").with(AppController.class, "index");
    })
    .start()      // already calls configure() if not yet called
    .join();

A similar approach could work for other items you generally need to star the app. conf.Module would need to be supplied. An external configuration file is already supported today, but you'd want to have some simple way of disabling the search for a conf/application.conf perhaps.

jjlauer avatar Mar 25 '16 13:03 jjlauer

Yes exactly what I have in mind

noguespi avatar Mar 27 '16 18:03 noguespi