ListenAndServe changed to be compatible with http.ListenAndServe.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user