mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
Merge 841578e64965059d18374960f619805283928c4d into 1a2bc0e7cb1a69b7750bd652d92c4d2b41504f04
This commit is contained in:
commit
7122766550
17
gin.go
17
gin.go
@ -353,6 +353,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 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
|
||||
}
|
||||
func (engine *Engine) prepareTrustedCIDRs() ([]*net.IPNet, error) {
|
||||
if engine.TrustedProxies == nil {
|
||||
return nil, nil
|
||||
|
@ -76,6 +76,7 @@ func TestRunEmpty(t *testing.T) {
|
||||
testRequest(t, "http://localhost:8080/example")
|
||||
}
|
||||
|
||||
|
||||
func TestBadTrustedCIDRsForRun(t *testing.T) {
|
||||
os.Setenv("PORT", "")
|
||||
router := New()
|
||||
@ -146,20 +147,20 @@ func TestBadTrustedCIDRsForRunTLS(t *testing.T) {
|
||||
|
||||
func TestRunTLS(t *testing.T) {
|
||||
router := New()
|
||||
service := &http.Server{}
|
||||
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"))
|
||||
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.RunTLS(":8443", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))
|
||||
testRequest(t, "https://localhost:8443/example")
|
||||
assert.Error(t, router.RunServer(service, ":8090"))
|
||||
testRequest(t, "http://localhost:8090/example")
|
||||
}
|
||||
|
||||
|
||||
func TestPusher(t *testing.T) {
|
||||
var html = template.Must(template.New("https").Parse(`
|
||||
<html>
|
||||
|
Loading…
x
Reference in New Issue
Block a user