Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework routeDispatchTries + endpoint lookup for more intuitive 404/405 responses. #52

Merged
merged 4 commits into from
Nov 15, 2021
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.bloop/
.metals/
.vscode/
target/
*.iml
.idea
Expand Down
2 changes: 2 additions & 0 deletions .mill-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0.9.7

2 changes: 1 addition & 1 deletion build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CaskMainModule(val crossScalaVersion: String) extends CaskModule {
def scalacPluginIvyDeps = T{ if (!isDotty) Agg(ivy"com.lihaoyi::acyclic:0.2.0") else Agg() }

object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"
def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
ivy"com.lihaoyi::requests::0.6.9"
Expand Down
79 changes: 38 additions & 41 deletions cask/src/cask/main/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ abstract class Main{

implicit def log: cask.util.Logger = new cask.util.Logger.Console()

def routeTries = Main.prepareRouteTries(allRoutes)
def dispatchTrie = Main.prepareDispatchTrie(allRoutes)

def defaultHandler = new BlockingHandler(
new Main.DefaultHandler(routeTries, mainDecorators, debugMode, handleNotFound, handleMethodNotAllowed, handleEndpointError)
new Main.DefaultHandler(dispatchTrie, mainDecorators, debugMode, handleNotFound, handleMethodNotAllowed, handleEndpointError)
)

def handleNotFound() = Main.defaultHandleNotFound()
Expand All @@ -74,7 +74,7 @@ abstract class Main{
}

object Main{
class DefaultHandler(routeTries: Map[String, DispatchTrie[(Routes, EndpointMetadata[_])]],
class DefaultHandler(dispatchTrie: DispatchTrie[Map[String, (Routes, EndpointMetadata[_])]],
mainDecorators: Seq[Decorator[_, _, _]],
debugMode: Boolean,
handleNotFound: () => Response.Raw,
Expand All @@ -101,35 +101,30 @@ object Main{
(r: Any) => Main.writeResponse(exchange, r.asInstanceOf[Response.Raw])
)

val dispatchTrie: DispatchTrie[(Routes, EndpointMetadata[_])] = routeTries.get(effectiveMethod) match {
case None =>
Main.writeResponse(exchange, handleMethodNotAllowed())
return
case Some(trie) => trie
}

dispatchTrie.lookup(Util.splitPath(exchange.getRequestPath).toList, Map()) match {
case None => Main.writeResponse(exchange, handleNotFound())
case Some(((routes, metadata), routeBindings, remaining)) =>
Decorator.invoke(
Request(exchange, remaining),
metadata.endpoint,
metadata.entryPoint.asInstanceOf[EntryPoint[Routes, _]],
routes,
routeBindings,
(mainDecorators ++ routes.decorators ++ metadata.decorators).toList,
Nil
) match{
case Result.Success(res) => runner(res)
case e: Result.Error =>
Main.writeResponse(
exchange,
handleError(routes, metadata, e)
)
None
case Some((methodMap, routeBindings, remaining)) =>
methodMap.get(effectiveMethod) match {
case None => Main.writeResponse(exchange, handleMethodNotAllowed())
case Some((routes, metadata)) =>
Decorator.invoke(
Request(exchange, remaining),
metadata.endpoint,
metadata.entryPoint.asInstanceOf[EntryPoint[Routes, _]],
routes,
routeBindings,
(mainDecorators ++ routes.decorators ++ metadata.decorators).toList,
Nil
) match {
case Result.Success(res) => runner(res)
case e: Result.Error =>
Main.writeResponse(
exchange,
handleError(routes, metadata, e)
)
}
}
}
// println("Completed Request: " + exchange.getRequestPath)
}catch{case e: Throwable =>
e.printStackTrace()
}
Expand All @@ -149,23 +144,25 @@ object Main{
)
}

def prepareRouteTries(allRoutes: Seq[Routes]): Map[String, DispatchTrie[(Routes, EndpointMetadata[_])]] = {
val routeList = for{
def prepareDispatchTrie(allRoutes: Seq[Routes]): DispatchTrie[Map[String, (Routes, EndpointMetadata[_])]] = {
val flattenedRoutes = for {
routes <- allRoutes
route <- routes.caskMetadata.value.map(x => x: EndpointMetadata[_])
} yield (routes, route)
metadata <- routes.caskMetadata.value
} yield {
val segments = Util.splitPath(metadata.endpoint.path)
val methodMap = metadata.endpoint.methods.map(_ -> (routes, metadata: EndpointMetadata[_])).toMap
(segments, methodMap, metadata.endpoint.subpath)
}

val allMethods: Set[String] =
routeList.flatMap(_._2.endpoint.methods).map(_.toLowerCase).toSet
val dispatchInputs = flattenedRoutes.groupBy(_._1).map { case (segments, values) =>
val methodMap = values.map(_._2).flatten.toMap
val hasSubpath = values.map(_._3).contains(true)
(segments, methodMap, hasSubpath)
}.toSeq

allMethods
.map { method =>
method -> DispatchTrie.construct[(Routes, EndpointMetadata[_])](0,
for ((route, metadata) <- routeList if metadata.endpoint.methods.contains(method))
yield (Util.splitPath(metadata.endpoint.path): collection.IndexedSeq[String], (route, metadata), metadata.endpoint.subpath)
)
}.toMap
DispatchTrie.construct(0, dispatchInputs)
}

def writeResponse(exchange: HttpServerExchange, response: Response.Raw) = {
response.data.headers.foreach{case (k, v) =>
exchange.getResponseHeaders.put(new HttpString(k), v)
Expand Down
2 changes: 1 addition & 1 deletion example/compress/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/compress2/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/compress3/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/cookies/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/decorated/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/decorated2/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/endpoints/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/formJsonPost/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/httpMethods/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/minimalApplication/app/test/src/ExampleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object ExampleTests extends TestSuite{

requests.post(s"$host/do-thing", data = "hello").text() ==> "olleh"

requests.get(s"$host/do-thing", check = false).statusCode ==> 404
requests.delete(s"$host/do-thing", check = false).statusCode ==> 405
}
}
}
2 changes: 1 addition & 1 deletion example/minimalApplication/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object ExampleTests extends TestSuite{

requests.post(s"$host/do-thing", data = "hello").text() ==> "olleh"

requests.get(s"$host/do-thing", check = false).statusCode ==> 404
requests.delete(s"$host/do-thing", check = false).statusCode ==> 405
}
}
}
2 changes: 1 addition & 1 deletion example/minimalApplication2/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/redirectAbort/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/scalatags/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait AppModule extends CrossScalaModule{
)

object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/staticFiles/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/staticFiles2/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/todo/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait AppModule extends CrossScalaModule{
)

object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/todoApi/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/todoDb/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait AppModule extends CrossScalaModule{
)

object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/twirl/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait AppModule extends CrossScalaModule with mill.twirllib.TwirlModule{
)

object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/variableRoutes/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/websockets/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/websockets2/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/websockets3/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down
2 changes: 1 addition & 1 deletion example/websockets4/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait AppModule extends CrossScalaModule{
def ivyDeps = Agg[Dep](
)
object test extends Tests{
def testFrameworks = Seq("utest.runner.Framework")
def testFramework = "utest.runner.Framework"

def ivyDeps = Agg(
ivy"com.lihaoyi::utest::0.7.10",
Expand Down