summaryrefslogtreecommitdiffhomepage
path: root/src/nginext
diff options
context:
space:
mode:
Diffstat (limited to 'src/nginext')
-rw-r--r--src/nginext/nginext.go11
-rw-r--r--src/nginext/nxt_go_port.c2
-rw-r--r--src/nginext/port.go20
3 files changed, 27 insertions, 6 deletions
diff --git a/src/nginext/nginext.go b/src/nginext/nginext.go
index de0218e8..895d6b28 100644
--- a/src/nginext/nginext.go
+++ b/src/nginext/nginext.go
@@ -12,6 +12,7 @@ import "C"
import (
"fmt"
+ "net/http"
"os"
"strconv"
"strings"
@@ -80,7 +81,7 @@ func nxt_go_set_quit() {
nxt_go_quit = true
}
-func ListenAndServe() {
+func ListenAndServe(addr string, handler http.Handler) error {
var read_port *port
go_ports_env := os.Getenv("NXT_GO_PORTS")
@@ -120,8 +121,14 @@ func ListenAndServe() {
C.nxt_go_ready()
for !nxt_go_quit {
- read_port.read()
+ err := read_port.read(handler)
+ if err != nil {
+ return err
+ }
}
+ } else {
+ return http.ListenAndServe(addr, handler)
}
+ return nil
}
diff --git a/src/nginext/nxt_go_port.c b/src/nginext/nxt_go_port.c
index 033dd5be..a46a33d1 100644
--- a/src/nginext/nxt_go_port.c
+++ b/src/nginext/nxt_go_port.c
@@ -108,8 +108,6 @@ nxt_go_data_handler(nxt_port_msg_t *port_msg, size_t size)
nxt_go_request_create_channel(r);
}
- nxt_go_request_serve(r);
-
return r;
}
diff --git a/src/nginext/port.go b/src/nginext/port.go
index ee858612..768fbf84 100644
--- a/src/nginext/port.go
+++ b/src/nginext/port.go
@@ -14,6 +14,7 @@ import "C"
import (
"fmt"
"net"
+ "net/http"
"os"
"sync"
"unsafe"
@@ -177,11 +178,15 @@ func new_port(pid int, id int, t int, rcv int, snd int) *port {
return p
}
-func (p *port) read() {
+func (p *port) read(handler http.Handler) error {
var buf [16384]byte
var oob [1024]byte
- n, oobn, _, _, _ := p.rcv.ReadMsgUnix(buf[:], oob[:])
+ n, oobn, _, _, err := p.rcv.ReadMsgUnix(buf[:], oob[:])
+
+ if err != nil {
+ return err
+ }
m := new_cmsg(buf[:n], oob[:oobn])
@@ -191,6 +196,16 @@ func (p *port) read() {
m.Close()
} else {
r := find_request(c_req)
+
+ go func(r *request) {
+ if handler == nil {
+ handler = http.DefaultServeMux
+ }
+
+ handler.ServeHTTP(r.response(), &r.req)
+ r.done()
+ }(r)
+
if len(r.msgs) == 0 {
r.push(m)
} else if r.ch != nil {
@@ -200,4 +215,5 @@ func (p *port) read() {
}
}
+ return err
}