ZIO Http A Functional Approach to Scalable and Type-Safe Web Development

knoldus 43 views 13 slides Jun 24, 2024
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

Explore the transformative power of ZIO HTTP - a powerful, purely functional library designed for building highly scalable, concurrent and type-safe HTTP service. Delve into seamless integration of ZIO's powerful features offering a robust foundation for building composable and immutable web app...


Slide Content

Exploring ZIO HTTP Presenter Name: Niloy Choudhury

Lack of etiquette and manners is a huge turn off.   KnolX Etiquettes Punctuality Join the session 5 minutes prior to the session start time. We start on time and conclude on time! Feedback Make sure to submit a constructive feedback for all sessions as it is very helpful for the presenter. Silent Mode Keep your mobile devices in silent mode, feel free to move out of session in case you need to attend an urgent call. Avoid Disturbance Avoid unwanted chit chat during the session.

What is ZIO HTTP Http servers in general Mechanisms offered by ZIO HTTP How to write ZIO HTTP server (syntax)  ZIO HTTP Routing Handlers, Query Params and URL Path Request Response Interception (Middleware) Error Handling Server configuration

What is ZIO HTTP A powerful library for building robust and scalable HTTP servers. ZIO Http offers a functional and type-safe approach to server development. Promotes code that's easy to understand, maintain, and test.

Http servers in general Routing Http Methods Path Variable Query Param Parse Request Request Response Interception Logging Middleware Error Handling Server Configuration

ZIO HTTP Routing A powerful routing mechanism for defining how incoming requests map to specific handlers. Routes are built using a DSL. Plain data, description of route. Not pattern matching or partial function.

Routes Routes Models a collection of  Route s  final case class Routes [ -Env , +Err ]( routes : Chunk [ zio.http. Route [ Env , Err ]]) A Route  consists of two parts:  RoutePattern and  Handler Method .GET / "hello" -> Handler.text ( "hello" ) sealed trait Route [ -Env , +Err ]   RoutePattern define how URLs map to handlers. Patterns can include literal strings, wildcards for any path segment, and path parameters. The Handler  produces response to the matching request.

Handler A Handler is responsible for processing the matched incoming HTTP requests and generating appropriate response. sealed trait Handler [ -R , +Err , -In , +Out ] type RequestHandler [ -R , +Err ] = Handler [ R , Err , Request , Response ] Function that takes a Request , including headers, body, and parameters.  Generates a Response object. Response including status code, headers, and an optional body. Creating Handler  Using companion object    Method . GET / "hello"   -> Handler . text ( "hello !" )       Method . GET / "forbidden" -> Handler . error ( Status . Forbidden ) Using smart constructor handler   Method . GET / Root -> handler ( Response . text ( "Greetings at your service" ))

Request ZIO HTTP  Request  represents a Http request object Accessing incoming Request Method . POST / "echo"  -> handler { req : Request =>   req . body . asString . map ( Response . text (_)) }. sandbox Query Params (Not part of the route) Method . GET / "search" -> handler { ( req : Request ) =>   val queries: Option [ String ] = req . queryParam ( "q" )    ... final case class Request (   version : Version = Version . Default ,   method : Method = Method . ANY ,   url : URL = URL . empty ,   headers : Headers = Headers . empty ,   body : Body = Body . empty ,   remoteAddress : Option [ InetAddress ] = None ,   remoteCertificate : Option [ Certificate ] = None , ) extends HeaderOps [ Request ] Request object

Request Response Interception (Middleware) Intercept HTTP requests and responses before they reach the handler. Implement cross-cutting concerns like logging, validation. Accepts a  Routes  and produces a new  Routes . Middleware can be chained together to form a processing pipeline for requests and responses. trait Middleware [ - UpperEnv ] { self =>   def apply [ Env1 <: UpperEnv , Err ]( app : Routes [ Env1 , Err ]): Routes [ Env1 , Err ] ... private val logWithUserAgent : Middleware [Any] =   Middleware . logAnnotateHeaders ( Header . UserAgent ) routes @@ requestLogging @@ logWithUserAgent

Server Configuration ZIO HTTP server provides methods to install HTTP applications into the server. Offers a comprehensive  Config  class for control over server behavior, helps to configure SSL/TLS address binding request decompression response compression Serve routes using the  Server.serve  method Server . serve ( routes ). provide ( Server . default ) Customizing config Server . serve ( ZIORoutes . routes )   . provide ( ZLayer . succeed ( Server . Config . default . port ( 8081 )),     Server . live   )

Demo
Tags