diff --git a/gin.go b/gin.go index 122e23ab..889ab5fb 100644 --- a/gin.go +++ b/gin.go @@ -17,7 +17,6 @@ import ( "github.com/gin-gonic/gin/internal/bytesconv" "github.com/gin-gonic/gin/render" - "github.com/quic-go/quic-go/http3" "golang.org/x/net/http2" "golang.org/x/net/http2/h2c" ) @@ -507,22 +506,6 @@ func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error) { return } -// RunQUIC attaches the router to a http.Server and starts listening and serving QUIC requests. -// It is a shortcut for http3.ListenAndServeQUIC(addr, certFile, keyFile, router) -// Note: this method will block the calling goroutine indefinitely unless an error happens. -func (engine *Engine) RunQUIC(addr, certFile, keyFile string) (err error) { - debugPrint("Listening and serving QUIC on %s\n", addr) - defer func() { debugPrintError(err) }() - - if engine.isUnsafeTrustedProxies() { - debugPrint("[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.\n" + - "Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.") - } - - err = http3.ListenAndServeQUIC(addr, certFile, keyFile, engine.Handler()) - return -} - // RunUnix attaches the router to a http.Server and starts listening and serving HTTP requests // through the specified unix socket (i.e. a file). // Note: this method will block the calling goroutine indefinitely unless an error happens. diff --git a/gin_1.19.go b/gin_1.19.go new file mode 100644 index 00000000..88428144 --- /dev/null +++ b/gin_1.19.go @@ -0,0 +1,27 @@ +// Copyright 2023 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +//go:build go1.19 + +package gin + +import ( + "github.com/quic-go/quic-go/http3" +) + +// RunQUIC attaches the router to a http.Server and starts listening and serving QUIC requests. +// It is a shortcut for http3.ListenAndServeQUIC(addr, certFile, keyFile, router) +// Note: this method will block the calling goroutine indefinitely unless an error happens. +func (engine *Engine) RunQUIC(addr, certFile, keyFile string) (err error) { + debugPrint("Listening and serving QUIC on %s\n", addr) + defer func() { debugPrintError(err) }() + + if engine.isUnsafeTrustedProxies() { + debugPrint("[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.\n" + + "Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.") + } + + err = http3.ListenAndServeQUIC(addr, certFile, keyFile, engine.Handler()) + return +} diff --git a/gin_integration_1.19_test.go b/gin_integration_1.19_test.go new file mode 100644 index 00000000..f0219844 --- /dev/null +++ b/gin_integration_1.19_test.go @@ -0,0 +1,31 @@ +// Copyright 2023 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +//go:build go1.19 + +package gin + +import ( + "net/http" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestRunQUIC(t *testing.T) { + router := New() + go func() { + router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) + + assert.NoError(t, router.RunQUIC(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) + }() + + // have to wait for the goroutine to start and run the server + // otherwise the main thread will complete + time.Sleep(5 * time.Millisecond) + + assert.Error(t, router.RunQUIC(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) + testRequest(t, "https://localhost:8443/example") +} diff --git a/gin_integration_test.go b/gin_integration_test.go index 11d0fb0c..02b96221 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -168,22 +168,6 @@ func TestRunTLS(t *testing.T) { testRequest(t, "https://localhost:8443/example") } -func TestRunQUIC(t *testing.T) { - router := New() - go func() { - router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) - - assert.NoError(t, router.RunQUIC(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) - }() - - // have to wait for the goroutine to start and run the server - // otherwise the main thread will complete - time.Sleep(5 * time.Millisecond) - - assert.Error(t, router.RunQUIC(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) - testRequest(t, "https://localhost:8443/example") -} - func TestPusher(t *testing.T) { var html = template.Must(template.New("https").Parse(`