Skip to content

Commit 386f8c6

Browse files
committed
Rewrite hellohttp2 test service to use stdlib
1 parent de9cb21 commit 386f8c6

2 files changed

Lines changed: 37 additions & 19 deletions

File tree

test/test_images/hellohttp2/hellohttp2.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,39 @@ limitations under the License.
1717
package main
1818

1919
import (
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-
3926
func 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+
}

test/test_images/hellohttp2/service.yaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ metadata:
55
namespace: default
66
spec:
77
template:
8+
metadata:
9+
annotations:
10+
# Example: Override the queue sidecar image with a custom image
11+
# queue.sidecar.serving.knative.dev/image: ko://knative.dev/serving/cmd/queue
812
spec:
913
containers:
10-
- image: ko://knative.dev/serving/test/test_images/hellohttp2
11-
ports:
12-
- name: h2c
13-
containerPort: 8080
14+
- image: ko://knative.dev/serving/test/test_images/hellohttp2
15+
ports:
16+
- name: h2c
17+
containerPort: 8080
18+
readinessProbe:
19+
httpGet:
20+
path: /healthz
21+
port: 8080

0 commit comments

Comments
 (0)