Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/shared/src/main/scala/krop/route/Param.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package krop.route

import fs2.io.file.Path as Fs2Path

/** A [[package.Param]] is used to extract values from a URI's path or query
* parameters.
*
Expand Down Expand Up @@ -104,6 +106,13 @@ object Param {
val seq: Param.All[Seq[String]] =
Param.All(SeqStringCodec.seqString)

/** `Param` that convert path segments to Fs2Path`.
*/
val fs2Path: Param[Fs2Path] =
Param
.separatedString("/")
.imap(Fs2Path.apply)(_.toString)

/** Constructs a [[Param]] that decodes input into a `String` by appending all
* the input together with `separator` inbetween each element. Encodes data
* by splitting on `separator`.
Expand Down
8 changes: 2 additions & 6 deletions core/shared/src/main/scala/krop/tool/DefaultAssets.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package krop.tool

import fs2.io.file.Path as Fs2Path
import krop.route.Handler
import krop.route.Param
import krop.route.Param.fs2Path
import krop.route.Path
import krop.route.Request
import krop.route.Response
Expand All @@ -27,11 +27,7 @@ import krop.route.Route
object DefaultAssets {
val assets: Handler =
Route(
Request.get(
Path / "assets" / Param
.separatedString("/")
.imap(Fs2Path.apply)(_.toString)
),
Request.get(Path / "assets" / fs2Path),
Response.staticDirectory(Fs2Path("assets/"))
).passthrough
}
29 changes: 29 additions & 0 deletions docs/src/pages/controller/route/paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,32 @@ intParam.name
// Better, as the name has been changed appropriately.
intParam.withName("<Int>").name
```
### Convert path segments to a FS2 Path

**fs2Path** utility in `Param` companion object is used to convert string path to a FS2 path
It supports bidirectional mapping: URL → Fs2Path and Fs2Path → URL.

**Flow Diagram:**

````
Request URL: /assets/images/icons/logo.svg
Param.separatedString("/") → "images/icons/logo.svg"
Fs2Path.apply → Fs2Path("images/icons/logo.svg")
Route handler receives typed Fs2Path
````

**Usage in Route :**

````scala
val staticDirectoryRoute =
Route(
Request.get(Path / "kroptest" / "assets" / AssetPath.fs2Path),
Response.StaticDirectory(Fs2Path("resources/kroptest/assets"))
).passthrough
````