From 832d3b9ecba2f373f42be211fecb01f2051c4a8e Mon Sep 17 00:00:00 2001 From: zebozhuang Date: Mon, 13 Apr 2015 00:32:31 +0800 Subject: [PATCH] support running with socket --- gin.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/gin.go b/gin.go index ea9345a..ab872b0 100644 --- a/gin.go +++ b/gin.go @@ -9,7 +9,9 @@ import ( "github.com/julienschmidt/httprouter" "html/template" "math" + "net" "net/http" + "os" "sync" ) @@ -21,6 +23,9 @@ const ( MIMEXML2 = "text/xml" MIMEPlain = "text/plain" MIMEPOSTForm = "application/x-www-form-urlencoded" + + UNIX = "unix" + TCP = "tcp" ) type ( @@ -133,6 +138,25 @@ func (engine *Engine) Run(addr string) { } } +func (engine *Engine) RunSocket(addr string) { + debugPrint("Listening and serving HTTP on %s", addr) + os.Remove(addr) + + listener, err := net.Listen(UNIX, addr) + if err != nil { + panic(err) + } + os.Chmod(0666) + + server := http.Server{Handler: engine} + err = server.Serve(listener) + if err != nil { + listener.Close() + panic(err) + } + listener.Close() +} + func (engine *Engine) RunTLS(addr string, cert string, key string) { debugPrint("Listening and serving HTTPS on %s", addr) if err := http.ListenAndServeTLS(addr, cert, key, engine); err != nil {