Skip to content

Instantly share code, notes, and snippets.

@vkuznecovas
Created January 26, 2018 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkuznecovas/a3dfb81802d30e97f5df7bceb225715a to your computer and use it in GitHub Desktop.
Save vkuznecovas/a3dfb81802d30e97f5df7bceb225715a to your computer and use it in GitHub Desktop.
fasthttp-test
package main
import (
"flag"
"fmt"
"log"
"strings"
"github.com/valyala/fasthttp"
)
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()
h := requestHandler
if err := fasthttp.ListenAndServe(*addr, h); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("text/plain; charset=utf8")
xForwardedHeader := string(ctx.Request.Header.Peek("X-Forwarded-For")[:])
if xForwardedHeader == "" {
fmt.Fprintf(ctx, "%v", ctx.RemoteIP())
return
}
xForwardedSplit := strings.Split(xForwardedHeader, ",")
if len(xForwardedSplit) > 0 {
fmt.Fprintf(ctx, "%v", xForwardedSplit[0])
} else {
fmt.Fprintf(ctx, "%v", ctx.RemoteIP())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment