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

This commit is contained in:
Yoshiki Nakagawa
2018-11-06 11:28:51 +09:00
committed by thinkerou
parent 8cb390f8fe
commit 66b47a8068
2 changed files with 54 additions and 0 deletions

18
gin.go
View File

@ -5,6 +5,7 @@
package gin
import (
"fmt"
"html/template"
"net"
"net/http"
@ -321,6 +322,23 @@ func (engine *Engine) RunUnix(file string) (err error) {
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.
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c := engine.pool.Get().(*Context)