Unit: Golang Hello world example can not work

Created on 28 May 2018  ยท  7Comments  ยท  Source: nginx/unit

When I use the nginx/unit project as my LoadBalancer component, I want use golang programming language to make a plugin tool. According to the official document, I config/make unit source code and go build my program, then use http put command to update the config:

{
    "listeners": {
        "*:8500": {
            "application": "go_chat_app"
        }
    },

    "applications": {
        "go_chat_app": {
            "type": "go",
            "working_directory": "/tmp",
            "executable": "/tmp/unit-go"
        }
    }
}

```bash

curl -X PUT -d @go.json --unix-socket /var/run/control.unit.sock http://localhost/

After start unit service, I can't see the unit-go child process in `ps` commond, but I see the 8500 port is listing by unit router process:

```bash
#netstat -anpt | grep 8500
tcp        0      0 0.0.0.0:8500            0.0.0.0:*               LISTEN      12798/unit: router

```bash

ps auxf

root 12795 0.0 0.0 16224 952 ? Ss 5ๆœˆ25 0:00 unit: main [/usr/sbin/unitd --log /var/log/unitd.log --pid /run/unitd.pid]
nobody 12797 0.0 0.0 26468 976 ? S 5ๆœˆ25 0:00 _ unit: controller
nobody 12798 0.0 0.0 174076 932 ? Sl 5ๆœˆ25 0:00 _ unit: router

and my golang code:
```golang
package main

import (
    "fmt"
    "net/http"
    "nginx/unit"
)

func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Content-Type", "text/plain");

    fmt.Fprintf(w, "Method : %s\n", r.Method)
    fmt.Fprintf(w, "URL    : %s\n", r.URL.Path)
    fmt.Fprintf(w, "Host   : %s\n", r.Host)
}

func main() {
    http.HandleFunc("/", handler)
    unit.ListenAndServe("8000", nil)
}

go.json:

{
    "listeners": {
        "*:8500": {
            "application": "go_chat_app"
        }
    },

    "applications": {
        "go_chat_app": {
            "type": "go",
            "working_directory": "/tmp",
            "executable": "/tmp/unit-go"
        }
    }
}

Anybody has same problem?

Most helpful comment

I've tried to reproduce your issue and failed with it. Sorry.
Let's ensure we using same version, same commands etc. This is my sequence of commands:

$ cd $HOME
$ script $HOME/unit-i126.log
$ rm -rf $HOME/unit-i126 && mkdir -p $HOME/unit-i126/src
$ cd $HOME/unit-i126/src
$ git clone https://github.com/nginx/unit.git
$ cp unit/pkg/rpm/rpmbuild/SOURCES/unit.example-go-app ../go-app.go
$ cp unit/pkg/rpm/rpmbuild/SOURCES/unit.example-go-config ../go-app.json
$ cd unit
$ export GOPATH=$HOME/unit-i126
$ ./configure
$ ./configure go
$ make -s all
$ make go-install
$ ./build/unitd
$ cd $HOME/unit-i126
$ go build go-app.go
$ cp go-app /tmp/
$ curl -X PUT -d @go-app.json --unix-socket src/unit/control.unit.sock http://localhost/
$ curl http://localhost:8500/
$ kill `cat src/unit/unit.pid`
$ exit

Please stop any running version of unit and carefully execute this commands line by line. In case of any issues, let me know and attach $HOME/unit-i126.log and $HOME/unit-i126/src/unit/unit.log files.

All 7 comments

When I curl localhost:8500, it returns "Read header timeout" error.

Please provide more information about your environment: OS version, golang version, configure command line options to reproduce the issue.

Thank you very much~
Here is my environment Info:
OS version: CentOS 7
Kernel version: 3.10.0-693.2.2.el7.x86_64
Golang version: go1.9.5 linux/amd64

Configure commands:
Firstly: ./config
Then: ./config go
Then: make all
Finally: make go-install

My directory:
~/unit
โ”œโ”€โ”€ full.json
โ”œโ”€โ”€ get.sh
โ”œโ”€โ”€ go.json
โ”œโ”€โ”€ put.sh
โ”œโ”€โ”€ src
โ”‚ย ย  โ”œโ”€โ”€ nginx
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ unit
โ”‚ย ย  โ””โ”€โ”€ unit
โ”‚ย ย  โ”œโ”€โ”€ auto
โ”‚ย ย  โ”œโ”€โ”€ build
โ”‚ย ย  โ”œโ”€โ”€ CHANGES
โ”‚ย ย  โ”œโ”€โ”€ configure
โ”‚ย ย  โ”œโ”€โ”€ docs
โ”‚ย ย  โ”œโ”€โ”€ LICENSE
โ”‚ย ย  โ”œโ”€โ”€ Makefile
โ”‚ย ย  โ”œโ”€โ”€ NOTICE
โ”‚ย ย  โ”œโ”€โ”€ pkg
โ”‚ย ย  โ”œโ”€โ”€ README
โ”‚ย ย  โ”œโ”€โ”€ sbin
โ”‚ย ย  โ”œโ”€โ”€ src
โ”‚ย ย  โ””โ”€โ”€ test
โ”œโ”€โ”€ unit
โ””โ”€โ”€ unit.go

GOPATH: ~/unit

This problem is also posted on stackoverflow-Can not find unit-go child process in nginx unit, curl request timeout, welcoming everyone to join the discussion~

I've tried to reproduce your issue and failed with it. Sorry.
Let's ensure we using same version, same commands etc. This is my sequence of commands:

$ cd $HOME
$ script $HOME/unit-i126.log
$ rm -rf $HOME/unit-i126 && mkdir -p $HOME/unit-i126/src
$ cd $HOME/unit-i126/src
$ git clone https://github.com/nginx/unit.git
$ cp unit/pkg/rpm/rpmbuild/SOURCES/unit.example-go-app ../go-app.go
$ cp unit/pkg/rpm/rpmbuild/SOURCES/unit.example-go-config ../go-app.json
$ cd unit
$ export GOPATH=$HOME/unit-i126
$ ./configure
$ ./configure go
$ make -s all
$ make go-install
$ ./build/unitd
$ cd $HOME/unit-i126
$ go build go-app.go
$ cp go-app /tmp/
$ curl -X PUT -d @go-app.json --unix-socket src/unit/control.unit.sock http://localhost/
$ curl http://localhost:8500/
$ kill `cat src/unit/unit.pid`
$ exit

Please stop any running version of unit and carefully execute this commands line by line. In case of any issues, let me know and attach $HOME/unit-i126.log and $HOME/unit-i126/src/unit/unit.log files.

@lth2015 What Unit version are you trying to run? Please, note that currently supported version is 1.1 and all the instructions are valid for it.

@mar0x thank you very much! It's works! may be I use a different version of unit by yum install unit and service unitd start.
@VBart thank you~

I will write a document to show how to can run unit-go step-by-step, thank you all guys~

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elijahsgh picture elijahsgh  ยท  4Comments

Madhu1512 picture Madhu1512  ยท  4Comments

pchaganti picture pchaganti  ยท  3Comments

hongzhidao picture hongzhidao  ยท  3Comments

AkulovNV picture AkulovNV  ยท  4Comments