Move to vendor

Signed-off-by: Olivier Gambier <olivier@docker.com>
This commit is contained in:
Olivier Gambier 2016-03-18 14:07:13 -07:00
parent c8d8e7e357
commit 77e69b9cf3
1268 changed files with 34 additions and 24 deletions

View file

@ -0,0 +1,30 @@
# Cloud Bigtable on Managed VMs using Go
# (Hello World for Cloud Bigtable)
This app counts how often each user visits.
## Prerequisites
1. Set up Cloud Console.
1. Go to the [Cloud Console](https://cloud.google.com/console) and create or select your project.
You will need the project ID later.
1. Go to **Settings > Project Billing Settings** and enable billing.
1. Select **APIs & Auth > APIs**.
1. Enable the **Cloud Bigtable API** and the **Cloud Bigtable Admin API**.
(You may need to search for the API).
1. Set up gcloud.
1. `gcloud components update`
1. `gcloud auth login`
1. `gcloud config set project PROJECT_ID`
1. Download App Engine SDK for Go.
1. `go get -u google.golang.org/appengine/...`
1. In helloworld.go, change the constants `project`, `zone` and `cluster`
## Running locally
1. From the sample project folder, `gcloud preview app run app.yaml`
## Deploying on Google App Engine Managed VM
1. Install and start [Docker](https://cloud.google.com/appengine/docs/managed-vms/getting-started#install_docker).
1. From the sample project folder, `aedeploy gcloud preview app deploy app.yaml`

View file

@ -0,0 +1,11 @@
runtime: go
api_version: go1
vm: true
manual_scaling:
instances: 1
handlers:
# Serve only the web root.
- url: /
script: _go_app

View file

@ -0,0 +1,169 @@
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
/*
helloworld tracks how often a user has visited the index page.
This program demonstrates usage of the Cloud Bigtable API for Managed VMs and Go.
Instructions for running this program are in the README.md.
*/
package main
import (
"bytes"
"encoding/binary"
"html/template"
"log"
"net/http"
"golang.org/x/net/context"
"google.golang.org/appengine"
aelog "google.golang.org/appengine/log"
"google.golang.org/appengine/user"
"google.golang.org/cloud/bigtable"
)
// User-provided constants.
const (
project = "PROJECT_ID"
zone = "CLUSTER_ZONE"
cluster = "CLUSTER_NAME"
)
var (
tableName = "bigtable-hello"
familyName = "emails"
// Client is initialized by main.
client *bigtable.Client
)
func main() {
ctx := context.Background()
// Set up admin client, tables, and column families.
// NewAdminClient uses Application Default Credentials to authenticate.
adminClient, err := bigtable.NewAdminClient(ctx, project, zone, cluster)
if err != nil {
log.Fatalf("Unable to create a table admin client. %v", err)
}
tables, err := adminClient.Tables(ctx)
if err != nil {
log.Fatalf("Unable to fetch table list. %v", err)
}
if !sliceContains(tables, tableName) {
if err := adminClient.CreateTable(ctx, tableName); err != nil {
log.Fatalf("Unable to create table: %v. %v", tableName, err)
}
}
tblInfo, err := adminClient.TableInfo(ctx, tableName)
if err != nil {
log.Fatalf("Unable to read info for table: %v. %v", tableName, err)
}
if !sliceContains(tblInfo.Families, familyName) {
if err := adminClient.CreateColumnFamily(ctx, tableName, familyName); err != nil {
log.Fatalf("Unable to create column family: %v. %v", familyName, err)
}
}
adminClient.Close()
// Set up Bigtable data operations client.
// NewClient uses Application Default Credentials to authenticate.
client, err = bigtable.NewClient(ctx, project, zone, cluster)
if err != nil {
log.Fatalf("Unable to create data operations client. %v", err)
}
http.Handle("/", appHandler(mainHandler))
appengine.Main() // Never returns.
}
// mainHandler tracks how many times each user has visited this page.
func mainHandler(w http.ResponseWriter, r *http.Request) *appError {
if r.URL.Path != "/" {
http.NotFound(w, r)
return nil
}
ctx := appengine.NewContext(r)
u := user.Current(ctx)
if u == nil {
login, err := user.LoginURL(ctx, r.URL.String())
if err != nil {
return &appError{err, "Error finding login URL", http.StatusInternalServerError}
}
http.Redirect(w, r, login, http.StatusFound)
return nil
}
logoutURL, err := user.LogoutURL(ctx, "/")
if err != nil {
return &appError{err, "Error finding logout URL", http.StatusInternalServerError}
}
// Display hello page.
tbl := client.Open(tableName)
rmw := bigtable.NewReadModifyWrite()
rmw.Increment(familyName, u.Email, 1)
row, err := tbl.ApplyReadModifyWrite(ctx, u.Email, rmw)
if err != nil {
return &appError{err, "Error applying ReadModifyWrite to row: " + u.Email, http.StatusInternalServerError}
}
data := struct {
Username, Logout string
Visits uint64
}{
Username: u.Email,
// Retrieve the most recently edited column.
Visits: binary.BigEndian.Uint64(row[familyName][0].Value),
Logout: logoutURL,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return &appError{err, "Error writing template", http.StatusInternalServerError}
}
buf.WriteTo(w)
return nil
}
var tmpl = template.Must(template.New("").Parse(`
<html><body>
<p>
{{with .Username}} Hello {{.}}{{end}}
{{with .Logout}}<a href="{{.}}">Sign out</a>{{end}}
</p>
<p>
You have visited {{.Visits}}
</p>
</body></html>`))
// sliceContains reports whether the provided string is present in the given slice of strings.
func sliceContains(list []string, target string) bool {
for _, s := range list {
if s == target {
return true
}
}
return false
}
// More info about this method of error handling can be found at: http://blog.golang.org/error-handling-and-go
type appHandler func(http.ResponseWriter, *http.Request) *appError
type appError struct {
Error error
Message string
Code int
}
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if e := fn(w, r); e != nil {
ctx := appengine.NewContext(r)
aelog.Errorf(ctx, "%v", e.Error)
http.Error(w, e.Message, e.Code)
}
}