From 9c1ab9ba59b363a11544029043d8a52793e0cb34 Mon Sep 17 00:00:00 2001 From: Shawn Lee Date: Mon, 31 Jul 2023 14:25:11 +0900 Subject: [PATCH] support h2c server (#228) --- httplib/server.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/httplib/server.go b/httplib/server.go index 24640b7..fc4d1cc 100644 --- a/httplib/server.go +++ b/httplib/server.go @@ -6,6 +6,9 @@ import ( "net/http" "sync" + "golang.org/x/net/http2" + "golang.org/x/net/http2/h2c" + log "github.com/sirupsen/logrus" ) @@ -16,12 +19,22 @@ type Server interface { type api struct { router http.Handler port string + h2c bool } func NewHTTPServer(router http.Handler, port string) Server { return &api{ router: router, port: port, + h2c: false, + } +} + +func NewH2CServer(router http.Handler, port string) Server { + return &api{ + router: router, + port: port, + h2c: true, } } @@ -32,9 +45,15 @@ func (a *api) Run(ctx context.Context, wg *sync.WaitGroup) { func (a *api) serve(ctx context.Context, wg *sync.WaitGroup) { wg.Add(1) + h2s := &http2.Server{} + h1d, h2d := a.router, h2c.NewHandler(a.router, h2s) + server := &http.Server{ Addr: ":" + a.port, - Handler: a.router, + Handler: h1d, + } + if a.h2c { + server.Handler = h2d } serverStopped := make(chan struct{})