func ExampleServe() { // Create network listener for accepting incoming requests. // // Note that you are not limited by TCP listener - arbitrary // net.Listener may be used by the server. // For example, unix socket listener or TLS listener. ln, err := net.Listen("tcp4", "127.0.0.1:8080") if err != nil { log.Fatalf("error in net.Listen: %s", err) } // This function will be called by the server for each incoming request. // // RequestCtx provides a lot of functionality related to http request // processing. See RequestCtx docs for details. requestHandler := func(ctx *fasthttp.RequestCtx) { fmt.Fprintf(ctx, "Hello, world! Requested path is %q", ctx.Path()) } // Start the server with default settings. // Create Server instance for adjusting server settings. // // Serve returns on ln.Close() or error, so usually it blocks forever. if err := fasthttp.Serve(ln, requestHandler); err != nil { log.Fatalf("error in Serve: %s", err) } }
func main() { flag.Parse() ln := getListener() if err := fasthttp.Serve(ln, requestHandler); err != nil { log.Fatalf("Error in ListenAndServe: %s", err) } }
func ExampleListen() { ln, err := reuseport.Listen("tcp4", "localhost:12345") if err != nil { log.Fatalf("error in reuseport listener: %s", err) } if err = fasthttp.Serve(ln, requestHandler); err != nil { log.Fatalf("error in fasthttp Server: %s", err) } }
func startServer(b *testing.B, ln *fasthttputil.InmemoryListener, h fasthttp.RequestHandler, isTLS bool) <-chan struct{} { ch := make(chan struct{}) go func() { var err error if isTLS { err = fasthttp.ServeTLS(ln, certFile, keyFile, h) } else { err = fasthttp.Serve(ln, h) } if err != nil { b.Fatalf("unexpected error in server: %s", err) } close(ch) }() return ch }