https://kotlinlang.org logo
#ktor
Title
# ktor
r

Robert Kempton

05/24/2022, 6:22 PM
Hello! I really like the idea of directory based routing, like how apache + php works. There you put files in a directory, and voila, routes defined. In addition to making it easy to define routes, finding routes based on a URL is really easy, you navigate to the file and you're done. I've created a plugin that provides package based routing for ktor. You provide a base package, and add classes in the base package. The package name and class name is used to determine the route path, and all you need to do is implement the KtorRoute interface. AFAIK, this package based routing has never been done before in any jvm language, so I'm sure it seems really weird. Here's the repo where the README has more info on usage https://github.com/snowbldr/ktor-pkg-router It's published to maven central, so is ready to use if you'd like to try it out. I hope you find this useful, and please let me know what you think, I'd be super interested in opinions on whether this is a good idea, a terrible idea, or if there's better ways to implement this.
👍 1
e

ephemient

05/24/2022, 6:34 PM
annotation processing to do this without reflection?
r

Robert Kempton

05/24/2022, 6:35 PM
Wouldn't annotation processing still use reflection to access the annotations?
e

ephemient

05/24/2022, 6:35 PM
no, that's done at compile time
r

Robert Kempton

05/24/2022, 6:42 PM
Do you have any good resources for how to do that? That seems like it would be a good move to make if it's possible to avoid the reflective method call at runtime. I've only ever seen annotations used by accessing the class/method via reflection and walking over the array of annotations.
(that's how spring does it, and I've mostly read a lot of spring code)
r

Robert Kempton

05/24/2022, 6:49 PM
That's neato, I wonder why spring doesn't do this, I bet it would help a lot in terms of speed. I'll give that a shot this evening and see how far I can get. Thanks for the suggestion! :)
e

ephemient

05/24/2022, 6:51 PM
it means you can't simply add features by adding a dependency - they have to be compiled with the processor. but even though Spring wants to be more dynamic, IIRC they still tend to perform class lookups on demand rather than classpath scanning
r

Robert Kempton

05/24/2022, 6:54 PM
They make very heavy use of classpath scanning, the auto configuration system in spring boot probably does it the most. That basically scans every class on the class path looking for the @Configuration annotation.
That's why spring boot starts up so slow