Skip to content

About

Example project demonstrating how to use Ktor-routes-codegen.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Ktor Routes File Example

This is an example project demonstrating how to use the Ktor-routes-codegen project to generate routing code from a routes file.

Running the Example

  1. Make sure you have Java 21 or higher installed
  2. Run the application:
./gradlew run
  1. Open your browser and navigate to http://localhost:8080

Project Structure

  • src/main/kotlin/com/example/Application.kt - Main application entry point
  • src/main/kotlin/com/example/controllers/ - Controller classes that handle requests
  • src/main/resources/routes - Routes definition file

How it Works

  1. The @GenerateRoutes annotation triggers the KSP processor at compile time
  2. The processor reads the routes file and generates type-safe routing code
  3. The generated GeneratedRoutes class is used in the application to configure routing

Routes File

The routes file uses a simple syntax inspired by Play Framework:

# HTTP_METHOD    PATH          CONTROLLER.ACTION(parameters)
GET              /             com.example.controllers.HomeController.index()
GET              /users/:id    com.example.controllers.UserController.show(id: String)

Generated Code

When you run ./gradlew build, the KSP processor generates routing code in the build/generated/ksp/main/kotlin/ directory.

The generated GeneratedRoutes class automatically creates route handlers for each line in the routes file. For example, the two lines above will look like this in the generated file:

class GeneratedRoutes {
    fun configureRoutes(routing: Routing) {
        routing.get("/") {
            val controller = HomeController()
            controller.index()
        }
        
        routing.get("/users/{id}") {
            val controller = UserController()
            val id = call.parameters["id"] ?: ""
            controller.show(id)
        }
    }
}

About

Example project demonstrating how to use Ktor-routes-codegen.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages