Skip to content

Commit

Permalink
chore: support h2c
Browse files Browse the repository at this point in the history
gin-gonic/gin#1398

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jul 31, 2022
1 parent 9c9307c commit 10ad520
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
19 changes: 16 additions & 3 deletions cmd/client/main.go
Expand Up @@ -2,27 +2,40 @@ package main

import (
"context"
"crypto/tls"
"log"
"net"
"net/http"

v1 "github.com/go-training/gin-connect-demo/gen/proto/v1" // generated by protoc-gen-go
"github.com/go-training/gin-connect-demo/gen/proto/v1/v1connect" // generated by protoc-gen-connect-go

"github.com/bufbuild/connect-go"
"golang.org/x/net/http2"
)

func main() {
c := &http.Client{
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(netw, addr)
},
},
}

client := v1connect.NewGiteaServiceClient(
http.DefaultClient,
c,
"http://localhost:8080/",
)
req := connect.NewRequest(&v1.GiteaRequest{
Name: "foobar",
})
req.Header().Set("Some-Header", "hello from connect")
req.Header().Set("Gitea-Header", "hello from connect")
res, err := client.Gitea(context.Background(), req)
if err != nil {
log.Fatalln(err)
}
log.Println(res.Msg)
log.Println("Message:", res.Msg.Giteaing)
log.Println("Gitea-Version:", res.Header().Get("Gitea-Version"))
}
8 changes: 5 additions & 3 deletions cmd/server/main.go
Expand Up @@ -29,6 +29,7 @@ func (s *GiteaServer) Gitea(

func giteaHandler(h http.Handler) gin.HandlerFunc {
return func(c *gin.Context) {
log.Println("Got connection:", c.Request.Proto)
h.ServeHTTP(c.Writer, c.Request)
}
}
Expand All @@ -37,8 +38,9 @@ func main() {
greeter := &GiteaServer{}
path, handler := v1connect.NewGiteaServiceHandler(greeter)

router := gin.Default()
router.POST(path+":name", giteaHandler(handler))
r := gin.Default()
r.UseH2C = true
r.POST(path+":name", giteaHandler(handler))

router.Run(":8080")
r.Run(":8080")
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/bufbuild/connect-go v0.2.0
github.com/gin-gonic/gin v1.8.1
golang.org/x/net v0.0.0-20220728211354-c7608f3a8462
google.golang.org/protobuf v1.28.1
)

Expand All @@ -22,7 +23,6 @@ require (
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/net v0.0.0-20220728211354-c7608f3a8462 // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down

0 comments on commit 10ad520

Please sign in to comment.