vendor/google.golang.org/appengine/internal/main_vm.go
changeset 251 1c52a0eeb952
parent 242 2a9ec03fe5a1
equal deleted inserted replaced
250:c040f992052f 251:1c52a0eeb952
    10 	"io"
    10 	"io"
    11 	"log"
    11 	"log"
    12 	"net/http"
    12 	"net/http"
    13 	"net/url"
    13 	"net/url"
    14 	"os"
    14 	"os"
       
    15 	"path/filepath"
       
    16 	"runtime"
    15 )
    17 )
    16 
    18 
    17 func Main() {
    19 func Main() {
       
    20 	MainPath = filepath.Dir(findMainPath())
    18 	installHealthChecker(http.DefaultServeMux)
    21 	installHealthChecker(http.DefaultServeMux)
    19 
    22 
    20 	port := "8080"
    23 	port := "8080"
    21 	if s := os.Getenv("PORT"); s != "" {
    24 	if s := os.Getenv("PORT"); s != "" {
    22 		port = s
    25 		port = s
    27 		host = "127.0.0.1"
    30 		host = "127.0.0.1"
    28 	}
    31 	}
    29 	if err := http.ListenAndServe(host+":"+port, http.HandlerFunc(handleHTTP)); err != nil {
    32 	if err := http.ListenAndServe(host+":"+port, http.HandlerFunc(handleHTTP)); err != nil {
    30 		log.Fatalf("http.ListenAndServe: %v", err)
    33 		log.Fatalf("http.ListenAndServe: %v", err)
    31 	}
    34 	}
       
    35 }
       
    36 
       
    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])
       
    42 	for {
       
    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" {
       
    46 			return frame.File
       
    47 		}
       
    48 		if !more {
       
    49 			break
       
    50 		}
       
    51 	}
       
    52 	return ""
    32 }
    53 }
    33 
    54 
    34 func installHealthChecker(mux *http.ServeMux) {
    55 func installHealthChecker(mux *http.ServeMux) {
    35 	// If no health check handler has been installed by this point, add a trivial one.
    56 	// If no health check handler has been installed by this point, add a trivial one.
    36 	const healthPath = "/_ah/health"
    57 	const healthPath = "/_ah/health"