Skip to content

Commit 978093a

Browse files
committed
Initial commit
0 parents  commit 978093a

File tree

13 files changed

+609
-0
lines changed

13 files changed

+609
-0
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.gitignore
2+
Dockerfile
3+
Makefile

.github/workflows/merge-main.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Build and push Docker image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
push:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- name: Login to Quay
16+
uses: docker/login-action@v3
17+
with:
18+
registry: quay.io
19+
username: ${{ vars.DOCKER_USERNAME }}
20+
password: ${{ secrets.DOCKER_PASSWORD }}
21+
22+
- name: Set up Docker BuildX
23+
uses: docker/setup-buildx-action@v3
24+
with:
25+
version: v0.11.2
26+
27+
- name: Restore build cache
28+
uses: actions/cache@v3
29+
with:
30+
path: /tmp/.buildx-${{ github.ref_name }}-cache
31+
key: ${{ runner.os }}-buildx-${{ github.ref_name }}
32+
restore-keys: |
33+
${{ runner.os }}-buildx-${{ github.ref_name }}
34+
35+
- name: Build and push
36+
run: |
37+
make push
38+
env:
39+
BRANCH_NAME: ${{ github.ref_name }}
40+
RELEASE_ID: ${{ github.run_number }}
41+
GIT_HASH: ${{ github.sha }}

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM golang:1.23-bookworm AS build
2+
WORKDIR /app
3+
4+
COPY go.mod ./
5+
COPY go.sum ./
6+
RUN go mod download
7+
8+
COPY ./ ./
9+
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /echo-grpc ./cmd/server
10+
11+
FROM gcr.io/distroless/base-debian12
12+
WORKDIR /
13+
COPY --from=build echo-grpc /echo-grpc
14+
ENTRYPOINT [ "/echo-grpc" ]

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
DOCKER_REPOSITORY ?= quay.io/road
2+
BRANCH_NAME ?= $(shell git rev-parse --abbrev-ref HEAD)
3+
GIT_HASH ?= $(shell git rev-parse HEAD)
4+
RELEASE_ID ?= $(shell id -un)-local
5+
DOCKER_LATEST_TAG ?= latest
6+
7+
build:
8+
docker buildx build \
9+
--cache-from type=local,src=/tmp/.buildx-$(BRANCH_NAME)-cache \
10+
--cache-to type=local,mode=max,dest=/tmp/.buildx-$(BRANCH_NAME)-cache \
11+
--provenance mode=min,inline-only=true \
12+
--tag $(DOCKER_REPOSITORY)/echo-grpc:$(GIT_HASH) \
13+
--tag $(DOCKER_REPOSITORY)/echo-grpc:$(DOCKER_LATEST_TAG) \
14+
--tag $(DOCKER_REPOSITORY)/echo-grpc:release-$(RELEASE_ID) \
15+
--push \
16+
.
17+
18+
push: build

buf.gen.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: v2
2+
clean: true
3+
managed:
4+
enabled: true
5+
override:
6+
- file_option: go_package_prefix
7+
value: github.com/e-flux-platform/echo-grpc/gen/go
8+
plugins:
9+
- remote: buf.build/protocolbuffers/go
10+
out: gen/go
11+
opt: paths=source_relative
12+
- remote: buf.build/grpc/go
13+
out: gen/go
14+
opt:
15+
- paths=source_relative
16+
- require_unimplemented_servers=false
17+
inputs:
18+
- directory: proto

buf.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: v2
2+
modules:
3+
- path: proto
4+
lint:
5+
use:
6+
- DEFAULT
7+
breaking:
8+
use:
9+
- FILE

cmd/client/main.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log/slog"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
11+
_ "github.com/joho/godotenv/autoload"
12+
"github.com/urfave/cli/v2"
13+
"google.golang.org/grpc"
14+
"google.golang.org/grpc/credentials/insecure"
15+
16+
echov1 "github.com/e-flux-platform/echo-grpc/gen/go/road/echo/v1"
17+
)
18+
19+
func main() {
20+
var serverAddr string
21+
22+
app := &cli.App{
23+
Flags: []cli.Flag{
24+
&cli.StringFlag{
25+
Name: "server-addr",
26+
EnvVars: []string{"SERVER_ADDR"},
27+
Destination: &serverAddr,
28+
Required: true,
29+
},
30+
},
31+
Action: func(cCtx *cli.Context) error {
32+
ctx, cancel := signal.NotifyContext(cCtx.Context, syscall.SIGTERM, syscall.SIGINT)
33+
defer cancel()
34+
35+
return run(ctx, serverAddr, cCtx.Args().First())
36+
},
37+
}
38+
39+
if err := app.RunContext(context.Background(), os.Args); err != nil {
40+
slog.Error("exiting", slog.Any("error", err))
41+
os.Exit(1)
42+
}
43+
}
44+
45+
func run(ctx context.Context, serverAddr, message string) error {
46+
conn, err := grpc.NewClient(serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
47+
if err != nil {
48+
return err
49+
}
50+
defer conn.Close()
51+
52+
client := echov1.NewEchoServiceClient(conn)
53+
54+
res, err := client.Echo(ctx, &echov1.EchoRequest{Message: message})
55+
if err != nil {
56+
return err
57+
}
58+
59+
fmt.Printf("received from server: %s\n", res.Message)
60+
61+
return nil
62+
}

cmd/server/main.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"net"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
11+
_ "github.com/joho/godotenv/autoload"
12+
"github.com/urfave/cli/v2"
13+
"golang.org/x/sync/errgroup"
14+
"google.golang.org/grpc"
15+
16+
echov1 "github.com/e-flux-platform/echo-grpc/gen/go/road/echo/v1"
17+
)
18+
19+
func main() {
20+
var listenAddr string
21+
22+
app := &cli.App{
23+
Flags: []cli.Flag{
24+
&cli.StringFlag{
25+
Name: "listen-addr",
26+
EnvVars: []string{"LISTEN_ADDR"},
27+
Destination: &listenAddr,
28+
Required: true,
29+
},
30+
},
31+
Action: func(cCtx *cli.Context) error {
32+
ctx, cancel := signal.NotifyContext(cCtx.Context, syscall.SIGTERM, syscall.SIGINT)
33+
defer cancel()
34+
35+
return run(ctx, listenAddr)
36+
},
37+
}
38+
39+
if err := app.RunContext(context.Background(), os.Args); err != nil {
40+
slog.Error("exiting", slog.Any("error", err))
41+
os.Exit(1)
42+
}
43+
}
44+
45+
func run(ctx context.Context, listenAddr string) error {
46+
lis, err := net.Listen("tcp", listenAddr)
47+
if err != nil {
48+
return err
49+
}
50+
51+
srv := grpc.NewServer()
52+
echov1.RegisterEchoServiceServer(srv, &server{})
53+
54+
eg, ctx := errgroup.WithContext(ctx)
55+
eg.Go(func() error {
56+
return srv.Serve(lis)
57+
})
58+
eg.Go(func() error {
59+
<-ctx.Done()
60+
srv.GracefulStop()
61+
return nil
62+
})
63+
return eg.Wait()
64+
}
65+
66+
type server struct{}
67+
68+
func (s *server) Echo(ctx context.Context, request *echov1.EchoRequest) (*echov1.EchoResponse, error) {
69+
return &echov1.EchoResponse{Message: request.Message}, nil
70+
}

0 commit comments

Comments
 (0)