javalin / website

Javalin website source code
https://javalin.io
36 stars 87 forks source link

Document Javalin 6 plugin changes #240

Closed zugazagoitia closed 7 months ago

zugazagoitia commented 1 year ago
Details Just to be clear, I've listed Kotlin in standalone snippet as it uses singleton object (unique to kt), but these Java examples above are fully compatible with Kotlin and here's the example:
Java Kotlin
// factory instance
config.plugins.register(new BasicAuth(), it-> {
  it.username = "panda";
  it.password = "bamboo";
});
// factory singleton
config.plugins.register(BasicAuthPlugin.FACTORY, it -> {
  it.username = "panda";
  it.password = "bamboo";
});
// plugin instance
config.plugins.register(
  new BasicAuthPlugin(it -> {
    it.username = "panda";
    it.password = "bamboo";
  })
);
   
// factory instance
cfg.plugins.register(BasicAuth()) {
    it.username = "panda"
    it.password = "bamboo"
}
// factory signleton
cfg.plugins.register(BasicAuthPlugin.FACTORY) {
    it.username = "panda"
    it.password = "bamboo"
}
// plugin instance
cfg.plugins.register(
    BasicAuthPlugin { 
        it.username = "panda"
        it.password = "bamboo"
    }
)
  
I didn't list it earlier as I think that the object singleton variant suits best for Kotlin users, but that's purely an extra api sugar, like our reified function extensions (to e.g. Context). > The plugin instance part feels very useful for an extension point perspective. "Just implement our interface and plug it into plugins.register for your own plugins" To limit all variants, I'd personally make plugin constructor private & remove factory field, so we'd force all plugins to use the same approach, but I don't mind if we'd keep it open just in case. If we'd limit all variants just to the factory pattern, all plugins should implement it like that: https://github.com/javalin/javalin/blob/bc0499db4e093ad31b9f044afbdc33abfe3b6765/javalin/src/main/java/io/javalin/plugin/bundled/BasicAuthPlugin.kt#L29-L31 And (optionally) provide a singleton object for kt users: https://github.com/javalin/javalin/blob/bc0499db4e093ad31b9f044afbdc33abfe3b6765/javalin/src/main/java/io/javalin/plugin/bundled/BasicAuthPlugin.kt#L34 _Originally posted by @dzikoysk in https://github.com/javalin/javalin/pull/1945#issuecomment-1627792414_