Skip to content

Commit

Permalink
Add RunFd
Browse files Browse the repository at this point in the history
  • Loading branch information
yyoshiki41 committed May 4, 2016
1 parent 542be2f commit d277f9d
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gin

import (
"fmt"
"html/template"
"net"
"net/http"
Expand Down Expand Up @@ -255,6 +256,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
}

// Conforms to the http.Handler interface.
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c := engine.pool.Get().(*Context)
Expand Down

0 comments on commit d277f9d

Please sign in to comment.