golang/go-cloud-run-hello-world/main.go (50 lines of code) (raw):

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) } }