summaryrefslogtreecommitdiffhomepage
path: root/src/go/unit/unit.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/go/unit/unit.go')
-rw-r--r--src/go/unit/unit.go134
1 files changed, 134 insertions, 0 deletions
diff --git a/src/go/unit/unit.go b/src/go/unit/unit.go
new file mode 100644
index 00000000..74e53ecf
--- /dev/null
+++ b/src/go/unit/unit.go
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) Max Romanov
+ * Copyright (C) NGINX, Inc.
+ */
+
+package unit
+
+/*
+#include "nxt_go_lib.h"
+*/
+import "C"
+
+import (
+ "fmt"
+ "net/http"
+ "os"
+ "strconv"
+ "strings"
+ "unsafe"
+)
+
+type cbuf struct {
+ b unsafe.Pointer
+ s C.size_t
+ f bool
+}
+
+func new_cbuf(buf []byte) *cbuf {
+ if len(buf) == 0 {
+ return nil
+ }
+
+ return &cbuf{
+ getCBytes(buf), C.size_t(len(buf)), true,
+ }
+}
+
+func (buf *cbuf) Close() {
+ if buf == nil {
+ return
+ }
+
+ if buf.f && buf.s > 0 {
+ C.free(buf.b)
+ buf.f = false
+ buf.b = nil
+ buf.s = 0
+ }
+}
+
+func (buf *cbuf) GoBytes() []byte {
+ if buf == nil {
+ var b [0]byte
+ return b[:0]
+ }
+
+ return C.GoBytes(buf.b, C.int(buf.s))
+}
+
+type cmsg struct {
+ buf cbuf
+ oob cbuf
+}
+
+func new_cmsg(buf []byte, oob []byte) *cmsg {
+ return &cmsg{
+ buf: cbuf{getCBytes(buf), C.size_t(len(buf)), true},
+ oob: cbuf{getCBytes(oob), C.size_t(len(oob)), true},
+ }
+}
+
+func (msg *cmsg) Close() {
+ msg.buf.Close()
+ msg.oob.Close()
+}
+
+var nxt_go_quit bool = false
+
+//export nxt_go_set_quit
+func nxt_go_set_quit() {
+ nxt_go_quit = true
+}
+
+func ListenAndServe(addr string, handler http.Handler) error {
+ var read_port *port
+
+ go_ports_env := os.Getenv("NXT_GO_PORTS")
+
+ ports := strings.Split(go_ports_env, ";")
+ pid := os.Getpid()
+
+ for _, port_str := range ports {
+ if len(port_str) <= 0 {
+ continue
+ }
+
+ attrs := strings.Split(port_str, ",")
+
+ var attrsN [5]int
+ var err error
+ for i, attr := range attrs {
+ attrsN[i], err = strconv.Atoi(attr)
+ if err != nil {
+ fmt.Printf("err %s\n", err)
+ break
+ }
+ }
+
+ if err != nil {
+ continue
+ }
+
+ p := new_port(attrsN[0], attrsN[1], attrsN[2], attrsN[3], attrsN[4])
+
+ if attrsN[0] == pid {
+ read_port = p
+ }
+ }
+
+ if read_port != nil {
+ C.nxt_go_ready()
+
+ for !nxt_go_quit {
+ err := read_port.read(handler)
+ if err != nil {
+ return err
+ }
+ }
+ } else {
+ return http.ListenAndServe(addr, handler)
+ }
+
+ return nil
+}