forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytebuffer_example_test.go
More file actions
29 lines (23 loc) · 855 Bytes
/
bytebuffer_example_test.go
File metadata and controls
29 lines (23 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package fasthttp_test
import (
"fmt"
"github.com/valyala/fasthttp"
)
func ExampleByteBuffer() {
// This request handler sets 'Your-IP' response header
// to 'Your IP is <ip>'. It uses ByteBuffer for constructing response
// header value with zero memory allocations.
yourIPRequestHandler := func(ctx *fasthttp.RequestCtx) {
b := fasthttp.AcquireByteBuffer()
b.B = append(b.B, "Your IP is <"...)
b.B = fasthttp.AppendIPv4(b.B, ctx.RemoteIP())
b.B = append(b.B, ">"...)
ctx.Response.Header.SetBytesV("Your-IP", b.B)
fmt.Fprintf(ctx, "Check response headers - they must contain 'Your-IP: %s'", b.B)
// It is safe to release byte buffer now, since it is
// no longer used.
fasthttp.ReleaseByteBuffer(b)
}
// Start fasthttp server returning your ip in response headers.
fasthttp.ListenAndServe(":8080", yourIPRequestHandler)
}