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.
20 MainPath = filepath.Dir(findMainPath())
21 installHealthChecker(http.DefaultServeMux)
24 if s := os.Getenv("PORT"); s != "" {
32 if err := http.ListenAndServe(host+":"+port, http.HandlerFunc(handleHTTP)); err != nil {
33 log.Fatalf("http.ListenAndServe: %v", err)
37 // Find the path to package main by looking at the root Caller.
38 func findMainPath() string {
39 pc := make([]uintptr, 100)
40 n := runtime.Callers(2, pc)
41 frames := runtime.CallersFrames(pc[:n])
43 frame, more := frames.Next()
44 // Tests won't have package main, instead they have testing.tRunner
45 if frame.Function == "main.main" || frame.Function == "testing.tRunner" {
55 func installHealthChecker(mux *http.ServeMux) {
56 // If no health check handler has been installed by this point, add a trivial one.
57 const healthPath = "/_ah/health"
58 hreq := &http.Request{
64 if _, pat := mux.Handler(hreq); pat != healthPath {
65 mux.HandleFunc(healthPath, func(w http.ResponseWriter, r *http.Request) {
66 io.WriteString(w, "ok")