Skip to content

Commit

Permalink
improves documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
rvflash committed Mar 18, 2019
1 parent 36e2190 commit 9758fee
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
[![Code Coverage](https://img.shields.io/codecov/c/github/rvflash/tcp.svg)](http://codecov.io/github/rvflash/tcp?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/rvflash/tcp)](https://goreportcard.com/report/github.com/rvflash/tcp)

TCP provides interfaces to create a TCP server.
The package `tcp` provides interfaces to create a TCP server.


### Installation

To install it, you need to install Go and set your Go workspace first.
Download and install it:
Then, download and install it:

```bash
$ go get -u github.com/rvflash/tcp
Expand All @@ -27,6 +27,41 @@ import "github.com/rvflash/tcp"
`tcp` uses the Go modules that required Go 1.11 or later.


## Features

### Handler

Just as Gin, a well done web framework whose provides functions based on HTTP methods,
`tcp` provides functions based on TCP segments.

Thus, it exposes a method for each of its segments:
* `ACK` to handle each new message.
* `FIN` to handle when the connection is closed.
* `SYN` to handle each new connection.

More functions are available, see the [godoc](https://godoc.org/github.com/rvflash/tcp) for more details.

Each of these methods take as parameter the HandlerFunc interface: `func(c *Context)`.
You must implement this interface to create your own handler.


> By analogy with the awesome standard HTTP package, `tcp` exposes and implements
the Handler interface `ServeTCP(ResponseWriter, *Request)`.


### Middleware

By using the `Default` method instead of the `New` to initiate a TCP server,
2 middlewares are defined on each segment.
The first allows to recover on panic, and the second enables logs.


### Custom Middleware

The `Next` method on the `Context` should only be used inside middleware. Its allows to pass to the pending handlers.
See the `Recovery` or `Logger` methods as sample code.


## Quick start

Assuming the following code that runs a server on port 9090:
Expand All @@ -36,26 +71,26 @@ package main

import (
"log"

"github.com/rvflash/tcp"
)

func main() {
// creates a server with a logger and a recover on panic as middlewares.
r := tcp.Default()
r.ACK(func(c *tcp.Context) {
// new message received
// gets the request body
buf, err := c.ReadAll()
if err != nil {
c.Error(err)
}
// writes something as response
c.String(string(buf))
// new message received
// gets the request body
buf, err := c.ReadAll()
if err != nil {
c.Error(err)
}
// writes something as response
c.String(string(buf))
})
err := r.Run(":9090") // listen and serve on 0.0.0.0:9090
if err != nil {
log.Fatalf("listen: %s", err)
log.Fatalf("listen: %s", err)
}
}
```
```

0 comments on commit 9758fee

Please sign in to comment.