Browse Source

support running with socket

zebozhuang 10 years ago
parent
commit
832d3b9ecb
1 changed files with 24 additions and 0 deletions
  1. 24 0
      gin.go

+ 24 - 0
gin.go

@@ -9,7 +9,9 @@ import (
 	"github.com/julienschmidt/httprouter"
 	"github.com/julienschmidt/httprouter"
 	"html/template"
 	"html/template"
 	"math"
 	"math"
+    "net"
 	"net/http"
 	"net/http"
+    "os"
 	"sync"
 	"sync"
 )
 )
 
 
@@ -21,6 +23,9 @@ const (
 	MIMEXML2     = "text/xml"
 	MIMEXML2     = "text/xml"
 	MIMEPlain    = "text/plain"
 	MIMEPlain    = "text/plain"
 	MIMEPOSTForm = "application/x-www-form-urlencoded"
 	MIMEPOSTForm = "application/x-www-form-urlencoded"
+
+    UNIX = "unix"
+    TCP  = "tcp"
 )
 )
 
 
 type (
 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) {
 func (engine *Engine) RunTLS(addr string, cert string, key string) {
 	debugPrint("Listening and serving HTTPS on %s", addr)
 	debugPrint("Listening and serving HTTPS on %s", addr)
 	if err := http.ListenAndServeTLS(addr, cert, key, engine); err != nil {
 	if err := http.ListenAndServeTLS(addr, cert, key, engine); err != nil {