Skip to content

Commit

Permalink
ZIO 2 support (#809)
Browse files Browse the repository at this point in the history
* upgrade to zio 2.0.0-RC1

* bump timeout for failing test

* other fixes

* increase timeout

* rejigger broken test

* try, try again

* get compiling & tests passing

Co-authored-by: Kit Langton <kit.langton@gmail.com>
  • Loading branch information
2 people authored and amitksingh1490 committed Feb 9, 2022
1 parent 4b760b4 commit 7660b9c
Show file tree
Hide file tree
Showing 77 changed files with 458 additions and 420 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*.ipr
*.iws
.idea
.bloop/
.metals/
null
out
.cache/
.history/
Expand All @@ -28,3 +31,6 @@ node_modules/
.bsp/
.DS_Store
web/.DS_Store

.vscode
project/metals.sbt
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import zio._
import zhttp.http._
import zhttp.service.Server

object HelloWorld extends App {
object HelloWorld extends ZIOAppDefault {
val app = Http.collect[Request] {
case Method.GET -> !! / "text" => Response.text("Hello World!")
}

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
val run =
Server.start(8090, app)
}
```
#### Examples
Expand Down
8 changes: 4 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ object Spec extends DefaultRunnableSpec {
}

def spec = suite("http") (
testM("should be ok") {
test("should be ok") {
val req = ???
val expectedRes = resp => resp.status.toJHttpStatus.code() == Status.OK
assertM(app(req))(expectedRes) // an apply method is added via `zhttp.test` package
Expand Down Expand Up @@ -135,11 +135,11 @@ import zhttp.http._
import zhttp.service.Server
import zio._

object HelloWorld extends App {
object HelloWorld extends ZIOAppDefault {
val app = Http.ok

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
val run =
Server.start(8090, app)
}
```

Expand Down
22 changes: 22 additions & 0 deletions docs/website/docs/advanced-examples/cors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# CORS Handling

```scala
import zhttp.http._
import zhttp.service.Server
import zio._

object HelloWorldWithCORS extends ZIOAppDefault {
// Create HTTP route with CORS enabled
val app: HttpApp[Any, Nothing] = CORS(
Http.collect[Request] {
case Method.GET -> !! / "text" => Response.text("Hello World!")
case Method.GET -> !! / "json" => Response.jsonString("""{"greetings": "Hello World!"}""")
},
config = CORSConfig(anyOrigin = true),
)

// Run it like any simple app
val run =
Server.start(8090, app.silent)
}
```
Empty file.
Empty file.
Empty file added docs/website/docs/index.md
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio._

import java.time.Clock

object Authentication extends App {
object Authentication extends ZIOAppDefault {
// Secret Authentication key
val SECRET_KEY = "secretKey"

Expand Down Expand Up @@ -56,8 +56,8 @@ object Authentication extends App {
val app: UHttpApp = login ++ authenticate(Http.forbidden("Not allowed!"), user)

// Run it like any simple app
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
val run =
Server.start(8090, app)
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@ import zio._
/**
* Example to build app on concrete entity
*/
object ConcreteEntity extends App {
// Request
object ConcreteEntity extends ZIOAppDefault {
//Request
case class CreateUser(name: String)

// Response
//Response
case class UserCreated(id: Long)

val user: Http[Any, Nothing, CreateUser, UserCreated] =
Http.collect[CreateUser] { case CreateUser(_) =>
UserCreated(2)
}

val app: HttpApp[Any, Nothing] =
user
.contramap[Request](req => CreateUser(req.path.toString)) // Http[Any, Nothing, Request, UserCreated]
.map(userCreated => Response.text(userCreated.id.toString)) // Http[Any, Nothing, Request, Response]
val app: Http[Any, Nothing, Request, Response[Any, Nothing]] = user
.contramap[Request](req => CreateUser(req.endpoint._2.toString))
//Http[Any, Nothing, Request, UserCreated]
.map(userCreated => Response.text(userCreated.id.toString))
//Http[Any, Nothing, Request, Response]

// Run it like any simple app
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
val run =
Server.start(8090, app)
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio._

import scala.util.Try

object HelloWorldAdvanced extends App {
object HelloWorldAdvanced extends ZIOAppDefault {
// Set a port
private val PORT = 0

Expand All @@ -27,7 +27,7 @@ object HelloWorldAdvanced extends App {
Server.paranoidLeakDetection ++ // Paranoid leak detection (affects performance)
Server.app(fooBar ++ app) // Setup the Http app

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {
override val run = {
// Configure thread count using CLI
val nThreads: Int = args.headOption.flatMap(x => Try(x.toInt).toOption).getOrElse(0)

Expand All @@ -41,7 +41,6 @@ object HelloWorldAdvanced extends App {
*> ZIO.never,
)
.provideCustomLayer(ServerChannelFactory.auto ++ EventLoopGroup.auto(nThreads))
.exitCode
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import zio._
/**
* Example to encode content using a ZStream
*/
object StreamingResponse extends App {
object StreamingResponse extends ZIOAppDefault {
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {

// Starting the server (for more advanced startup configuration checkout `HelloWorldAdvanced`)
Server.start(8090, app.silent).exitCode
}

// Create a message as a Chunk[Byte]
val message = Chunk.fromArray("Hello world !\r\n".getBytes(HTTP_CHARSET))
// Use `Http.collect` to match on route
Expand All @@ -32,5 +31,11 @@ object StreamingResponse extends App {
data = HttpData.fromStream(ZStream.fromChunk(message)), // Encoding content using a ZStream
)
}
val run = {

// Starting the server (for more advanced startup
// configuration checkout `HelloWorldAdvanced`)
Server.start(8090, app.silent)
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import zio._
import zio.duration._
import zio.stream.ZStream

object WebSocketAdvanced extends App {
object WebSocketAdvanced extends ZIOAppDefault {
// Message Handlers
private val open = Socket.succeed(WebSocketFrame.text("Greetings!"))

Expand Down Expand Up @@ -53,8 +53,8 @@ object WebSocketAdvanced extends App {
case Method.GET -> !! / "subscriptions" => socketApp.toResponse
}

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
val run =
Server.start(8090, app)
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import zhttp.http._
import zhttp.service.Server
import zio._

object HelloWorld extends App {
object HelloWorld extends ZIOAppDefault {

// Create HTTP route
val app: HttpApp[Any, Nothing] = Http.collect[Request] {
Expand All @@ -14,7 +14,7 @@ object HelloWorld extends App {
}

// Run it like any simple app
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app.silent).exitCode
val run =
Server.start(8090, app.silent)
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.io.InputStream
import java.security.KeyStore
import javax.net.ssl.TrustManagerFactory

object HttpsClient extends App {
object HttpsClient extends ZIOAppDefault {
val env = ChannelFactory.auto ++ EventLoopGroup.auto()
val url = "https://sports.api.decathlon.com/groups/water-aerobics"
val headers = Headers.host("sports.api.decathlon.com")
Expand All @@ -35,8 +35,8 @@ object HttpsClient extends App {
_ <- console.putStrLn { data }
} yield ()

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode]
= program.exitCode.provideCustomLayer(env)
override val run =
program.provideCustom(env)

}
```
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
# Simple HTTP Client
```scala
import zhttp.http.Headers
import zhttp.http.{Header, HttpData}
import zhttp.service.{ChannelFactory, Client, EventLoopGroup}
import zio._

object SimpleClient extends App {
object SimpleClient extends ZIOAppDefault {
val env = ChannelFactory.auto ++ EventLoopGroup.auto()
val url = "http://sports.api.decathlon.com/groups/water-aerobics"
val headers = Headers.host("sports.api.decathlon.com")
val headers = List(Header.host("sports.api.decathlon.com"))

val program = for {
res <- Client.request(url, headers)
data <- res.getBodyAsString
_ <- console.putStrLn { data }
res <- Client.request(url, headers)
_ <- console.putStrLn {
res.content match {
case HttpData.CompleteData(data) => data.map(_.toChar).mkString
case HttpData.StreamData(_) => "<Chunked>"
case HttpData.Empty => ""
}
}
} yield ()

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
program.exitCode.provideCustomLayer(env)
override val run =
program.provide(env)

}
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio._
import zio.duration._
import zio.stream.ZStream

object WebSocketEcho extends App {
object WebSocketEcho extends ZIOAppDefault {
private val socket =
Socket.collect[WebSocketFrame] {
case WebSocketFrame.Text("FOO") => ZStream.succeed(WebSocketFrame.text("BAR"))
Expand All @@ -24,8 +24,8 @@ object WebSocketEcho extends App {
case Method.GET -> !! / "subscriptions" => socket.toResponse
}

override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
override val run =
Server.start(8090, app)
}

```
Empty file.
8 changes: 4 additions & 4 deletions example/src/main/scala/example/Authentication.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package example
import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim}
import zhttp.http._
import zhttp.service.Server
import zio.{App, ExitCode, URIO}
import zio._

import java.time.Clock

object Authentication extends App {
object Authentication extends ZIOAppDefault {
// Secret Authentication key
val SECRET_KEY = "secretKey"

Expand Down Expand Up @@ -55,6 +55,6 @@ object Authentication extends App {
val app: UHttpApp = login ++ authenticate(Http.forbidden("Not allowed!"), user)

// Run it like any simple app
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
override val run =
Server.start(8090, app)
}
8 changes: 4 additions & 4 deletions example/src/main/scala/example/BasicAuth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package example
import zhttp.http.Middleware.basicAuth
import zhttp.http._
import zhttp.service.Server
import zio.{App, ExitCode, URIO}
import zio._

object BasicAuth extends App {
object BasicAuth extends ZIOAppDefault {

// Http app that requires a JWT claim
val user: UHttpApp = Http.collect[Request] { case Method.GET -> !! / "user" / name / "greet" =>
Expand All @@ -16,6 +16,6 @@ object BasicAuth extends App {
val app: UHttpApp = user @@ basicAuth("admin", "admin")

// Run it like any simple app
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
val run =
Server.start(8090, app)
}
8 changes: 4 additions & 4 deletions example/src/main/scala/example/CSRF.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import zhttp.http._
import zhttp.service.Server
import zio._

object CSRF extends App {
object CSRF extends ZIOAppDefault {
val privateApp = Http.collect[Request] { case Method.GET -> !! / "unsafeEndpoint" =>
Response.text("secure info")
} @@ csrfValidate() // Check for matching csrf header and cookie
Expand All @@ -14,7 +14,7 @@ object CSRF extends App {
Response.text("hello")
} @@ csrfGenerate() // set x-csrf token cookie

val app = publicApp ++ privateApp
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app).exitCode
val app = publicApp ++ privateApp

def run = Server.start(8090, app)
}
Loading

0 comments on commit 7660b9c

Please sign in to comment.