Skip to content
This repository was archived by the owner on Nov 12, 2024. It is now read-only.

Commit f4a2d1a

Browse files
committed
Copy from private repo
1 parent e737c04 commit f4a2d1a

File tree

15 files changed

+715
-5
lines changed

15 files changed

+715
-5
lines changed

.gitignore

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
##### Go #####
2+
3+
# Binaries for programs and plugins
4+
*.exe
5+
*.exe~
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, built with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Dependency directories (remove the comment below to include it)
17+
# vendor/
18+
19+
##### VisualStudioCode #####
20+
21+
.vscode/*
22+
*.code-workspace
23+
24+
# Local History for Visual Studio Code
25+
.history/
26+
27+
##### go-task #####
28+
29+
.task
30+
31+
##### goreleaser #####
32+
33+
dist/

.goreleaser.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
project_name: grpc-examples
2+
before:
3+
hooks:
4+
# You may remove this if you don't use go modules.
5+
- go mod download
6+
# # you may remove this if you don't need go generate
7+
# - go generate ./...
8+
builds:
9+
- dir: examples/helloworld/greeter_client
10+
id: "greeter_client"
11+
binary: greeter_client
12+
env:
13+
- CGO_ENABLED=0
14+
goos:
15+
- linux
16+
- windows
17+
- darwin
18+
- dir: examples/helloworld/greeter_server
19+
id: "greeter_server"
20+
binary: greeter_server
21+
env:
22+
- CGO_ENABLED=0
23+
goos:
24+
- linux
25+
- windows
26+
- darwin
27+
goarch:
28+
- amd64
29+
- arm64
30+
goarm:
31+
- 6
32+
- 7
33+
archives:
34+
- replacements:
35+
darwin: Darwin
36+
linux: Linux
37+
windows: Windows
38+
386: i386
39+
amd64: x86_64
40+
checksum:
41+
name_template: 'checksums.txt'
42+
snapshot:
43+
name_template: "{{ .Tag }}"
44+
changelog:
45+
sort: asc
46+
filters:
47+
exclude:
48+
- '^docs:'
49+
- '^test:'

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
## My Project
1+
# gRPC Examples
22

3-
TODO: Fill this README out!
3+
gRPC examples and feature references
44

5-
Be sure to:
5+
## Example applications
66

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
7+
* [helloworld](examples/helloworld/) - Forked from [grpc/grpc-go/examples/helloworld](https://github.com/grpc/grpc-go/tree/master/examples/helloworld)
8+
9+
## Building examples
10+
11+
This project uses [Task](https://taskfile.dev) to build and run examples. Each `Taskfile.yml` can be used to build and run each example or variation.
912

1013
## Security
1114

examples/helloworld/Taskfile.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '3'
2+
3+
vars:
4+
CLIENT_APP: greeter_client
5+
SERVER_APP: greeter_server
6+
REGISTRY: ""
7+
REPOSITORY: aws-samples
8+
TAG: latest
9+
PROTODIR: helloworld
10+
11+
includes:
12+
client:
13+
taskfile: ./greeter_client
14+
dir: ./greeter_client
15+
server:
16+
taskfile: ./greeter_server
17+
dir: ./greeter_server
18+
19+
tasks:
20+
proto:
21+
desc: "[Re]Generate go code for protocol buffers."
22+
cmds:
23+
- protoc -I {{.PROTODIR}} --go_out=paths=source_relative:{{.PROTODIR}} --go-grpc_out=paths=source_relative:{{.PROTODIR}} helloworld.proto
24+
build-container:
25+
desc: Builds and pushes this example's containers.
26+
cmds:
27+
- task: client:buildx-container
28+
- task: server:buildx-container
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM golang:1.14.1 as builder
2+
RUN CGO_ENABLED=0 go get github.com/aws-samples/grpc-examples/examples/helloworld/greeter_client
3+
4+
FROM scratch
5+
COPY --from=builder /go/bin/greeter_client /greeter_client
6+
CMD ["/greeter_client"]
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: '3'
2+
3+
vars:
4+
CLIENT_APP: greeter_client
5+
REGISTRY: ""
6+
REPOSITORY: aws-samples
7+
TAG: latest
8+
9+
tasks:
10+
build:
11+
desc: Build the go binary.
12+
cmds:
13+
- go install .
14+
env:
15+
CGO_ENABLED: 0
16+
sources:
17+
- ./*.go
18+
generates:
19+
- ./{{.CLIENT_APP}}{{exeExt}}
20+
method: checksum
21+
clean:
22+
desc: Deletes the go binary.
23+
cmds:
24+
- go clean -i .
25+
build-container:
26+
desc: Build the Docker container locally.
27+
cmds:
28+
- docker build -t {{.REGISTRY}}{{if ne .REGISTRY ""}}/{{end}}{{.REPOSITORY}}{{if ne .REPOSITORY ""}}/{{end}}{{.CLIENT_APP}}:{{.TAG}} .
29+
buildx-container:
30+
desc: Builds and pushes the multi-architecture image.
31+
cmds:
32+
- docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag {{.REGISTRY}}{{if ne .REGISTRY ""}}/{{end}}{{.REPOSITORY}}{{if ne .REPOSITORY ""}}/{{end}}{{.CLIENT_APP}}:{{.TAG}} --push .
33+
run-container:
34+
desc: Run the Docker container locally.
35+
cmds:
36+
- docker run -d --name {{.CLIENT_APP}} --link greeter_server -e "GREETER_ENDPOINT=greeter_server:50051" {{.REGISTRY}}{{if ne .REGISTRY ""}}/{{end}}{{.REPOSITORY}}{{if ne .REPOSITORY ""}}/{{end}}{{.CLIENT_APP}}:{{.TAG}}
37+
clean-container:
38+
desc: Stops and removes the container locally.
39+
cmds:
40+
- cmd: docker stop {{.CLIENT_APP}} > /dev/null 2>&1
41+
ignore_error: true
42+
- cmd: docker rm {{.CLIENT_APP}} > /dev/null 2>&1
43+
ignore_error: true
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
*
3+
* Copyright 2015 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
// Package main implements a client for Greeter service.
20+
package main
21+
22+
import (
23+
"context"
24+
"log"
25+
"os"
26+
"time"
27+
28+
pb "github.com/aws-samples/grpc-examples/examples/helloworld/helloworld"
29+
"google.golang.org/grpc"
30+
)
31+
32+
const (
33+
defaultName = "world"
34+
)
35+
36+
var (
37+
address = "localhost:50051"
38+
)
39+
40+
func init() {
41+
if ep, ok := os.LookupEnv("GREETER_ENDPOINT"); ok {
42+
address = ep
43+
}
44+
}
45+
46+
func main() {
47+
// Set up a connection to the server.
48+
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
49+
if err != nil {
50+
log.Fatalf("did not connect: %v", err)
51+
}
52+
defer conn.Close()
53+
c := pb.NewGreeterClient(conn)
54+
55+
// Contact the server and print out its response.
56+
name := defaultName
57+
if len(os.Args) > 1 {
58+
name = os.Args[1]
59+
}
60+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
61+
defer cancel()
62+
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
63+
if err != nil {
64+
log.Fatalf("could not greet: %v", err)
65+
}
66+
log.Printf("Greeting: %s", r.GetMessage())
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM golang:1.14.1 as builder
2+
RUN CGO_ENABLED=0 go get github.com/grpc-ecosystem/grpc-health-probe
3+
RUN CGO_ENABLED=0 go get github.com/aws-samples/grpc-examples/examples/helloworld/greeter_server
4+
5+
FROM scratch
6+
COPY --from=builder /go/bin/grpc-health-probe /grpc-health-probe
7+
COPY --from=builder /go/bin/greeter_server /greeter_server
8+
EXPOSE 50051
9+
CMD ["/greeter_server"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: '3'
2+
3+
vars:
4+
SERVER_APP: greeter_server
5+
REGISTRY: ""
6+
REPOSITORY: aws-samples
7+
TAG: latest
8+
9+
tasks:
10+
build:
11+
desc: Build the go binary.
12+
cmds:
13+
- go install .
14+
env:
15+
CGO_ENABLED: 0
16+
sources:
17+
- ./*.go
18+
generates:
19+
- ./{{.SERVER_APP}}{{exeExt}}
20+
method: checksum
21+
clean:
22+
desc: Deletes the go binary.
23+
cmds:
24+
- go clean -i .
25+
build-container:
26+
desc: Build the Docker container locally.
27+
cmds:
28+
- docker build -t {{.REGISTRY}}{{if ne .REGISTRY ""}}/{{end}}{{.REPOSITORY}}{{if ne .REPOSITORY ""}}/{{end}}{{.SERVER_APP}}:{{.TAG}} .
29+
buildx-container:
30+
desc: Builds and pushes the multi-architecture image.
31+
cmds:
32+
- docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag {{.REGISTRY}}{{if ne .REGISTRY ""}}/{{end}}{{.REPOSITORY}}{{if ne .REPOSITORY ""}}/{{end}}{{.SERVER_APP}}:{{.TAG}} --push .
33+
run-container:
34+
desc: Run the Docker container locally.
35+
cmds:
36+
- docker run -d --name {{.SERVER_APP}} -p 50051:50051 {{.REGISTRY}}{{if ne .REGISTRY ""}}/{{end}}{{.REPOSITORY}}{{if ne .REPOSITORY ""}}/{{end}}{{.SERVER_APP}}:{{.TAG}}
37+
clean-container:
38+
desc: Stops and removes the container locally.
39+
cmds:
40+
- cmd: docker stop {{.SERVER_APP}} > /dev/null 2>&1
41+
ignore_error: true
42+
- cmd: docker rm {{.SERVER_APP}} > /dev/null 2>&1
43+
ignore_error: true
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
*
3+
* Copyright 2015 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
// Package main implements a server for Greeter service.
20+
package main
21+
22+
import (
23+
"context"
24+
"log"
25+
"net"
26+
27+
pb "github.com/aws-samples/grpc-examples/examples/helloworld/helloworld"
28+
"google.golang.org/grpc"
29+
"google.golang.org/grpc/health"
30+
"google.golang.org/grpc/health/grpc_health_v1"
31+
)
32+
33+
const (
34+
port = ":50051"
35+
)
36+
37+
// server is used to implement helloworld.GreeterServer.
38+
type server struct {
39+
pb.UnimplementedGreeterServer
40+
}
41+
42+
// sayHello implements helloworld.GreeterServer.SayHello
43+
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
44+
log.Printf("Received: %v", in.GetName())
45+
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
46+
}
47+
48+
func main() {
49+
lis, err := net.Listen("tcp", port)
50+
if err != nil {
51+
log.Fatalf("failed to listen: %v", err)
52+
}
53+
s := grpc.NewServer()
54+
pb.RegisterGreeterServer(s, &server{})
55+
grpc_health_v1.RegisterHealthServer(s, health.NewServer())
56+
if err := s.Serve(lis); err != nil {
57+
log.Fatalf("failed to serve: %v", err)
58+
}
59+
}

0 commit comments

Comments
 (0)