From c7fe14f6e150b3c17bacc0178d0507458a65be50 Mon Sep 17 00:00:00 2001 From: Baptiste Lyet Date: Wed, 28 Jan 2026 18:30:18 +0100 Subject: [PATCH 1/2] Documentation : Move response into a folder --- docs/src/pages/controller/route/README.md | 2 +- .../controller/route/{response.md => response/README.md} | 4 ++-- docs/src/pages/controller/route/response/static.md | 0 3 files changed, 3 insertions(+), 3 deletions(-) rename docs/src/pages/controller/route/{response.md => response/README.md} (93%) create mode 100644 docs/src/pages/controller/route/response/static.md diff --git a/docs/src/pages/controller/route/README.md b/docs/src/pages/controller/route/README.md index f7a75d2..df45ace 100644 --- a/docs/src/pages/controller/route/README.md +++ b/docs/src/pages/controller/route/README.md @@ -78,7 +78,7 @@ route.handle(userId => s"You asked for user $userId") Notice that adding a handler produces a value with a different type, a @:api(krop.route.Handler). -For more details see the separate pages for [Request](request.md), [Response](response.md) and [Handler](../handler.md). +For more details see the separate pages for [Request](request.md), [Response](response/README.md) and [Handler](../handler.md). diff --git a/docs/src/pages/controller/route/response.md b/docs/src/pages/controller/route/response/README.md similarity index 93% rename from docs/src/pages/controller/route/response.md rename to docs/src/pages/controller/route/response/README.md index b7150f3..a606b0f 100644 --- a/docs/src/pages/controller/route/response.md +++ b/docs/src/pages/controller/route/response/README.md @@ -17,7 +17,7 @@ You usually won't do this yourself; it is handled by the `Route` the `Response` ## Entities -Entities (response bodies) are handled in the same way as [requests](request.md): by specifying an @:api(krop.route.Entity). In this case the `Entity` is responsible for encoding Scala values as data in the HTTP response. +Entities (response bodies) are handled in the same way as [requests](../request.md): by specifying an @:api(krop.route.Entity). In this case the `Entity` is responsible for encoding Scala values as data in the HTTP response. Use `Entity.unit` to indicate that your response has no entity. For example: @@ -60,7 +60,7 @@ val getUser = ) ``` -For more complex cases you can use `orElse`, which allows you to handle an `Either[A, B]` and introduce custom error handling. The example below shows complex error handling combining `orElse` and `orNotFound`. A 404 Not Found is returned if the user id does not correspond to an existing user, and a 400 Bad Request is returned if the `Name` [entity](entities.md) is not valid. +For more complex cases you can use `orElse`, which allows you to handle an `Either[A, B]` and introduce custom error handling. The example below shows complex error handling combining `orElse` and `orNotFound`. A 404 Not Found is returned if the user id does not correspond to an existing user, and a 400 Bad Request is returned if the `Name` [entity](../entities.md) is not valid. ```scala mdoc:silent import io.circe.{Decoder, Encoder} diff --git a/docs/src/pages/controller/route/response/static.md b/docs/src/pages/controller/route/response/static.md new file mode 100644 index 0000000..e69de29 From 8a88baa8e35cae34e6f9fed29ef62a6413dfd15e Mon Sep 17 00:00:00 2001 From: Baptiste Lyet Date: Wed, 28 Jan 2026 18:31:06 +0100 Subject: [PATCH 2/2] Documentation : Add static responses --- .../pages/controller/route/response/static.md | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/docs/src/pages/controller/route/response/static.md b/docs/src/pages/controller/route/response/static.md index e69de29..1750b00 100644 --- a/docs/src/pages/controller/route/response/static.md +++ b/docs/src/pages/controller/route/response/static.md @@ -0,0 +1,57 @@ +# Static responses + +Krop provides several Response constructors for serving static content. These differ in where the content is loaded from and in the type of value the route handler must return. + +## StaticResource + +StaticResource responds with a file loaded from the application’s classpath (resources). Files are resolved using the JVM classloader. + +The pathPrefix is the resource directory, and the handler returns the remaining resource name. + +```scala +val staticResourceHandleableRoute = + Route( + Request.get(Path / "kroptest" / "assets"), + Response.staticResource("/kroptest/assets/") + ).handle(() => "create.html") +``` + +In this example, a request to /kroptest/assets will respond with the resource +/kroptest/assets/create.html. + +Only resources available on the classpath can be served. Files added to the filesystem at runtime are not visible. + +## StaticFile + +StaticFile responds with a single file loaded from the filesystem. + +The file path is fixed when the response is defined, and no handler input is required. + +```scala +val staticFileRoute = + Route( + Request.get(Path / "kroptest" / "assets" / "create.html"), + Response.staticFile("resources/kroptest/assets/create.html") + ).passthrough +``` + + +In this example, a request to /kroptest/assets/create.html will read the file +resources/kroptest/assets/create.html from disk. + +## StaticDirectory + +StaticDirectory responds with files loaded from a filesystem directory. The handler determines which file inside the directory is served. + +The pathPrefix parameter specifies the base directory, and the handler returns a relative Fs2Path within that directory. + +```scala +val staticDirectoryRoute = + Route( + Request.get(Path / "kroptest" / "assets2" / Param.string), + Response.StaticDirectory(Fs2Path("resources/kroptest/assets")) + ).handle(Fs2Path(_)) +``` + +In this example, a request to /kroptest/assets2/create.html will read the file +resources/kroptest/assets/create.html from disk. \ No newline at end of file