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
5// +build !appengine
6
7package internal
8
9import (
10	"io"
11	"log"
12	"net/http"
13	"net/url"
14	"os"
15)
16
17func Main() {
18	installHealthChecker(http.DefaultServeMux)
19
20	port := "8080"
21	if s := os.Getenv("PORT"); s != "" {
22		port = s
23	}
24
25	host := ""
26	if IsDevAppServer() {
27		host = "127.0.0.1"
28	}
29	if err := http.ListenAndServe(host+":"+port, http.HandlerFunc(handleHTTP)); err != nil {
30		log.Fatalf("http.ListenAndServe: %v", err)
31	}
32}
33
34func installHealthChecker(mux *http.ServeMux) {
35	// If no health check handler has been installed by this point, add a trivial one.
36	const healthPath = "/_ah/health"
37	hreq := &http.Request{
38		Method: "GET",
39		URL: &url.URL{
40			Path: healthPath,
41		},
42	}
43	if _, pat := mux.Handler(hreq); pat != healthPath {
44		mux.HandleFunc(healthPath, func(w http.ResponseWriter, r *http.Request) {
45			io.WriteString(w, "ok")
46		})
47	}
48}
49