Skip to content

Commit

Permalink
Doc: Add outline (#815)
Browse files Browse the repository at this point in the history
* doc: add categories

* refactor: doc structure refactored

* fix: package-lock.json removed

* refactor: create outline sub-directories

* refactor: rename test to testing
  • Loading branch information
girdharshubham authored Jan 12, 2022
1 parent 2f0eb65 commit 0ffb4d6
Show file tree
Hide file tree
Showing 46 changed files with 230 additions and 11,118 deletions.
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
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"label": "Basic Examples",
"position": 2
"position": 1
}
150 changes: 150 additions & 0 deletions docs/website/docs/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
---
sidebar_position: 2
---

# Getting Started

## Http

### Creating a "_Hello World_" app

```scala
import zhttp.http._

val app = Http.text("Hello World!")
```

An application can be made using any of the available operators on `zhttp.Http`. In the above program for any Http request, the response is always `"Hello World!"`.

### Routing

```scala
import zhttp.http._

val app = Http.collect[Request] {
case Method.GET -> Root / "fruits" / "a" => Response.text("Apple")
case Method.GET -> Root / "fruits" / "b" => Response.text("Banana")
}
```

Pattern matching on route is supported by the framework

### Composition

```scala
import zhttp.http._

val a = Http.collect[Request] { case Method.GET -> Root / "a" => Response.ok }
val b = Http.collect[Request] { case Method.GET -> Root / "b" => Response.ok }

val app = a <> b
```

Apps can be composed using the `<>` operator. The way it works is, if none of the routes match in `a` , or a `NotFound` error is thrown from `a`, and then the control is passed on to the `b` app.

### ZIO Integration

```scala
val app = Http.collectM[Request] {
case Method.GET -> Root / "hello" => ZIO.succeed(Response.text("Hello World"))
}
```

`Http.collectM` allow routes to return a ZIO effect value.

### Accessing the Request

```scala
import zhttp.http._

val app = Http.collect[Request] {
case req @ Method.GET -> Root / "fruits" / "a" =>
Response.text("URL:" + req.url.path.asString + " Headers: " + r.headers)
case req @ Method.POST -> Root / "fruits" / "a" =>
Response.text(req.getBodyAsString.getOrElse("No body!"))
}
```

### Testing

zhttp provides a `zhttp-test` package for use in unit tests. You can utilize it as follows:

```scala
import zio.test._
import zhttp.test._
import zhttp.http._

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

def spec = suite("http") (
testM("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
}
)
}
```

```scala
import zhttp.http._

val app = Http.collect[Request] {
case req @ Method.GET -> Root / "fruits" / "a" =>
Response.text("URL:" + req.url.path.asString + " Headers: " + r.headers)
case req @ Method.POST -> Root / "fruits" / "a" =>
Response.text(req.getBodyAsString.getOrElse("No body!"))
}
```

## Socket

### Creating a socket app

```scala
import zhttp.socket._

private val socket = Socket.collect[WebSocketFrame] {
case WebSocketFrame.Text("FOO") => ZStream.succeed(WebSocketFrame.text("BAR"))
}

private val app = Http.collect[Request] {
case Method.GET -> Root / "greet" / name => Response.text(s"Greetings {$name}!")
case Method.GET -> Root / "ws" => Response.socket(socket)
}
```

## Server

### Starting an Http App

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

object HelloWorld extends App {
val app = Http.ok

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

A simple Http app that responds with empty content and a `200` status code is deployed on port `8090` using `Server.start`.

## Examples

- [Simple Server](https://github.com/dream11/zio-http/blob/main/example/src/main/scala/HelloWorld.scala)
- [Advanced Server](https://github.com/dream11/zio-http/blob/main/example/src/main/scala/HelloWorldAdvanced.scala)
- [WebSocket Server](https://github.com/dream11/zio-http/blob/main/example/src/main/scala/SocketEchoServer.scala)
- [Streaming Response](https://github.com/dream11/zio-http/blob/main/example/src/main/scala/StreamingResponse.scala)
- [Simple Client](https://github.com/dream11/zio-http/blob/main/example/src/main/scala/SimpleClient.scala)
- [File Streaming](https://github.com/dream11/zio-http/blob/main/example/src/main/scala/FileStreaming.scala)
- [Authentication](https://github.com/dream11/zio-http/blob/main/example/src/main/scala/Authentication.scala)



Loading

0 comments on commit 0ffb4d6

Please sign in to comment.