Skip to content

Commit

Permalink
Merge branch 'main' into refactor/middleware-http-support
Browse files Browse the repository at this point in the history
# Conflicts:
#	zio-http/src/main/scala/zhttp/http/HExit.scala
#	zio-http/src/main/scala/zhttp/http/Http.scala
#	zio-http/src/main/scala/zhttp/http/Middleware.scala
#	zio-http/src/test/scala/zhttp/http/HExitSpec.scala
#	zio-http/src/test/scala/zhttp/middleware/MiddlewareSpec.scala
  • Loading branch information
tusharmath committed Jan 15, 2022
2 parents 0d9ec11 + 9ee2048 commit 3b47e6f
Show file tree
Hide file tree
Showing 73 changed files with 668 additions and 11,527 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.6]
scala: [2.13.7]
java: [temurin@11]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -234,7 +234,7 @@ jobs:
strategy:
matrix:
os: [centos]
scala: [2.13.6]
scala: [2.13.7]
java: [temurin@11]
runs-on: [ "${{ matrix.os }}", zio-http ]
steps:
Expand All @@ -259,15 +259,15 @@ jobs:
run: |
cp ./zio-http/example/src/main/scala/example/PlainTextBenchmarkServer.scala ./FrameworkBenchMarks/frameworks/Scala/zio-http/src/main/scala/Main.scala
cd ./FrameworkBenchMarks
echo ${{github.event.pull_request.head.sha}}
sed -i "s/---COMMIT_SHA---/${{github.event.pull_request.head.sha}}/g" frameworks/Scala/zio-http/build.sbt
sed -i "s/---COMMIT_SHA---/${{github.event.pull_request.head.repo.owner.login}}\/zio-http.git#${{github.event.pull_request.head.sha}}/g" frameworks/Scala/zio-http/build.sbt
./tfb --test zio-http | tee result
RESULT_REQUEST=$(echo $(grep -B 1 -A 17 "Concurrency: 256 for plaintext" result) | grep -oiE "requests/sec: [0-9]+.[0-9]+")
RESULT_CONCURRENCY=$(echo $(grep -B 1 -A 17 "Concurrency: 256 for plaintext" result) | grep -oiE "concurrency: [0-9]+")
echo ::set-output name=request_result::$(echo $RESULT_REQUEST)
echo ::set-output name=concurrency_result::$(echo $RESULT_CONCURRENCY)
- uses: peter-evans/commit-comment@v1
- if: ${{github.event.pull_request.head.repo.full_name == 'dream11/zio-http'}}
uses: peter-evans/commit-comment@v1
with:
sha: ${{github.event.pull_request.head.sha}}
body: |
Expand Down
22 changes: 0 additions & 22 deletions docs/website/docs/advanced-examples/cors.md

This file was deleted.

48 changes: 0 additions & 48 deletions docs/website/docs/advanced-examples/sticky-threads.md

This file was deleted.

27 changes: 0 additions & 27 deletions docs/website/docs/advanced-examples/stream-file.md

This file was deleted.

4 changes: 4 additions & 0 deletions docs/website/docs/client/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Client",
"position": 4
}
1 change: 1 addition & 0 deletions docs/website/docs/client/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "DSL",
"position": 3
}
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/cookies/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Cookies",
"position": 6
}
1 change: 1 addition & 0 deletions docs/website/docs/dsl/cookies/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/headers/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Headers",
"position": 5
}
1 change: 1 addition & 0 deletions docs/website/docs/dsl/headers/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/http-data/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "HttpData",
"position": 4
}
1 change: 1 addition & 0 deletions docs/website/docs/dsl/http-data/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/http-endpoint/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Endpoint",
"position": 7
}
1 change: 1 addition & 0 deletions docs/website/docs/dsl/http-endpoint/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/http/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Http",
"position": 1
}
1 change: 1 addition & 0 deletions docs/website/docs/dsl/http/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/middleware/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Middleware",
"position": 9
}
1 change: 1 addition & 0 deletions docs/website/docs/dsl/middleware/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/request/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Request",
"position": 2
}
1 change: 1 addition & 0 deletions docs/website/docs/dsl/request/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/response/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Response",
"position": 3
}
1 change: 1 addition & 0 deletions docs/website/docs/dsl/response/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/server/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Server",
"position": 8
}
3 changes: 3 additions & 0 deletions docs/website/docs/dsl/server/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Config

Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/dsl/socket/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Socket",
"position": 10
}
1 change: 1 addition & 0 deletions docs/website/docs/dsl/socket/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Work in progress
4 changes: 4 additions & 0 deletions docs/website/docs/examples/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Examples",
"position": 7
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"label": "Advanced Examples",
"position": 3
"position": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

```scala
import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim}
import zhttp.http.{Method, _}
import zhttp.http._
import zhttp.service.Server
import zio._

Expand All @@ -27,36 +27,33 @@ object Authentication extends App {
}

// Authentication middleware
// Takes in a Failing HttpApp and a Succeed HttpApp which are
// called based on Authentication success or failure
// Takes in a Failing HttpApp and a Succeed HttpApp which are called based on Authentication success or failure
// For each request tries to read the `X-ACCESS-TOKEN` header
// Validates JWT Claim
def authenticate[R, E](fail: HttpApp[R, E], success: JwtClaim => HttpApp[R, E]): HttpApp[R, E] =
Http.flatten {
Http.fromFunction[Request] {
def authenticate[R, E](fail: HttpApp[R, E], success: JwtClaim => HttpApp[R, E]): HttpApp[R, E] =
Http
.fromFunction[Request] {
_.getHeader("X-ACCESS-TOKEN")
.flatMap(header => jwtDecode(header.value.toString))
.fold[HttpApp[R, E]](fail)(success)
}
}
.flatMap(header => jwtDecode(header._2.toString))
.fold[HttpApp[R, E]](fail)(success)
}
.flatten

// Http app that requires a JWT claim
def user(claim: JwtClaim): UHttpApp = Http.collect {
case Method.GET -> !! / "user" / name / "greet" =>
Response.text(s"Welcome to the ZIO party! ${name}")
case Method.GET -> !! / "user" / "expiration" =>
Response.text(s"Expires in: ${claim.expiration.getOrElse(-1L)}")
def user(claim: JwtClaim): UHttpApp = Http.collect[Request] {
case Method.GET -> !! / "user" / name / "greet" => Response.text(s"Welcome to the ZIO party! ${name}")
case Method.GET -> !! / "user" / "expiration" => Response.text(s"Expires in: ${claim.expiration.getOrElse(-1L)}")
}

// App that let's the user login
// Login is successful only if the password is the reverse of the username
def login: UHttpApp = Http.collect { case Method.GET -> !! / "login" / username / password =>
def login: UHttpApp = Http.collect[Request] { case Method.GET -> !! / "login" / username / password =>
if (password.reverse == username) Response.text(jwtEncode(username))
else Response.fromHttpError(HttpError.Unauthorized("Invalid username of password\n"))
}

// Composing all the HttpApps together
val app: UHttpApp = login +++ authenticate(Http.forbidden("Not allowed!"), user)
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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import zio._
* Example to build app on concrete entity
*/
object ConcreteEntity extends App {
//Request
// 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: 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]
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]

// Run it like any simple app
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Expand Down
24 changes: 24 additions & 0 deletions docs/website/docs/examples/advanced-examples/cors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# CORS Handling

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

object HelloWorldWithCORS extends App {
// Create CORS configuration
val config: CORSConfig =
CORSConfig(allowedOrigins = _ == "dev", allowedMethods = Some(Set(Method.PUT, Method.DELETE)))

// Create HTTP route with CORS enabled
val app: HttpApp[Any, Nothing] =
Http.collect[Request] {
case Method.GET -> !! / "text" => Response.text("Hello World!")
case Method.GET -> !! / "json" => Response.json("""{"greetings": "Hello World!"}""")
} @@ cors(config)

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

3 comments on commit 3b47e6f

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 846285.78

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 833175.83

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 840485.21

Please sign in to comment.