github.com/blend/go-sdk@v1.20220411.3/examples/reverseproxy/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"net"
    12  	"net/http"
    13  	"os"
    14  
    15  	"github.com/blend/go-sdk/graceful"
    16  	"github.com/blend/go-sdk/logger"
    17  	"github.com/blend/go-sdk/r2"
    18  	"github.com/blend/go-sdk/reverseproxy"
    19  	"github.com/blend/go-sdk/webutil"
    20  )
    21  
    22  func main() {
    23  	log := logger.Prod()
    24  
    25  	_, err := r2.New("https://www.google.com").CopyTo(os.Stdout)
    26  	if err != nil {
    27  		log.Fatal(err)
    28  		os.Exit(1)
    29  	}
    30  
    31  	upstream := reverseproxy.NewUpstream(webutil.MustParseURL("https://www.google.com"))
    32  	proxy, _ := reverseproxy.NewProxy(
    33  		reverseproxy.OptProxyUpstream(upstream),
    34  		reverseproxy.OptProxySetHeaderValue(webutil.HeaderXForwardedProto, webutil.SchemeHTTPS),
    35  		reverseproxy.OptProxySetHeaderValue(webutil.HeaderXForwardedHost, "www.google.com"),
    36  		reverseproxy.OptProxyLog(log),
    37  	)
    38  
    39  	listener, err := net.Listen("tcp", "127.0.0.1:")
    40  	if err != nil {
    41  		log.Fatal(err)
    42  		os.Exit(1)
    43  	}
    44  	server := http.Server{
    45  		Handler: proxy,
    46  	}
    47  	log.Infof("listening on: %s", listener.Addr().String())
    48  	if err := graceful.Shutdown(webutil.NewGracefulHTTPServer(&server, webutil.OptGracefulHTTPServerListener(listener))); err != nil {
    49  		log.Fatal(err)
    50  		os.Exit(1)
    51  	}
    52  }