Installation for Cygwin

Installation for Cygwin - 图1

Installation

First, you need to install the Go language. Please install the latest version, not the one that is listed here.

  1. wget -N https://storage.googleapis.com/golang/go1.8.1.windows-amd64.msi
  2. msiexec /i go1.8.1.windows-amd64.msi /passive /promptrestart

Then you need to install ProtocolBuffers 3.0.0-beta-3 or later. Use the Windows release as no native Cygwin protoc with version 3 is available yet.

  1. wget -N https://github.com/google/protobuf/releases/download/v3.2.0/protoc-3.2.0-win32.zip`
  2. 7z x protoc-3.2.0-win32.zip -o/usr/local/

Then you need to set up your Go workspace. Create the workspace dir.

  1. mkdir /home/user/go
  2. mkdir /home/user/go/bin
  3. mkdir /home/user/go/pkg
  4. mkdir /home/user/go/src

From an elevated cmd.exe prompt set the GOPATH variable in Windows and add the $GOPATH/bin directory to your path using reg add instead of setx because setx can truncate your PATH variable to 1024 characters.

  1. setx GOPATH c:\path\to\your\cygwin\home\user\go /M
  2. set pathkey="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment"
  3. for /F "usebackq skip=2 tokens=2*" %A IN (`reg query %pathkey% /v Path`) do (reg add %pathkey% /f /v Path /t REG_SZ /d "%B;c:\path\to\your\cygwin\home\user\go\bin")

Then go get -u -v the following packages:

  1. go get -u -v github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
  2. go get -u -v github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
  3. go get -u -v google.golang.org/protobuf/cmd/protoc-gen-go
  4. go get -u -v google.golang.org/grpc/cmd/protoc-gen-go-grpc

This will probably fail with a similar output to this:

  1. github.com/grpc-ecosystem/grpc-gateway (download)
  2. # cd .; git clone https://github.com/grpc-ecosystem/grpc-gateway C:\path\to\your\cygwin\home\user\go\src\github.com\grpc-ecosystem\grpc-gateway
  3. Cloning into 'C:\path\to\your\cygwin\home\user\go\src\github.com\grpc-ecosystem\grpc-gateway'...
  4. fatal: Invalid path '/home/user/go/C:\path\to\your\cygwin\home\user\go\src\github.com\grpc-ecosystem\grpc-gateway': No such file or directory
  5. package github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway: exit status 128

To fix this you need to run the go get -u -v commands and look for all lines starting with # cd .;. Copy and paste these lines into your shell and change the clone destination directories.

  1. git clone https://github.com/grpc-ecosystem/grpc-gateway $(cygpath -u $GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway
  2. git clone https://github.com/golang/glog $(cygpath -u $GOPATH)/src/github.com/golang/glog
  3. git clone https://github.com/golang/protobuf $(cygpath -u $GOPATH)/src/github.com/golang/protobuf
  4. git clone https://github.com/google/go-genproto $(cygpath -u $GOPATH)/src/google.golang.org/genproto

Once the clone operations are finished the go get -u -v commands shouldn’t give you an error anymore.

Usage

Follow the instructions in the README.

Adjust steps 3, 5 and 7 like this. protoc expects native Windows paths.

  1. protoc -I. -I$(cygpath -w /usr/local/include) -I${GOPATH}/src --go_out=. --go-grpc_out=. ./path/to/your_service.proto
  2. protoc -I. -I$(cygpath -w /usr/local/include) -I${GOPATH}/src --grpc-gateway_out=logtostderr=true:. ./path/to/your_service.proto
  3. protoc -I. -I$(cygpath -w /usr/local/include) -I${GOPATH}/src --openapiv2_out=logtostderr=true:. ./path/to/your_service.proto

Then cd into the directory where your entry-point main.go file is located and run:

  1. go get -v

This will fail in this same way as it did during the installation. Look for all lines starting with # cd .;. Copy and paste these lines into your shell and change the clone destination directories.

  1. git clone https://go.googlesource.com/net $(cygpath -u $GOPATH)/src/golang.org/x/net
  2. git clone https://go.googlesource.com/text $(cygpath -u $GOPATH)/src/golang.org/x/text
  3. git clone https://github.com/grpc/grpc-go $(cygpath -u $GOPATH)/src/google.golang.org/grpc

Once the clone operations are finished the go get -v commands shouldn’t give you an error anymore.

Then run:

  1. go install

This will compile and install your gRPC-Gateway service into $GOPATH/bin.