Skip to content

Commit

Permalink
Refactor: Support middlewares on Http (#773)
Browse files Browse the repository at this point in the history
* refactor: remove type-params from Response

* chore: self review

* refactor: rename Middleware to HttpMiddleware

* refactor: add `@@@` to Http as an alternative to `@@`.

* feature: add new Middleware API

* feature: add `flatten` and `ifThenElse`

* feature: add `ifThenElseZIO`

* refactor: fix type params for `identity`

* feature: add `when`

* feature: add `make` constructor

* refactor: make middleware methods final

* refactor: git remains

* refactor: implement HttpMiddleware as Middleware

* scala3 fix

* Refactor CORS middleware (#788)

* Refactor/merge middleware and http middleware (#790)

* Refactor move cors and timeout

* move some httpMiddlewares to Middleware

* move some AuthMiddleware to Middleware

* move remaining AuthMiddleware to Middleware

* move Middlewares to middleware package

* scaladoc

* codec example

* move Middleware to http package

* named alias for `@@`

* rename Auth to AuthMiddlewares

* rename CORSMiddleware to CorsMiddlewares

* rename CSRF to CsrfMiddlewares

* make primitives private

* rename MiddlewareExtensions to HttpMiddlewares

* rename operators in HttpMiddlewares

* scalaDoc

* arg rename

* doc update and general refactor

* simplify cors middleware

* rename CorsConfig

* renames

* Make middlewares package private MiddlewareRequest

* Introduce MiddlewareRequest (#798)

* Introduce MiddlewareRequest

* PR review comments

* Refactor move runAfter to Middleware

* refactor: add `UMiddleware`

* feature: add `contramapZIO`

* refactor: move cors config to Cors file

* refactor: rename files

* refactor: remove AuthSpec from WebSpec

* refactor: fix naming for Http operators

* refactor: add partial type suport for contraMapZIO

* Refactor: Codec (#841)

* Add Run Before (#840)

* Add Run Before

* Add Run Before and After

* use renamed operator

* refactor: add partial type suport for contraMap

* Implement missing operators in Middleware (#807)

* Implement missing operators in Middleware

* fix as operator

* headers Middleware changes

* sign cookie

* extend with HeaderExtensions

* rename suite

* PR comments

* refactor: use `Request` instead of `MiddlewareRequest`

* refactor: rename methods

* refactor: resolve fix me issue

Co-authored-by: amitsingh <amitksingh1490@gmail.com>
Co-authored-by: Amit Kumar Singh <amit.singh@dream11.com>
  • Loading branch information
3 people authored Jan 18, 2022
1 parent 13c7d2b commit e9abd9f
Show file tree
Hide file tree
Showing 17 changed files with 1,079 additions and 631 deletions.
7 changes: 4 additions & 3 deletions example/src/main/scala/example/HelloWorldWithCORS.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package example

import zhttp.http.Middleware.cors
import zhttp.http.{CORSConfig, HttpApp, Method, Response, _}
import zhttp.http._
import zhttp.http.middleware.Cors.CorsConfig
import zhttp.service.Server
import zio.{App, ExitCode, URIO}

object HelloWorldWithCORS extends App {

// Create CORS configuration
val config: CORSConfig =
CORSConfig(allowedOrigins = _ == "dev", allowedMethods = Some(Set(Method.PUT, Method.DELETE)))
val config: CorsConfig =
CorsConfig(allowedOrigins = _ == "dev", allowedMethods = Some(Set(Method.PUT, Method.DELETE)))

// Create HTTP route with CORS enabled
val app: HttpApp[Any, Nothing] =
Expand Down
12 changes: 6 additions & 6 deletions example/src/main/scala/example/HelloWorldWithMiddlewares.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package example

import zhttp.http.Middleware.{addHeader, debug, patchZIO, timeout}
import zhttp.http._
import zhttp.http.middleware.HttpMiddleware
import zhttp.service.Server
import zio.clock.{Clock, currentTime}
import zio.console.Console
Expand All @@ -20,20 +20,20 @@ object HelloWorldWithMiddlewares extends App {
case Method.GET -> !! / "long-running" => ZIO.succeed(Response.text("Hello World!")).delay(5 seconds)
}

val serverTime: Middleware[Clock, Nothing] = patchZIO((_, _) =>
val serverTime: HttpMiddleware[Clock, Nothing] = Middleware.patchZIO(_ =>
for {
currentMilliseconds <- currentTime(TimeUnit.MILLISECONDS)
withHeader = Patch.addHeader("X-Time", currentMilliseconds.toString)
} yield withHeader,
)

val middlewares: Middleware[Console with Clock, IOException] =
val middlewares: HttpMiddleware[Console with Clock, IOException] =
// print debug info about request and response
debug ++
Middleware.debug ++
// close connection if request takes more than 3 seconds
timeout(3 seconds) ++
Middleware.timeout(3 seconds) ++
// add static header
addHeader("X-Environment", "Dev") ++
Middleware.addHeader("X-Environment", "Dev") ++
// add dynamic header
serverTime

Expand Down
19 changes: 0 additions & 19 deletions zio-http/src/main/scala/zhttp/http/CORS.scala

This file was deleted.

24 changes: 14 additions & 10 deletions zio-http/src/main/scala/zhttp/http/Http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>

import Http._

/**
* Attaches the provided middleware to the Http app
*/
final def @@[R1 <: R, E1 >: E, A1 <: A, B1 >: B, A2, B2](
mid: Middleware[R1, E1, A1, B1, A2, B2],
): Http[R1, E1, A2, B2] = mid(self)

/**
* Alias for flatmap
*/
Expand Down Expand Up @@ -180,6 +187,13 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>
final def mapZIO[R1 <: R, E1 >: E, C](bFc: B => ZIO[R1, E1, C]): Http[R1, E1, A, C] =
self >>> Http.fromFunctionZIO(bFc)

/**
* Named alias for @@
*/
final def middleware[R1 <: R, E1 >: E, A1 <: A, B1 >: B, A2, B2](
mid: Middleware[R1, E1, A1, B1, A2, B2],
): Http[R1, E1, A2, B2] = mid(self)

/**
* Named alias for `<>`
*/
Expand Down Expand Up @@ -342,16 +356,6 @@ object Http {
implicit final class HttpAppSyntax[-R, +E](val http: HttpApp[R, E]) extends HeaderModifier[HttpApp[R, E]] {
self =>

/**
* Attaches the provided middleware to the HttpApp
*/
def @@[R1 <: R, E1 >: E](mid: Middleware[R1, E1]): HttpApp[R1, E1] = middleware(mid)

/**
* Attaches the provided middleware to the HttpApp
*/
def middleware[R1 <: R, E1 >: E](mid: Middleware[R1, E1]): HttpApp[R1, E1] = mid(http)

/**
* Patches the response produced by the app
*/
Expand Down
Loading

0 comments on commit e9abd9f

Please sign in to comment.