@@ -17,29 +17,39 @@ limitations under the License.
1717package main
1818
1919import (
20- "flag"
2120 "fmt"
2221 "log"
2322 "net/http"
2423 "os"
25-
26- "knative.dev/pkg/network"
2724)
2825
29- func handler (w http.ResponseWriter , r * http.Request ) {
30- if r .ProtoMajor == 2 {
31- log .Print ("hellohttp2 received an http2 request." )
32- fmt .Fprintln (w , "Hello, New World! How about donuts and coffee?" )
33- } else {
34- log .Print ("hellohttp2 received an HTTP 1.1 request." )
35- w .WriteHeader (http .StatusUpgradeRequired )
36- }
37- }
38-
3926func main () {
40- flag .Parse ()
4127 log .Print ("hellohttp2 app started." )
4228
43- s := network .NewServer (":" + os .Getenv ("PORT" ), http .HandlerFunc (handler ))
29+ mux := http .NewServeMux ()
30+ mux .HandleFunc ("/" , handler )
31+ mux .HandleFunc ("/healthz" , healthzHandler )
32+
33+ protocols := & http.Protocols {}
34+ protocols .SetHTTP1 (true )
35+ protocols .SetUnencryptedHTTP2 (true )
36+
37+ s := & http.Server {
38+ Addr : ":" + os .Getenv ("PORT" ),
39+ Handler : mux ,
40+ Protocols : protocols ,
41+ }
4442 log .Fatal (s .ListenAndServe ())
4543}
44+
45+ func handler (w http.ResponseWriter , r * http.Request ) {
46+ log .Printf ("Request: proto=%s method=%s path=%s" , r .Proto , r .Method , r .URL .Path )
47+
48+ fmt .Fprintf (w , "proto=%s method=%s path=%s\n " , r .Proto , r .Method , r .URL .Path )
49+ }
50+
51+ func healthzHandler (w http.ResponseWriter , r * http.Request ) {
52+ log .Printf ("Health check: proto=%s method=%s path=%s" , r .Proto , r .Method , r .URL .Path )
53+ w .WriteHeader (http .StatusOK )
54+ fmt .Fprintln (w , "OK" )
55+ }
0 commit comments