-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for passing custom contexts
- Loading branch information
Showing
9 changed files
with
114 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package app | ||
|
||
case class Context( | ||
session: Session | ||
) | ||
|
||
case class Session(data: collection.mutable.Map[String, String]) | ||
|
||
trait CustomParser[T] extends cask.router.ArgReader[Any, T, Context] | ||
object CustomParser: | ||
given CustomParser[Context] with | ||
def arity = 0 | ||
def read(ctx: Context, label: String, input: Any): Context = ctx | ||
given CustomParser[Session] with | ||
def arity = 0 | ||
def read(ctx: Context, label: String, input: Any): Session = ctx.session | ||
given literal[Literal]: CustomParser[Literal] with | ||
def arity = 1 | ||
def read(ctx: Context, label: String, input: Any): Literal = input.asInstanceOf[Literal] | ||
|
||
object DecoratedContext extends cask.MainRoutes{ | ||
|
||
class custom extends cask.router.Decorator[cask.Response.Raw, cask.Response.Raw, Any, Context]{ | ||
|
||
override type InputParser[T] = CustomParser[T] | ||
|
||
def wrapFunction(req: cask.Request, delegate: Delegate) = { | ||
// Create a custom context out of the request. Custom contexts are useful | ||
// to group an expensive operation that may be used by multiple | ||
// parameter readers. | ||
val ctx = Context(Session(collection.mutable.Map.empty)) // this would typically be populated from a signed cookie | ||
|
||
delegate(ctx, Map("user" -> 1337)).map{ response => | ||
val extraCookies = ctx.session.data.map( | ||
(k, v) => cask.Cookie(k, v) | ||
) | ||
|
||
response.copy( | ||
cookies = response.cookies ++ extraCookies | ||
) | ||
} | ||
|
||
} | ||
} | ||
|
||
@custom() | ||
@cask.get("/hello/:world") | ||
def hello(world: String, req: cask.Request)(session: Session, user: Int) = { | ||
session.data("hello") = "world" | ||
world + user | ||
} | ||
|
||
initialize() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package app | ||
import io.undertow.Undertow | ||
|
||
import utest._ | ||
|
||
object ExampleTests extends TestSuite{ | ||
def withServer[T](example: cask.main.Main)(f: String => T): T = { | ||
val server = Undertow.builder | ||
.addHttpListener(8081, "localhost") | ||
.setHandler(example.defaultHandler) | ||
.build | ||
server.start() | ||
val res = | ||
try f("http://localhost:8081") | ||
finally server.stop() | ||
res | ||
} | ||
|
||
val tests = Tests{ | ||
test("DecoratedContext") - withServer(DecoratedContext){ host => | ||
val response = requests.get(s"$host/hello/woo") | ||
response.text() ==> "woo1337" | ||
response.cookies("hello").getValue ==> "world" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import mill._, scalalib._ | ||
|
||
trait AppModule extends CrossScalaModule{ | ||
|
||
def ivyDeps = Agg[Dep]( | ||
) | ||
object test extends ScalaTests with TestModule.Utest{ | ||
|
||
def ivyDeps = Agg( | ||
ivy"com.lihaoyi::utest::0.8.1", | ||
ivy"com.lihaoyi::requests::0.8.0", | ||
) | ||
} | ||
} |