main_vm.go 1000 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. host := ""
  20. if IsDevAppServer() {
  21. host = "127.0.0.1"
  22. }
  23. if err := http.ListenAndServe(host+":"+port, http.HandlerFunc(handleHTTP)); err != nil {
  24. log.Fatalf("http.ListenAndServe: %v", err)
  25. }
  26. }
  27. func installHealthChecker(mux *http.ServeMux) {
  28. // If no health check handler has been installed by this point, add a trivial one.
  29. const healthPath = "/_ah/health"
  30. hreq := &http.Request{
  31. Method: "GET",
  32. URL: &url.URL{
  33. Path: healthPath,
  34. },
  35. }
  36. if _, pat := mux.Handler(hreq); pat != healthPath {
  37. mux.HandleFunc(healthPath, func(w http.ResponseWriter, r *http.Request) {
  38. io.WriteString(w, "ok")
  39. })
  40. }
  41. }