From cd6e95f0e4e05e3266e3e90f56a2501e3500e7f6 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Tue, 19 May 2015 00:45:08 +0200 Subject: [PATCH] Unix sockets support --- gin.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/gin.go b/gin.go index f6ad4d42..67fe7468 100644 --- a/gin.go +++ b/gin.go @@ -6,7 +6,9 @@ package gin import ( "html/template" + "net" "net/http" + "os" "sync" "github.com/gin-gonic/gin/binding" @@ -159,7 +161,7 @@ func (engine *Engine) handle(method, path string, handlers HandlersChain) { func (engine *Engine) Run(addr string) (err error) { debugPrint("Listening and serving HTTP on %s\n", addr) - defer debugPrintError(err) + defer func() { debugPrintError(err) }() err = http.ListenAndServe(addr, engine) return @@ -167,12 +169,27 @@ func (engine *Engine) Run(addr string) (err error) { func (engine *Engine) RunTLS(addr string, cert string, key string) (err error) { debugPrint("Listening and serving HTTPS on %s\n", addr) - defer debugPrintError(err) + defer func() { debugPrintError(err) }() err = http.ListenAndServe(addr, engine) return } +func (engine *Engine) RunUnix(file string) (err error) { + debugPrint("Listening and serving HTTP on unix:/%s", file) + defer func() { debugPrintError(err) }() + + os.Remove(file) + listener, err2 := net.Listen("unix", file) + err = err2 + if err != nil { + return + } + defer listener.Close() + err = http.Serve(listener, engine) + return +} + // ServeHTTP makes the router implement the http.Handler interface. func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) { c := engine.getcontext(w, req)