File-based server routing proof of concept

Hi everyone! This past week, I’ve been working on a proof of concept for Kemal, inspired by NitroJS’s file-based server routing.

Basically, the routes are defined by the project’s structure. My idea is to have a functional library in a couple of weeks that works very much like Nitro.

What’s missing:

  • Configuring the routes folder name

  • Auto-registration of middleware from files

  • No support for nested dynamic parameters yet

  • Websocket route registration

You can find the example project here: GitHub - krthr/kemal-file-routes-example (it is running in https://kemal-file-routes-example.krthr.co/)

And the WIP kemal-file-routes shard is here: GitHub - krthr/kemal-file-routes

7 Likes

Nice.

A few initial thoughts for your consideration:

I think define_handler is a bit too generic. I know you’re technically defining a handler, but as we’re already in a special file, I think it could be more aesthetic. route perhaps? Or skipping the enclosing block entirely. Or, I think this is my favourite, do like Next and remove the method from the file name and instead have get/put/delete/etc blocks. I think it makes sense to have the different methods on the same path in the same file.

A simpler way to get URL parameters. Getting them as block parameters would be cool, but I suspect that would be hard, if not impossible.

Hey, thanks for the comment!

I took heavy inspiration from Nuxt (Nitro), that’s why I used the define_handler. See: server · Nuxt Directory Structure v4

  • I agree the name could be less generic.
  • Personally, I don’t like the Next approach as I like to separate the logic of every route in different files
  • It’s a nice idea. Right now I couldn’t say if it impossible or not. I might have some time to explore it :thinking: