-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.go
94 lines (82 loc) · 2.42 KB
/
hello.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
)
type Data struct {
Service string
Revision string
Project string
Region string
}
func main() {
tmpl := template.Must(template.ParseFiles("index.html"))
// Get project ID from metadata server
project := ""
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/project/project-id", nil)
req.Header.Set("Metadata-Flavor", "Google")
res, err := client.Do(req)
if err == nil {
defer res.Body.Close()
if res.StatusCode == 200 {
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
project = string(responseBody)
}
}
// Get region from metadata server
region := ""
req, _ = http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/region", nil)
req.Header.Set("Metadata-Flavor", "Google")
res, err = client.Do(req)
if err == nil {
defer res.Body.Close()
if res.StatusCode == 200 {
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
region = regexp.MustCompile(`projects/[^/]*/regions/`).ReplaceAllString(string(responseBody), "")
}
}
service := os.Getenv("K_SERVICE")
revision := os.Getenv("K_REVISION")
data := Data{
Service: service,
Revision: revision,
Project: project,
Region: region,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, data)
})
fs := http.FileServer(http.Dir("./assets"))
http.Handle("/assets/", http.StripPrefix("/assets/", fs))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Print("Hello from Cloud Run! The container started successfully and is listening for HTTP requests on $PORT")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}