go.uber.org/yarpc@v1.72.1/transport/http/inbound_example_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package http_test
    22  
    23  import (
    24  	"fmt"
    25  	"io"
    26  	"log"
    27  	nethttp "net/http"
    28  	"os"
    29  
    30  	"go.uber.org/yarpc"
    31  	"go.uber.org/yarpc/transport/http"
    32  )
    33  
    34  func ExampleInbound() {
    35  	transport := http.NewTransport()
    36  	inbound := transport.NewInbound("127.0.0.1:8888")
    37  
    38  	dispatcher := yarpc.NewDispatcher(yarpc.Config{
    39  		Name:     "myservice",
    40  		Inbounds: yarpc.Inbounds{inbound},
    41  	})
    42  	if err := dispatcher.Start(); err != nil {
    43  		log.Fatal(err)
    44  	}
    45  	defer dispatcher.Stop()
    46  }
    47  
    48  func ExampleMux() {
    49  	// import nethttp "net/http"
    50  
    51  	// We set up a ServeMux which provides a /health endpoint.
    52  	mux := nethttp.NewServeMux()
    53  	mux.HandleFunc("/health", func(w nethttp.ResponseWriter, _ *nethttp.Request) {
    54  		if _, err := fmt.Fprintln(w, "hello from /health"); err != nil {
    55  			panic(err)
    56  		}
    57  	})
    58  
    59  	// This inbound will serve the YARPC service on the path /yarpc.  The
    60  	// /health endpoint on the Mux will be left alone.
    61  	transport := http.NewTransport()
    62  	inbound := transport.NewInbound("127.0.0.1:8888", http.Mux("/yarpc", mux))
    63  
    64  	// Fire up a dispatcher with the new inbound.
    65  	dispatcher := yarpc.NewDispatcher(yarpc.Config{
    66  		Name:     "server",
    67  		Inbounds: yarpc.Inbounds{inbound},
    68  	})
    69  	if err := dispatcher.Start(); err != nil {
    70  		log.Fatal(err)
    71  	}
    72  	defer dispatcher.Stop()
    73  
    74  	// Make a request to the /health endpoint.
    75  	res, err := nethttp.Get("http://127.0.0.1:8888/health")
    76  	if err != nil {
    77  		log.Fatal(err)
    78  	}
    79  	defer res.Body.Close()
    80  
    81  	if _, err := io.Copy(os.Stdout, res.Body); err != nil {
    82  		log.Fatal(err)
    83  	}
    84  	// Output: hello from /health
    85  }
    86  
    87  func ExampleInterceptor() {
    88  	// import nethttp "net/http"
    89  
    90  	// Given a fallback http.Handler
    91  	fallback := nethttp.HandlerFunc(func(w nethttp.ResponseWriter, r *nethttp.Request) {
    92  		io.WriteString(w, "hello, world!")
    93  	})
    94  
    95  	// Create an interceptor that falls back to a handler when the HTTP request is
    96  	// missing the RPC-Encoding header.
    97  	intercept := func(transportHandler nethttp.Handler) nethttp.Handler {
    98  		return nethttp.HandlerFunc(func(w nethttp.ResponseWriter, r *nethttp.Request) {
    99  			if r.Header.Get(http.EncodingHeader) == "" {
   100  				// Not a YARPC request, use fallback handler.
   101  				fallback.ServeHTTP(w, r)
   102  			} else {
   103  				transportHandler.ServeHTTP(w, r)
   104  			}
   105  		})
   106  	}
   107  
   108  	// Create a new inbound, attaching the interceptor
   109  	transport := http.NewTransport()
   110  	inbound := transport.NewInbound("127.0.0.1:8889", http.Interceptor(intercept))
   111  
   112  	// Fire up a dispatcher with the new inbound.
   113  	dispatcher := yarpc.NewDispatcher(yarpc.Config{
   114  		Name:     "server",
   115  		Inbounds: yarpc.Inbounds{inbound},
   116  	})
   117  	if err := dispatcher.Start(); err != nil {
   118  		log.Fatal(err)
   119  	}
   120  	defer dispatcher.Stop()
   121  
   122  	// Make a non-YARPC request to the / endpoint.
   123  	res, err := nethttp.Get("http://127.0.0.1:8889/")
   124  	if err != nil {
   125  		log.Fatal(err)
   126  	}
   127  	defer res.Body.Close()
   128  
   129  	if _, err := io.Copy(os.Stdout, res.Body); err != nil {
   130  		log.Fatal(err)
   131  	}
   132  	// Output: hello, world!
   133  }