gocloud.dev: gocloud.dev/gcp/gcpcloud Index | Examples | Files

package gcpcloud

import "gocloud.dev/gcp/gcpcloud"

Package gcpcloud contains Wire providers for GCP services.

This is an example of how to bootstrap an HTTP server running on Google Cloud Platform (GCP). The code in this function would be placed in main().

Code:

package main

import (
    "context"
    "fmt"
    "log"
    "net/http"

    "github.com/google/wire"
    "go.opencensus.io/trace"
    "gocloud.dev/gcp/gcpcloud"
    "gocloud.dev/server"
    "gocloud.dev/server/health"
)

// This is an example of how to bootstrap an HTTP server running on
// Google Cloud Platform (GCP). The code in this function would be
// placed in main().
func main() {
    // Connect and authenticate to GCP.
    srv, cleanup, err := setup(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    defer cleanup()

    // Set up the HTTP routes.
    http.HandleFunc("/", greet)

    // Run the server. This behaves much like http.ListenAndServe,
    // including that passing a nil handler will use http.DefaultServeMux.
    log.Fatal(srv.ListenAndServe(":8080"))
}

// setup is a Wire injector function that creates an HTTP server
// configured to send diagnostics to Stackdriver. The second return
// value is a clean-up function that can be called to shut down any
// resources created by setup.
//
// The body of this function will be filled in by running Wire. While
// the name of the function does not matter, the signature signals to
// Wire what provider functions to call. See
// https://github.com/google/wire/blob/master/docs/guide.md#injectors
// for more details.
func setup(ctx context.Context) (*server.Server, func(), error) {
    wire.Build(
        // The GCP set includes all the default wiring for GCP, including
        // for *server.Server.
        gcpcloud.GCP,
        // Providing nil instructs the server to use the default sampling policy.
        wire.Value(trace.Sampler(nil)),
        // Health checks can be added to delay your server reporting healthy
        // to the load balancer before critical dependencies are available.
        wire.Value([]health.Checker{}),
    )
    return nil, nil, nil
}

// greet is an ordinary http.HandleFunc.
func greet(w http.ResponseWriter, req *http.Request) {
    fmt.Fprintln(w, "Hello, World!")
}

Index

Examples

Package Files

gcpcloud.go

Variables

var GCP = wire.NewSet(Services, gcp.DefaultIdentity)

GCP is a Wire provider set that includes all Google Cloud Platform services in this repository and authenticates using Application Default Credentials.

var Services = wire.NewSet(
    gcp.DefaultTransport,
    gcp.NewHTTPClient,

    gcpruntimeconfig.Set,
    gcpkms.Set,
    gcppubsub.Set,
    gcsblob.Set,
    cloudsql.CertSourceSet,
    gcpfirestore.Set,
    sdserver.Set,
)

Services is a Wire provider set that includes the default wiring for all Google Cloud Platform services in this repository, but does not include credentials. Individual services may require additional configuration.

Package gcpcloud imports 9 packages (graph). Updated 2019-07-22. Refresh now. Tools for package owners.