11package  ratpack.example.kotlin 
22
3- import  ratpack.launch.LaunchConfig 
4- import  ratpack.handling.Handler 
5- import  ratpack.guice.Guice.handler 
6- import  ratpack.handling.ChainAction 
7- import  ratpack.handling.Chain 
8- import  ratpack.func.Action 
9- import  ratpack.guice.BindingsSpec 
3+ import  ratpack.registry.Registry 
4+ import  ratpack.guice.Guice 
105
116class  HandlerFactory  : ratpack.launch.HandlerFactory  {
12-   override  fun  create (launchConfig  :  LaunchConfig ? ) =  handler(launchConfig!! , registerModules, routes)
13- 
14-   /* *
15-    * Registers all of the Guice modules that make up the application. 
16-    * 
17-    * This is only invoked once during application bootstrap. If you change the 
18-    * module configuration of an application, you must restart it. 
19-    */  
20-   private  val  registerModules =  Action  {(registry :  BindingsSpec ? ) -> 
21-     registry!! .add(MyModule ())
22-   }
23- 
24-   private  val  routes =  Action  {(chain :  Chain ? ) -> 
25-     Routes ().execute(chain!! )
26-   }
27- 
28-   private  class  Routes  : ChainAction () {
29-     /* *
30-      * Adds potential routes. 
31-      * 
32-      * After this method completes, a handler chain will be constructed from 
33-      * the specified routes. 
34-      * 
35-      * This method will be called for every request. This makes it possible 
36-      * to dynamically define the routes if necessary. 
37-      */  
38-     override  fun  execute () {
39-       //  Map to /foo
40-       handler(" foo"  ) { context ->  context.render(" from the foo handler"  ) }
41- 
42-       //  Map to /bar
43-       handler(" bar"  ) { context ->  context.render(" from the bar handler"  ) }
44- 
45-       //  Set up a nested routing block, which is delegated to `nestedHandler`
46-       prefix(" nested"  ) {(nested :  Chain ? ) -> 
47-         //  Map to /nested/*/*
48-         nested!! .handler(" :var1/:var2?"  ) { context -> 
49-           //  The path tokens are the :var1 and :var2 path components above
50-           val  pathTokens =  context.getPathTokens()!! 
51-           context.render(" from the nested handler, var1: ${pathTokens[" var1"  ]} , var2: ${pathTokens[" var2"  ]} "  )
52-         }
53-       }
54- 
55-       //  Map to a dependency injected handler
56-       handler(" injected"  , getRegistry()!! [javaClass<MyHandler >()])
57- 
58-       //  Bind the /static app path to the src/ratpack/assets/images dir
59-       //  Try /static/logo.png
60-       prefix(" static"  ) {(nested :  Chain ? ) ->  nested!! .assets(" assets/images"  ) }
61- 
62-       //  If nothing above matched, we'll get to here.
63-       handler { context ->  context.render(" root handler!"  ) }
64-     }
65-   }
7+   override  fun  create (registry  :  Registry ) =  Guice .builder(registry)
8+       .bindings({ registry ->  registry.add(MyModule ()) })
9+       .build({ chain -> 
10+         chain
11+             //  Map to /foo
12+             .handler(" foo"  ) { context ->  context.render(" from the foo handler"  ) }
13+ 
14+             //  Map to /bar
15+             .handler(" bar"  ) { context ->  context.render(" from the bar handler"  ) }
16+ 
17+             //  Set up a nested routing block, which is delegated to `nestedHandler`
18+             .prefix(" nested"  ) { nested -> 
19+               //  Map to /nested/*/*
20+               nested.handler(" :var1/:var2?"  ) { context -> 
21+                 //  The path tokens are the :var1 and :var2 path components above
22+                 val  pathTokens =  context.getPathTokens()
23+                 context.render(" from the nested handler, var1: ${pathTokens[" var1"  ]} , var2: ${pathTokens[" var2"  ]} "  )
24+               }
25+             }
26+ 
27+             //  Map to a dependency injected handler
28+             .handler(" injected"  , chain.getRegistry()[javaClass<MyHandler >()])
29+ 
30+             //  Bind the /static app path to the src/ratpack/assets/images dir
31+             //  Try /static/logo.png
32+             .prefix(" static"  ) { nested ->  nested.assets(" assets/images"  ) }
33+ 
34+             //  If nothing above matched, we'll get to here.
35+             .handler { context ->  context.render(" root handler!"  ) }
36+       })
6637}
0 commit comments