github.com/cornelk/go-cloud@v0.17.1/aws/awscloud/example_test.go (about)

     1  // Copyright 2018 The Go Cloud Development Kit Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package awscloud_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"net/http"
    22  
    23  	"github.com/cornelk/go-cloud/aws/awscloud"
    24  	"github.com/cornelk/go-cloud/server"
    25  	"github.com/cornelk/go-cloud/server/health"
    26  	"github.com/google/wire"
    27  	"go.opencensus.io/trace"
    28  )
    29  
    30  // This is an example of how to bootstrap an HTTP server running on
    31  // Amazon Web Services (AWS). The code in this function would be
    32  // placed in main().
    33  func Example() {
    34  	// Connect and authenticate to AWS.
    35  	srv, cleanup, err := setup(context.Background())
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  	defer cleanup()
    40  
    41  	// Set up the HTTP routes.
    42  	http.HandleFunc("/", greet)
    43  
    44  	// Run the server. This behaves much like http.ListenAndServe,
    45  	// including that passing a nil handler will use http.DefaultServeMux.
    46  	log.Fatal(srv.ListenAndServe(":8080"))
    47  }
    48  
    49  // setup is a Wire injector function that creates an HTTP server
    50  // configured to send diagnostics to AWS X-Ray. The second return
    51  // value is a clean-up function that can be called to shut down any
    52  // resources created by setup.
    53  //
    54  // The body of this function will be filled in by running Wire. While
    55  // the name of the function does not matter, the signature signals to
    56  // Wire what provider functions to call. See
    57  // https://github.com/google/wire/blob/master/docs/guide.md#injectors
    58  // for more details.
    59  func setup(ctx context.Context) (*server.Server, func(), error) {
    60  	wire.Build(
    61  		// The AWS set includes all the default wiring for AWS, including
    62  		// for *server.Server.
    63  		awscloud.AWS,
    64  		// Providing nil instructs the server to use the default sampling policy.
    65  		wire.Value(trace.Sampler(nil)),
    66  		// Health checks can be added to delay your server reporting healthy
    67  		// to the load balancer before critical dependencies are available.
    68  		wire.Value([]health.Checker(nil)),
    69  	)
    70  	return nil, nil, nil
    71  }
    72  
    73  // greet is an ordinary http.HandleFunc.
    74  func greet(w http.ResponseWriter, req *http.Request) {
    75  	fmt.Fprintln(w, "Hello, World!")
    76  }