Browse Source

feat(server): Implements RunFd method (#1609)

Yoshiki Nakagawa 7 years ago
parent
commit
66b47a8068
2 changed files with 54 additions and 0 deletions
  1. 18 0
      gin.go
  2. 36 0
      gin_integration_test.go

+ 18 - 0
gin.go

@@ -5,6 +5,7 @@
 package gin
 package gin
 
 
 import (
 import (
+	"fmt"
 	"html/template"
 	"html/template"
 	"net"
 	"net"
 	"net/http"
 	"net/http"
@@ -321,6 +322,23 @@ func (engine *Engine) RunUnix(file string) (err error) {
 	return
 	return
 }
 }
 
 
+// RunFd attaches the router to a http.Server and starts listening and serving HTTP requests
+// through the specified file descriptor.
+// Note: this method will block the calling goroutine indefinitely unless an error happens.
+func (engine *Engine) RunFd(fd int) (err error) {
+	debugPrint("Listening and serving HTTP on fd@%d", fd)
+	defer func() { debugPrintError(err) }()
+
+	f := os.NewFile(uintptr(fd), fmt.Sprintf("fd@%d", fd))
+	listener, err := net.FileListener(f)
+	if err != nil {
+		return
+	}
+	defer listener.Close()
+	err = http.Serve(listener, engine)
+	return
+}
+
 // ServeHTTP conforms to the http.Handler interface.
 // ServeHTTP conforms to the http.Handler interface.
 func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 	c := engine.pool.Get().(*Context)
 	c := engine.pool.Get().(*Context)

+ 36 - 0
gin_integration_test.go

@@ -134,6 +134,42 @@ func TestBadUnixSocket(t *testing.T) {
 	assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
 	assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
 }
 }
 
 
+func TestFileDescriptor(t *testing.T) {
+	router := New()
+
+	addr, err := net.ResolveTCPAddr("tcp", ":8000")
+	assert.NoError(t, err)
+	listener, err := net.ListenTCP("tcp", addr)
+	assert.NoError(t, err)
+	socketFile, err := listener.File()
+	assert.NoError(t, err)
+
+	go func() {
+		router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
+		assert.NoError(t, router.RunFd(int(socketFile.Fd())))
+	}()
+	// have to wait for the goroutine to start and run the server
+	// otherwise the main thread will complete
+	time.Sleep(5 * time.Millisecond)
+
+	c, err := net.Dial("tcp", "localhost:8000")
+	assert.NoError(t, err)
+
+	fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
+	scanner := bufio.NewScanner(c)
+	var response string
+	for scanner.Scan() {
+		response += scanner.Text()
+	}
+	assert.Contains(t, response, "HTTP/1.0 200", "should get a 200")
+	assert.Contains(t, response, "it worked", "resp body should match")
+}
+
+func TestBadFileDescriptor(t *testing.T) {
+	router := New()
+	assert.Error(t, router.RunFd(0))
+}
+
 func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
 func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
 	router := New()
 	router := New()
 	router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
 	router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })