-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRun.scala
67 lines (57 loc) · 2.08 KB
/
Run.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package example
import cats.effect.{IO, IOApp, Deferred, ExitCode}
import io.quartz.QuartzH2Server
import io.quartz.http2._
import io.quartz.http2.model.{Headers, Method, ContentType, Request, Response}
import io.quartz.http2.model.Method._
import io.quartz.http2.model.StatusCode
import cats.data.ReaderT
import cats.effect.Resource
import io.quartz.http2.routes.HttpRouteRIO
import io.quartz.http2.routes.WebFilter
import io.quartz.http2.routes.RIO
import java.util.Date
import org.typelevel.log4cats.Logger
import io.quartz.MyLogger._
/*
A rich example on many things at once
1. environent trait from cats Resource passed to ReaderT/RIO route function
2. ReaderT based HttpRouteRIO
3. header attribute "test_tid" data from filter
4. 403 forbidden on web URI starting from /private
*/
object MyApp extends IOApp {
val filter: WebFilter = (request: Request) =>
IO(
Either.cond(
!request.uri.getPath().startsWith("/private"),
request.hdr("test_tid" -> "ABC123Z9292827"),
Response.Error(StatusCode.Forbidden).asText("Denied: " + request.uri.getPath())
)
)
class Resource1 {
// time tick, epoch time
def tick: IO[String] = IO(Date().getTime().toString())
}
val RR: HttpRouteRIO[Resource1] = { case req @ GET -> Root / "tick" =>
for { // ReaderT monad
value <- RIO(45) // ReaderT effect, look at the RIO object
envResource <- ReaderT.ask[IO, Resource1]
tick <- RIO.liftIO(envResource.tick)
r <- RIO(Response.Ok().asText(tick + " tid=" + req.headers.get("test_tid").getOrElse("Missing header: test_tid")))
} yield (r)
}
def run(args: List[String]): IO[ExitCode] =
for {
r <- IO(
Resource.make[IO, Resource1](Logger[IO].info("Init resource OK") >> IO(Resource1()))(_ =>
Logger[IO].info("discard resource OK")
)
)
ctx <- QuartzH2Server.buildSSLContext("TLS", "keystore.jks", "password")
exitCode <- r.use(env =>
new QuartzH2Server("localhost", 8443, 60000, Some(ctx))
.startRIO(env, RR, filter, sync = false)
)
} yield (exitCode)
}