main_vm.go 936 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // +build !appengine
  5. package internal
  6. import (
  7. "io"
  8. "log"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. )
  13. func Main() {
  14. installHealthChecker(http.DefaultServeMux)
  15. port := "8080"
  16. if s := os.Getenv("PORT"); s != "" {
  17. port = s
  18. }
  19. if err := http.ListenAndServe(":"+port, http.HandlerFunc(handleHTTP)); err != nil {
  20. log.Fatalf("http.ListenAndServe: %v", err)
  21. }
  22. }
  23. func installHealthChecker(mux *http.ServeMux) {
  24. // If no health check handler has been installed by this point, add a trivial one.
  25. const healthPath = "/_ah/health"
  26. hreq := &http.Request{
  27. Method: "GET",
  28. URL: &url.URL{
  29. Path: healthPath,
  30. },
  31. }
  32. if _, pat := mux.Handler(hreq); pat != healthPath {
  33. mux.HandleFunc(healthPath, func(w http.ResponseWriter, r *http.Request) {
  34. io.WriteString(w, "ok")
  35. })
  36. }
  37. }