Skip to content

Commit

Permalink
Added transparent compression support to helloworldserver example
Browse files Browse the repository at this point in the history
  • Loading branch information
valyala committed Dec 30, 2015
1 parent 0d58868 commit 842cd85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion examples/helloworldserver/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# HelloWorld server example

Displays various request info.
* Displays various request info.
* Sets response headers and cookies.
* Supports transparent compression.

# How to build

Expand Down
21 changes: 19 additions & 2 deletions examples/helloworldserver/helloworldserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import (
"github.com/valyala/fasthttp"
)

var addr = flag.String("addr", ":8080", "TCP address to listen to")
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
compress = flag.Bool("compress", false, "Whether to enable transparent response compression")
)

func main() {
flag.Parse()

if err := fasthttp.ListenAndServe(*addr, requestHandler); err != nil {
h := requestHandler
if *compress {
h = fasthttp.CompressHandler(h)
}

if err := fasthttp.ListenAndServe(*addr, h); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
Expand All @@ -35,4 +43,13 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)

ctx.SetContentType("text/plain; charset=utf8")

// Set arbitrary headers
ctx.Response.Header.Set("X-My-Header", "my-header-value")

// Set cookies
var c fasthttp.Cookie
c.SetKey("cookie-name")
c.SetValue("cookie-value")
ctx.Response.Header.SetCookie(&c)
}

0 comments on commit 842cd85

Please sign in to comment.