summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2018-08-13 01:12:07 +0800
committerLi Haoyi <haoyi.sg@gmail.com>2018-08-13 01:12:07 +0800
commit974dcbe09830785f202f54959ae5397e9084f7a0 (patch)
treed127d2aff446b8c50debfae3823ebba7e2b457ea /example
parenteb32d8d4a1e2bd50e5416fcfefd72dfe6da1a7bb (diff)
downloadcask-974dcbe09830785f202f54959ae5397e9084f7a0.tar.gz
cask-974dcbe09830785f202f54959ae5397e9084f7a0.tar.bz2
cask-974dcbe09830785f202f54959ae5397e9084f7a0.zip
add docs on writing custom endpoints
Diffstat (limited to 'example')
-rw-r--r--example/endpoints/app/src/Endpoints.scala32
-rw-r--r--example/endpoints/app/test/src/ExampleTests.scala27
-rw-r--r--example/endpoints/build.sc18
3 files changed, 77 insertions, 0 deletions
diff --git a/example/endpoints/app/src/Endpoints.scala b/example/endpoints/app/src/Endpoints.scala
new file mode 100644
index 0000000..1960b93
--- /dev/null
+++ b/example/endpoints/app/src/Endpoints.scala
@@ -0,0 +1,32 @@
+package app
+
+
+class custom(val path: String, val methods: Seq[String]) extends cask.Endpoint{
+ type Output = Int
+ def wrapFunction(ctx: cask.ParamContext, delegate: Delegate): Returned = {
+ delegate(Map()) match{
+ case cask.internal.Router.Result.Success(num) =>
+ cask.internal.Router.Result.Success(
+ cask.Response("Echo " + num, statusCode = num)
+ )
+ case e: cask.internal.Router.Result.Error => e
+ }
+ }
+
+ // Change this if you want to change
+ def wrapPathSegment(s: String) = Seq(s)
+
+ type Input = Seq[String]
+ type InputParser[T] = cask.endpoints.QueryParamReader[T]
+}
+
+object Endpoints extends cask.MainRoutes{
+
+
+ @custom("/echo/:status", methods = Seq("get"))
+ def echoStatus(status: String) = {
+ status.toInt
+ }
+
+ initialize()
+}
diff --git a/example/endpoints/app/test/src/ExampleTests.scala b/example/endpoints/app/test/src/ExampleTests.scala
new file mode 100644
index 0000000..1302d85
--- /dev/null
+++ b/example/endpoints/app/test/src/ExampleTests.scala
@@ -0,0 +1,27 @@
+package app
+import io.undertow.Undertow
+
+import utest._
+
+object ExampleTests extends TestSuite{
+ def test[T](example: cask.main.BaseMain)(f: String => T): T = {
+ val server = Undertow.builder
+ .addHttpListener(8080, "localhost")
+ .setHandler(example.defaultHandler)
+ .build
+ server.start()
+ val res =
+ try f("http://localhost:8080")
+ finally server.stop()
+ res
+ }
+
+ val tests = Tests{
+ 'Endpoints - test(Endpoints){ host =>
+ requests.get(s"$host/echo/200").text() ==> "Echo 200"
+ requests.get(s"$host/echo/200").statusCode ==> 200
+ requests.get(s"$host/echo/400").text() ==> "Echo 400"
+ requests.get(s"$host/echo/400").statusCode ==> 400
+ }
+ }
+}
diff --git a/example/endpoints/build.sc b/example/endpoints/build.sc
new file mode 100644
index 0000000..2794393
--- /dev/null
+++ b/example/endpoints/build.sc
@@ -0,0 +1,18 @@
+import mill._, scalalib._
+
+
+trait AppModule extends ScalaModule{
+ def scalaVersion = "2.12.6"
+ def ivyDeps = Agg(
+ ivy"com.lihaoyi::cask:0.0.1",
+ )
+
+ object test extends Tests{
+ def testFrameworks = Seq("utest.runner.Framework")
+
+ def ivyDeps = Agg(
+ ivy"com.lihaoyi::utest::0.6.3",
+ ivy"com.lihaoyi::requests::0.1.2",
+ )
+ }
+} \ No newline at end of file