| 1 |
// +build ignore |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright 2016 The Go4 Authors. |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
// The serve_on_cloud program deploys an HTTP server on Google Compute Engine, |
| 20 |
// serving from Google Cloud Storage. Its purpose is to help testing |
| 21 |
// go4.org/cloud/cloudlaunch and go4.org/wkfs/gcs. |
| 22 |
package main |
| 23 |
|
| 24 |
import ( |
| 25 |
"flag" |
| 26 |
"fmt" |
| 27 |
"log" |
| 28 |
"net/http" |
| 29 |
"os" |
| 30 |
"path" |
| 31 |
"regexp" |
| 32 |
"time" |
| 33 |
|
| 34 |
"go4.org/cloud/cloudlaunch" |
| 35 |
"go4.org/wkfs" |
| 36 |
_ "go4.org/wkfs/gcs" |
| 37 |
|
| 38 |
"cloud.google.com/go/compute/metadata" |
| 39 |
storageapi "google.golang.org/api/storage/v1" |
| 40 |
compute "google.golang.org/api/compute/v1" |
| 41 |
) |
| 42 |
|
| 43 |
var httpAddr = flag.String("http", ":80", "HTTP address") |
| 44 |
|
| 45 |
var gcsBucket string |
| 46 |
|
| 47 |
func serveHTTP(w http.ResponseWriter, r *http.Request) { |
| 48 |
rc, err := wkfs.Open(path.Join("/gcs", gcsBucket, r.URL.Path)) |
| 49 |
if err != nil { |
| 50 |
http.Error(w, fmt.Sprintf("could not open %v: %v", r.URL.Path, err), 500) |
| 51 |
return |
| 52 |
} |
| 53 |
defer rc.Close() |
| 54 |
http.ServeContent(w, r, r.URL.Path, time.Now(), rc) |
| 55 |
} |
| 56 |
|
| 57 |
func main() { |
| 58 |
if !metadata.OnGCE() { |
| 59 |
bucket := os.Getenv("GCSBUCKET") |
| 60 |
if bucket == "" { |
| 61 |
log.Fatal("You need to set the GCSBUCKET env var to specify the Google Cloud Storage bucket to serve from.") |
| 62 |
} |
| 63 |
projectID := os.Getenv("GCEPROJECTID") |
| 64 |
if projectID == "" { |
| 65 |
log.Fatal("You need to set the GCEPROJECTID env var to specify the Google Cloud project where the instance will run.") |
| 66 |
} |
| 67 |
(&cloudlaunch.Config{ |
| 68 |
Name: "serveoncloud", |
| 69 |
BinaryBucket: bucket, |
| 70 |
GCEProjectID: projectID, |
| 71 |
Scopes: []string{ |
| 72 |
storageapi.DevstorageFullControlScope, |
| 73 |
compute.ComputeScope, |
| 74 |
}, |
| 75 |
}).MaybeDeploy() |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
flag.Parse() |
| 80 |
|
| 81 |
storageURLRxp := regexp.MustCompile(`https://storage.googleapis.com/(.+?)/serveoncloud.*`) |
| 82 |
cloudConfig, err := metadata.InstanceAttributeValue("user-data") |
| 83 |
if err != nil || cloudConfig == "" { |
| 84 |
log.Fatalf("could not get cloud config from metadata: %v", err) |
| 85 |
} |
| 86 |
m := storageURLRxp.FindStringSubmatch(cloudConfig) |
| 87 |
if len(m) < 2 { |
| 88 |
log.Fatal("storage URL not found in cloud config") |
| 89 |
} |
| 90 |
gcsBucket = m[1] |
| 91 |
|
| 92 |
http.HandleFunc("/", serveHTTP) |
| 93 |
|
| 94 |
log.Fatal(http.ListenAndServe(*httpAddr, nil)) |
| 95 |
} |