From 8fd5324a5f291e9bfb8df52db457ddecde96e453 Mon Sep 17 00:00:00 2001 From: yangqiuhua <284751067@qq.com> Date: Thu, 16 May 2019 10:36:35 +0800 Subject: [PATCH 1/5] feat(gin,run): add expose out http.Server can use Server config --- gin.go | 17 +++++++++++++++++ gin_integration_test.go | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/gin.go b/gin.go index 4dbe9836..db8e1004 100644 --- a/gin.go +++ b/gin.go @@ -295,6 +295,23 @@ func (engine *Engine) Run(addr ...string) (err error) { return } +// Run attaches the router to a http.Server and starts listening and serving HTTP requests. +// You can use your http.server config like ReadTimeout ... +// It is a shortcut for http.ListenAndServe(addr, router) +// Note: this method will block the calling goroutine indefinitely unless an error happens. +func (engine *Engine) RunServer(server *http.Server, addr ...string) (err error) { + defer func() { debugPrintError(err) }() + + address := resolveAddress(addr) + debugPrint("Listening and serving HTTP on %s\n", address) + if len(addr) > 0{ + server.Addr = address + } + server.Handler = engine + err = server.ListenAndServe() + return +} + // RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests. // It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router) // Note: this method will block the calling goroutine indefinitely unless an error happens. diff --git a/gin_integration_test.go b/gin_integration_test.go index 9beec14d..a85d3540 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -54,6 +54,22 @@ func TestRunEmpty(t *testing.T) { testRequest(t, "http://localhost:8080/example") } +func TestRunServer(t *testing.T) { + os.Setenv("PORT", "") + router := New() + service := &http.Server{} + go func() { + router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) + assert.NoError(t, router.RunServer(service),":8080") + }() + // 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.RunServer(service),":8080") + testRequest(t, "http://localhost:8080/example") +} + func TestRunTLS(t *testing.T) { router := New() go func() { From 5f6cd32d8e9e455663c4ba43db7e65c590562f9c Mon Sep 17 00:00:00 2001 From: yangqiuhua <284751067@qq.com> Date: Thu, 16 May 2019 10:59:15 +0800 Subject: [PATCH 2/5] feat(gin,runserver): fix the runserver default port --- gin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gin.go b/gin.go index db8e1004..eeb28222 100644 --- a/gin.go +++ b/gin.go @@ -304,7 +304,7 @@ func (engine *Engine) RunServer(server *http.Server, addr ...string) (err error) address := resolveAddress(addr) debugPrint("Listening and serving HTTP on %s\n", address) - if len(addr) > 0{ + if len(addr) > 0 || server.Addr == ""{ server.Addr = address } server.Handler = engine From 72be513b9f16ee46ac05aab21a18c7de0b47bc14 Mon Sep 17 00:00:00 2001 From: yangqiuhua <284751067@qq.com> Date: Thu, 16 May 2019 11:40:25 +0800 Subject: [PATCH 3/5] feat(gin,runserver): fix RunServer test --- gin_integration_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gin_integration_test.go b/gin_integration_test.go index a85d3540..40b6a5dd 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -60,14 +60,14 @@ func TestRunServer(t *testing.T) { service := &http.Server{} go func() { router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) - assert.NoError(t, router.RunServer(service),":8080") + assert.NoError(t, router.RunServer(service,":8090")) }() // 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.RunServer(service),":8080") - testRequest(t, "http://localhost:8080/example") + assert.Error(t, router.RunServer(service,":8090")) + testRequest(t, "http://localhost:8090/example") } func TestRunTLS(t *testing.T) { From a1f4f8406c7d5010abd1395f8cc0aa06bd9cb37c Mon Sep 17 00:00:00 2001 From: yangqiuhua <284751067@qq.com> Date: Thu, 16 May 2019 14:38:05 +0800 Subject: [PATCH 4/5] feat(gin,runserver): go fmt file --- gin.go | 22 +++++++++++----------- gin_integration_test.go | 6 +++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/gin.go b/gin.go index eeb28222..ad2bb02d 100644 --- a/gin.go +++ b/gin.go @@ -297,19 +297,19 @@ func (engine *Engine) Run(addr ...string) (err error) { // Run attaches the router to a http.Server and starts listening and serving HTTP requests. // You can use your http.server config like ReadTimeout ... -// It is a shortcut for http.ListenAndServe(addr, router) +// It is a shortcut for server.ListenAndServe() // Note: this method will block the calling goroutine indefinitely unless an error happens. func (engine *Engine) RunServer(server *http.Server, addr ...string) (err error) { - defer func() { debugPrintError(err) }() - - address := resolveAddress(addr) - debugPrint("Listening and serving HTTP on %s\n", address) - if len(addr) > 0 || server.Addr == ""{ - server.Addr = address - } - server.Handler = engine - err = server.ListenAndServe() - return + defer func() { debugPrintError(err) }() + + address := resolveAddress(addr) + debugPrint("Listening and serving HTTP on %s\n", address) + if len(addr) > 0 || server.Addr == "" { + server.Addr = address + } + server.Handler = engine + err = server.ListenAndServe() + return } // RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests. diff --git a/gin_integration_test.go b/gin_integration_test.go index 40b6a5dd..d535b2c7 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -60,13 +60,13 @@ func TestRunServer(t *testing.T) { service := &http.Server{} go func() { router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) - assert.NoError(t, router.RunServer(service,":8090")) + assert.NoError(t, router.RunServer(service, ":8090")) }() // 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.RunServer(service,":8090")) + + assert.Error(t, router.RunServer(service, ":8090")) testRequest(t, "http://localhost:8090/example") } From 1062d79a374e8f84c3ea2445cb5de46b372a6c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=B7=E8=8C=AB=E5=B0=91=E5=B9=B4?= <284751067@qq.com> Date: Tue, 17 Aug 2021 14:42:20 +0800 Subject: [PATCH 5/5] Update gin_integration_test.go test --- gin_integration_test.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/gin_integration_test.go b/gin_integration_test.go index d535b2c7..57704272 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -70,21 +70,6 @@ func TestRunServer(t *testing.T) { testRequest(t, "http://localhost:8090/example") } -func TestRunTLS(t *testing.T) { - router := New() - go func() { - router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") }) - - assert.NoError(t, router.RunTLS(":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.RunTLS(":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(`