Skip to content

Commit

Permalink
Simplify NotFound tool and add assets
Browse files Browse the repository at this point in the history
NotFound tool is now an Application, which simplifies using it.

KropAssets tool correctly serves assets from resources
  • Loading branch information
noelwelsh committed Sep 21, 2023
1 parent de7ec8c commit 7c2952e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala/krop/tool/KropAssets.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ object KropAssets {
given LoggerFactory[IO] = Slf4jFactory.create[IO]

val kropAssets: Route = {
val route = ResourceServiceBuilder[IO]("/krop/assets").toRoutes
val route = ResourceServiceBuilder[IO]("/krop/assets")
.withPathPrefix("/krop/assets")
.toRoutes
Route(route)
}

Expand Down
59 changes: 39 additions & 20 deletions core/src/main/scala/krop/tool/NotFound.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,56 @@
package krop.tool

import cats.effect.IO
import krop.Application
import krop.Mode
import org.http4s.*
import org.http4s.dsl.io.*
import org.http4s.headers.`Content-Type`

/** This is a tool that displays useful information in development mode if no
* route matches a request. In production mode it simply returns a 404 Not
* Found.
*/
object NotFound {
def requestToString(request: Request[IO]): String =
s"${request.method} ${request.uri.path}"

def development(request: Request[IO]): IO[Response[IO]] = {
val html = s"""
|<!doctype html>
|<html lang=en>
|<head>
| <meta charset=utf-8>
| <link href="/krop/assets/krop.css" rel="stylesheet"/>
| <title>Krop: Not Found</title>
|</head>
|<body>
| <h1>Not Found</h1>
| <p>The request for did not match any routes :-(</p>
| <p><code>${requestToString(request)}</code></p>
|</body>
|</html>
""".stripMargin
/** The development version of this tool, which returns useful information in
* the case of an unmatched request.
*/
val development: Application = {
// Need assets to serve the CSS
KropAssets.kropAssets.otherwise(
Application.lift { req =>
val html =
s"""
|<!doctype html>
|<html lang=en>
|<head>
| <meta charset=utf-8>
| <link href="/krop/assets/krop.css" rel="stylesheet"/>
| <title>Krop: Not Found</title>
|</head>
|<body>
| <h1>Not Found</h1>
| <p>The request</p>
| <p><code>${requestToString(req)}</code></p>
| <p>did not match any routes :-{</p>
|</body>
|</html>
""".stripMargin

org.http4s.dsl.io.NotFound(html, `Content-Type`(MediaType.text.html))
org.http4s.dsl.io.NotFound(html, `Content-Type`(MediaType.text.html))
}
)
}

val production: IO[Response[IO]] = IO.pure(Response.notFound)
/** The production version of this tool, which returns NotFound to every
* request.
*/
val production: Application = Application.notFound

def notFound(request: Request[IO]): IO[Response[IO]] =
if Mode.mode.isProduction then production else development(request)
/** The notFound Application tool. */
val notFound: Application =
if Mode.mode.isProduction then production else development
}

0 comments on commit 7c2952e

Please sign in to comment.