@@ -17,29 +17,43 @@ 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
26+ func main () {
27+ log .Print ("hellohttp2 app started." )
28+
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+ }
42+ log .Fatal (s .ListenAndServe ())
43+ }
44+
2945func 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?" )
46+ log .Printf ("Request: proto=%s method=%s path=%s" , r .Proto , r .Method , r .URL .Path )
47+
48+ if res , ok := os .LookupEnv ("RESPONSE" ); ok {
49+ fmt .Fprint (w , res )
3350 } else {
34- log .Print ("hellohttp2 received an HTTP 1.1 request." )
35- w .WriteHeader (http .StatusUpgradeRequired )
51+ fmt .Fprintf (w , "proto=%s method=%s path=%s\n " , r .Proto , r .Method , r .URL .Path )
3652 }
3753}
3854
39- func main () {
40- flag .Parse ()
41- log .Print ("hellohttp2 app started." )
42-
43- s := network .NewServer (":" + os .Getenv ("PORT" ), http .HandlerFunc (handler ))
44- log .Fatal (s .ListenAndServe ())
55+ func healthzHandler (w http.ResponseWriter , r * http.Request ) {
56+ log .Printf ("Health check: proto=%s method=%s path=%s" , r .Proto , r .Method , r .URL .Path )
57+ w .WriteHeader (http .StatusOK )
58+ fmt .Fprintln (w , "OK" )
4559}
0 commit comments