-
-
Save vkuznecovas/a3dfb81802d30e97f5df7bceb225715a to your computer and use it in GitHub Desktop.
fasthttp-test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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