-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (57 loc) · 1.47 KB
/
main.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
package main
import (
"html/template"
"log"
"net/http"
"os"
)
// templateData provides template parameters.
type templateData struct {
Service string
Revision string
}
// Variables used to generate the HTML page.
var (
data templateData
tmpl *template.Template
)
func main() {
// Initialize template parameters.
service := os.Getenv("K_SERVICE")
if service == "" {
service = "???"
}
revision := os.Getenv("K_REVISION")
if revision == "" {
revision = "???"
}
// Prepare template for execution.
tmpl = template.Must(template.ParseFiles("index.html"))
data = templateData{
Service: service,
Revision: revision,
}
// Define HTTP server.
http.HandleFunc("/", helloRunHandler)
fs := http.FileServer(http.Dir("./assets"))
http.Handle("/assets/", http.StripPrefix("/assets/", fs))
// PORT environment variable is provided by Cloud Run.
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.Printf("Listening on port %s", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal(err)
}
}
// helloRunHandler responds to requests by rendering an HTML page.
func helloRunHandler(w http.ResponseWriter, r *http.Request) {
if err := tmpl.Execute(w, data); err != nil {
msg := http.StatusText(http.StatusInternalServerError)
log.Printf("template.Execute: %v", err)
http.Error(w, msg, http.StatusInternalServerError)
}
}