Skip to content

Commit

Permalink
neat golang
Browse files Browse the repository at this point in the history
  • Loading branch information
feuyeux committed Sep 29, 2024
1 parent 2944ab4 commit af5b662
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 127 deletions.
File renamed without changes.
File renamed without changes.
21 changes: 16 additions & 5 deletions doc/protoc.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
Protobuf language bindings have two components.
<!-- markdownlint-disable MD033 MD041 -->
# protoc

The generated code (typically produced from `protoc`) and
## install

the runtime libraries that must be included when using the generated code.
# <https://github.com/protocolbuffers/protobuf/releases>

```sh
unzip -j /d/zoo/protoc-28.2-win64.zip bin/protoc.exe -d /d/zoo/bin/
```

## Usage

Protobuf language bindings have two components.

The generated code (typically produced from `protoc`) and

the runtime libraries that must be included when using the generated code.

```sh
PS C:\Users\han\coding> protoc -h
$protoc -h

Usage: C:\Program Files\protoc-3.19.1-win64\bin\protoc.exe [OPTION] PROTO_FILES

Parse PROTO_FILES and generate output based on the options given:
Expand All @@ -29,4 +41,3 @@ Parse PROTO_FILES and generate output based on the options given:
--python_out=OUT_DIR Generate Python source file.
--ruby_out=OUT_DIR Generate Ruby source file.
```

56 changes: 28 additions & 28 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
# build docker images

## build all images
# Docker Images

https://hub.docker.com/repositories/feuyeux

| No. | Lang | Base Image | Server | Client |
|:----|:-----------------------------|:---------------------------------------------------------------|:---------------------------------|:---------------------------------|
| 1 | [C++](hello-grpc-cpp) | | feuyeux/grpc_server_cpp:1.0.0 | feuyeux/grpc_client_cpp:1.0.0 |
| 2 | [Rust](hello-grpc-rust) | | feuyeux/grpc_server_rust:1.0.0 | feuyeux/grpc_client_rust:1.0.0 |
| 3 | [Java](hello-grpc-java) | | feuyeux/grpc_server_java:1.0.0 | feuyeux/grpc_client_java:1.0.0 |
| 4 | [Go](hello-grpc-go) | [golang:1.23-alpine](go_grpc.dockerfile) | feuyeux/grpc_server_go:1.0.0 | feuyeux/grpc_client_go:1.0.0 |
| 5 | [C#](hello-grpc-csharp) | [mcr.microsoft.com/dotnet/runtime:8.0](csharp_grpc.dockerfile) | feuyeux/grpc_server_csharp:1.0.0 | feuyeux/grpc_client_csharp:1.0.0 |
| 6 | [Python](hello-grpc-python) | | feuyeux/grpc_server_python:1.0.0 | feuyeux/grpc_client_python:1.0.0 |
| 7 | [Node.js](hello-grpc-nodejs) | | feuyeux/grpc_server_nodejs:1.0.0 | feuyeux/grpc_client_nodejs:1.0.0 |
| 8 | [Dart](hello-grpc-dart) | | feuyeux/grpc_server_dart:1.0.0 | feuyeux/grpc_client_dart:1.0.0 |
| 9 | [Kotlin](hello-grpc-kotlin) | | feuyeux/grpc_server_kotlin:1.0.0 | feuyeux/grpc_client_kotlin:1.0.0 |
| 10 | [Swift](hello-grpc-swift) | | feuyeux/grpc_server_swift:1.0.0 | feuyeux/grpc_client_swift:1.0.0 |
| 11 | [PHP](hello-grpc-php) | | feuyeux/grpc_server_php:1.0.0 | feuyeux/grpc_client_php:1.0.0 |
| 12 | [TypeScript](hello-grpc-ts) | | feuyeux/grpc_server_ts:1.0.0 | feuyeux/grpc_client_ts:1.0.0 |

## Build

```sh
sh build.sh

langs=(cpp rust java go csharp python nodejs dart kotlin swift php ts)
lang=$langs[2]
sh build_$lang.sh
sh ${lang}_build.sh
```

## push all images
## Run

```sh
sh push.sh
sh ${lang}_run_server.sh
```

## verify
```sh
sh ${lang}_run_client.sh
```

### clean all containers
## Push

```sh
sh tools/clean_world.sh
sh ${lang}_push.sh
```

| No. | Lang | Server | Client |
|:----|:-----------------------------|:---------------------------------|:---------------------------------|
| 1 | [C++](hello-grpc-cpp) | feuyeux/grpc_server_cpp:1.0.0 | feuyeux/grpc_client_cpp:1.0.0 |
| 2 | [Rust](hello-grpc-rust) | feuyeux/grpc_server_rust:1.0.0 | feuyeux/grpc_client_rust:1.0.0 |
| 3 | [Java](hello-grpc-java) | feuyeux/grpc_server_java:1.0.0 | feuyeux/grpc_client_java:1.0.0 |
| 4 | [Go](hello-grpc-go) | feuyeux/grpc_server_go:1.0.0 | feuyeux/grpc_client_go:1.0.0 |
| 5 | [C#](hello-grpc-csharp) | feuyeux/grpc_server_csharp:1.0.0 | feuyeux/grpc_client_csharp:1.0.0 |
| 6 | [Python](hello-grpc-python) | feuyeux/grpc_server_python:1.0.0 | feuyeux/grpc_client_python:1.0.0 |
| 7 | [Node.js](hello-grpc-nodejs) | feuyeux/grpc_server_nodejs:1.0.0 | feuyeux/grpc_client_nodejs:1.0.0 |
| 8 | [Dart](hello-grpc-dart) | feuyeux/grpc_server_dart:1.0.0 | feuyeux/grpc_client_dart:1.0.0 |
| 9 | [Kotlin](hello-grpc-kotlin) | feuyeux/grpc_server_kotlin:1.0.0 | feuyeux/grpc_client_kotlin:1.0.0 |
| 10 | [Swift](hello-grpc-swift) | feuyeux/grpc_server_swift:1.0.0 | feuyeux/grpc_client_swift:1.0.0 |
| 11 | [PHP](hello-grpc-php) | feuyeux/grpc_server_php:1.0.0 | feuyeux/grpc_client_php:1.0.0 |
| 12 | [TypeScript](hello-grpc-ts) | feuyeux/grpc_server_ts:1.0.0 | feuyeux/grpc_client_ts:1.0.0 |
2 changes: 1 addition & 1 deletion docker/csharp_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ elif [[ "${1}" == "s" ]]; then
docker build -f csharp_grpc.dockerfile --target server -t feuyeux/grpc_server_csharp:1.0.0 .
else
echo "build csharp"
docker build -f csharp_grpc.dockerfile --target build -t feuyeux/grpc_csharp:1.0.0 .
docker build -f csharp_grpc.dockerfile --target build -t feuyeux/grpc_build_csharp:1.0.0 .
docker build -f csharp_grpc.dockerfile --target server -t feuyeux/grpc_server_csharp:1.0.0 .
docker build -f csharp_grpc.dockerfile --target client -t feuyeux/grpc_client_csharp:1.0.0 .
fi
Expand Down
4 changes: 2 additions & 2 deletions docker/build_go.sh → docker/go_build.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ echo "~~~ build grpc server golang ~~~"
env GOOS=linux GOARCH=amd64 go build -o proto_server server/proto_server.go
mv proto_server ../docker/
cd ../docker
docker build -f grpc-server-go.dockerfile -t feuyeux/grpc_server_go:1.0.0 .
docker build -f go_grpc.dockerfile --target server -t feuyeux/grpc_server_go:1.0.0 .
rm -rf proto_server
echo

Expand All @@ -23,6 +23,6 @@ export GO111MODULE="on"
env GOOS=linux GOARCH=amd64 go build -o proto_client client/proto_client.go
mv proto_client ../docker/
cd ../docker
docker build -f grpc-client-go.dockerfile -t feuyeux/grpc_client_go:1.0.0 .
docker build -f go_grpc.dockerfile --target client -t feuyeux/grpc_client_go:1.0.0 .
rm -rf proto_client
echo
11 changes: 11 additions & 0 deletions docker/go_grpc.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.23-alpine AS server
# https://hub.docker.com/_/golang
COPY proto_server grpc-server
COPY tls/server_certs /var/hello_grpc/server_certs
COPY tls/client_certs /var/hello_grpc/client_certs
ENTRYPOINT ["./grpc-server"]

FROM golang:1.23-alpine AS client
COPY proto_client grpc-client
COPY tls/client_certs /var/hello_grpc/client_certs
ENTRYPOINT ["./grpc-client"]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22-alpine
FROM golang:1.23-alpine
COPY proto_client grpc-client
COPY tls/client_certs /var/hello_grpc/client_certs
ENTRYPOINT ["./grpc-client"]
3 changes: 3 additions & 0 deletions docker/go_push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
docker push feuyeux/grpc_server_go:1.0.0
docker push feuyeux/grpc_client_go:1.0.0
13 changes: 13 additions & 0 deletions docker/go_run_client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
cd "$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)/" || exit
export CLIENT_NAME=grpc_client_go
export CLIENT_IMG=feuyeux/$CLIENT_NAME:1.0.0
# if there's first argument, it's secure, otherwise insecure
if [ "$1" = "secure" ]; then
sh run_tls_client.sh
else
sh run_insecure_client.sh
fi
13 changes: 13 additions & 0 deletions docker/go_run_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
cd "$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)/" || exit
export SERVER_NAME=grpc_server_go
export SERVER_IMG=feuyeux/$SERVER_NAME:1.0.0
# if there's first argument, it's secure, otherwise insecure
if [ "$1" = "secure" ]; then
sh run_tls_server.sh
else
sh run_insecure_server.sh
fi
5 changes: 0 additions & 5 deletions docker/grpc-server-go.dockerfile

This file was deleted.

9 changes: 0 additions & 9 deletions docker/tools/fix_fmt.sh

This file was deleted.

7 changes: 1 addition & 6 deletions hello-grpc-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ sh proto2go.sh
## 3 Build

```bash
go mod tidy
go fmt hello-grpc/...
go fmt server/proto_server.go
go fmt client/proto_client.go
go install server/proto_server.go
go install client/proto_client.go
sh build.sh
```

## 4 Run
Expand Down
10 changes: 6 additions & 4 deletions hello-grpc-go/client_start.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash
# shellcheck disable=SC2046
cd "$(
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
cd "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)/" || exit
export GO111MODULE="on"

Expand All @@ -16,8 +17,9 @@ elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
export GOPATH=$GOPATH:${PWD}
GRPC_SERVER=$(hostname -I | cut -d' ' -f1)
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ] || [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then
# Do something under Windows NT platform
export GOPATH=$GOPATH;$PWD
windows_path=$GOPATH
linux_path=$(echo "$windows_path" | sed 's/^\([a-zA-Z]\):/\/\1/' | sed 's/\\/\//g')
export GOPATH=$linux_path:${PWD}
GRPC_SERVER=$(ipconfig | grep -A 3 'Ethernet adapter Ethernet' | grep 'IPv4 Address' | cut -d: -f2 | sed 's/ //g')
fi

Expand Down
8 changes: 4 additions & 4 deletions hello-grpc-go/conn/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func buildConnByDisc() *grpc.ClientConn {
Certificates: []tls.Certificate{cert},
RootCAs: GetCertPool(rootCert),
}
conn, err := grpc.Dial("etcd:///",
conn, err := grpc.NewClient("etcd:///",
grpc.WithStatsHandler(&StatsHandler{}),
grpc.WithTransportCredentials(credentials.NewTLS(c)),
grpc.WithDefaultServiceConfig(grpcServiceConfig))
Expand All @@ -84,7 +84,7 @@ func buildConnByDisc() *grpc.ClientConn {
return conn
} else {
log.Infof("Connect With InSecure through discovery")
conn, err := grpc.Dial("etcd:///",
conn, err := grpc.NewClient("etcd:///",
grpc.WithStatsHandler(&StatsHandler{}),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultServiceConfig(grpcServiceConfig))
Expand Down Expand Up @@ -133,7 +133,7 @@ func transportInsecure(address string) (*grpc.ClientConn, error) {
Timeout: time.Second, // wait 1 second for ping ack before considering the connection dead
PermitWithoutStream: true, // send pings even without active streams
})
return grpc.Dial(address,
return grpc.NewClient(address,
grpc.WithTransportCredentials(insecure.NewCredentials()),
keepaliveConfig,
retryConfig,
Expand All @@ -146,7 +146,7 @@ func transportCredentials(address string) (*grpc.ClientConn, error) {
if err != nil {
panic(err)
}
return grpc.Dial(address, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
return grpc.NewClient(address, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
ServerName: serverName,
Certificates: []tls.Certificate{cert},
RootCAs: GetCertPool(rootCert),
Expand Down
4 changes: 2 additions & 2 deletions hello-grpc-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module hello-grpc

go 1.22
go 1.23

require (
// https://github.com/google/uuid/tags
Expand All @@ -22,7 +22,7 @@ require (
// https://github.com/grpc/grpc-go/tags
google.golang.org/grpc v1.67.0
// https://github.com/protocolbuffers/protobuf-go/tags
google.golang.org/protobuf v1.34.2 // indirect
google.golang.org/protobuf v1.34.2
)

require (
Expand Down
26 changes: 0 additions & 26 deletions hello-grpc-go/go.mod.bk

This file was deleted.

22 changes: 11 additions & 11 deletions hello-grpc-go/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ import (
// DInfo dev info
func DInfo(msg string) string {
id := uuid.New().String()
filename, line, funcname, funcname1, funcname2 := buildLogParams()
log.Infof("[%s] %s[%d]:%s<-%s<-%s: %s\n", id, filename, line, funcname, funcname1, funcname2, msg)
filename, line, funcName, funcName1, funcName2 := buildLogParams()
log.Infof("[%s] %s[%d]:%s<-%s<-%s: %s\n", id, filename, line, funcName, funcName1, funcName2, msg)
return id
}

// TInfo dev tracing info
func TInfo(id, msg string) {
filename, line, funcname, funcname1, funcname2 := buildLogParams()
log.Infof("[%s] %s[%d]:%s<-%s<-%s: %s\n", id, filename, line, funcname, funcname1, funcname2, msg)
filename, line, funcName, funcName1, funcName2 := buildLogParams()
log.Infof("[%s] %s[%d]:%s<-%s<-%s: %s\n", id, filename, line, funcName, funcName1, funcName2, msg)
}

func DInfof(format string, args ...interface{}) {
filename, line, funcname, funcname1, funcname2 := buildLogParams()
log.Infof("%s[%d]:%s<-%s<-%s: %s\n", filename, line, funcname, funcname1, funcname2, fmt.Sprintf(format, args...))
filename, line, funcName, funcName1, funcName2 := buildLogParams()
log.Infof("%s[%d]:%s<-%s<-%s: %s\n", filename, line, funcName, funcName1, funcName2, fmt.Sprintf(format, args...))
}

func buildLogParams() (string, int, string, string, string) {
return buildLogParams0(3)
}

func buildLogParams0(skip int) (string, int, string, string, string) {
funcName, funcName1, funcname2, line, filename := "???", "???", "???", 0, "???"
funcName, funcName1, funcName2, line, filename := "???", "???", "???", 0, "???"
pc, filename, line, ok := runtime.Caller(skip)
if ok {
funcName = runtime.FuncForPC(pc).Name()
Expand All @@ -50,9 +50,9 @@ func buildLogParams0(skip int) (string, int, string, string, string) {
}
pc2, _, _, ok := runtime.Caller(skip + 2)
if ok {
funcname2 = runtime.FuncForPC(pc2).Name()
funcname2 = filepath.Ext(funcname2)
funcname2 = strings.TrimPrefix(funcname2, ".")
funcName2 = runtime.FuncForPC(pc2).Name()
funcName2 = filepath.Ext(funcName2)
funcName2 = strings.TrimPrefix(funcName2, ".")
}
return filename, line, funcName, funcName1, funcname2
return filename, line, funcName, funcName1, funcName2
}
27 changes: 20 additions & 7 deletions hello-grpc-go/proto2go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,28 @@ cd "$(
pwd -P
)/" || exit
export GOROOT=$(go env GOROOT)
echo "GOROOT=$GOROOT"
export GOPATH=$HOME/gopath
export GOPATH=$(go env GOPATH)
echo "GOROOT=$GOROOT GOPATH=$GOPATH"
export PATH="$PATH:$GOPATH/bin"
#
export GOPROXY=https://mirrors.aliyun.com/goproxy/
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# proto2go.sh: 18: protoc: not found
PROTOC_VERSION=$(protoc --version)
if [ -z "$PROTOC_VERSION" ]; then
echo "install protoc firstly, see doc/proto.md"
exit 1
else
echo "Protoc version: $PROTOC_VERSION"
fi

if [ ! -f "$GOPATH/bin/protoc-gen-go.exe" ]; then
export GOPROXY=https://mirrors.aliyun.com/goproxy/
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install -v golang.org/x/tools/gopls@latest
echo "protoc-gen-go.exe has been installed successfully."
else
echo "protoc-gen-go.exe is already installed."
fi

echo "generate the messages"
protoc --go_out=. ./proto/landing.proto
echo "generate the services"
Expand Down
Loading

0 comments on commit af5b662

Please sign in to comment.