Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-jones committed Dec 6, 2024
0 parents commit 978093a
Show file tree
Hide file tree
Showing 13 changed files with 609 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.gitignore
Dockerfile
Makefile
41 changes: 41 additions & 0 deletions .github/workflows/merge-main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build and push Docker image

on:
push:
branches:
- main

jobs:
push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Login to Quay
uses: docker/login-action@v3
with:
registry: quay.io
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Set up Docker BuildX
uses: docker/setup-buildx-action@v3
with:
version: v0.11.2

- name: Restore build cache
uses: actions/cache@v3
with:
path: /tmp/.buildx-${{ github.ref_name }}-cache
key: ${{ runner.os }}-buildx-${{ github.ref_name }}
restore-keys: |
${{ runner.os }}-buildx-${{ github.ref_name }}
- name: Build and push
run: |
make push
env:
BRANCH_NAME: ${{ github.ref_name }}
RELEASE_ID: ${{ github.run_number }}
GIT_HASH: ${{ github.sha }}
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.23-bookworm AS build
WORKDIR /app

COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY ./ ./
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /echo-grpc ./cmd/server

FROM gcr.io/distroless/base-debian12
WORKDIR /
COPY --from=build echo-grpc /echo-grpc
ENTRYPOINT [ "/echo-grpc" ]
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
DOCKER_REPOSITORY ?= quay.io/road
BRANCH_NAME ?= $(shell git rev-parse --abbrev-ref HEAD)
GIT_HASH ?= $(shell git rev-parse HEAD)
RELEASE_ID ?= $(shell id -un)-local
DOCKER_LATEST_TAG ?= latest

build:
docker buildx build \
--cache-from type=local,src=/tmp/.buildx-$(BRANCH_NAME)-cache \
--cache-to type=local,mode=max,dest=/tmp/.buildx-$(BRANCH_NAME)-cache \
--provenance mode=min,inline-only=true \
--tag $(DOCKER_REPOSITORY)/echo-grpc:$(GIT_HASH) \
--tag $(DOCKER_REPOSITORY)/echo-grpc:$(DOCKER_LATEST_TAG) \
--tag $(DOCKER_REPOSITORY)/echo-grpc:release-$(RELEASE_ID) \
--push \
.

push: build
18 changes: 18 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: v2
clean: true
managed:
enabled: true
override:
- file_option: go_package_prefix
value: github.com/e-flux-platform/echo-grpc/gen/go
plugins:
- remote: buf.build/protocolbuffers/go
out: gen/go
opt: paths=source_relative
- remote: buf.build/grpc/go
out: gen/go
opt:
- paths=source_relative
- require_unimplemented_servers=false
inputs:
- directory: proto
9 changes: 9 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v2
modules:
- path: proto
lint:
use:
- DEFAULT
breaking:
use:
- FILE
62 changes: 62 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"

_ "github.com/joho/godotenv/autoload"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

echov1 "github.com/e-flux-platform/echo-grpc/gen/go/road/echo/v1"
)

func main() {
var serverAddr string

app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "server-addr",
EnvVars: []string{"SERVER_ADDR"},
Destination: &serverAddr,
Required: true,
},
},
Action: func(cCtx *cli.Context) error {
ctx, cancel := signal.NotifyContext(cCtx.Context, syscall.SIGTERM, syscall.SIGINT)
defer cancel()

return run(ctx, serverAddr, cCtx.Args().First())
},
}

if err := app.RunContext(context.Background(), os.Args); err != nil {
slog.Error("exiting", slog.Any("error", err))
os.Exit(1)
}
}

func run(ctx context.Context, serverAddr, message string) error {
conn, err := grpc.NewClient(serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
}
defer conn.Close()

client := echov1.NewEchoServiceClient(conn)

res, err := client.Echo(ctx, &echov1.EchoRequest{Message: message})
if err != nil {
return err
}

fmt.Printf("received from server: %s\n", res.Message)

return nil
}
70 changes: 70 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"context"
"log/slog"
"net"
"os"
"os/signal"
"syscall"

_ "github.com/joho/godotenv/autoload"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"

echov1 "github.com/e-flux-platform/echo-grpc/gen/go/road/echo/v1"
)

func main() {
var listenAddr string

app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen-addr",
EnvVars: []string{"LISTEN_ADDR"},
Destination: &listenAddr,
Required: true,
},
},
Action: func(cCtx *cli.Context) error {
ctx, cancel := signal.NotifyContext(cCtx.Context, syscall.SIGTERM, syscall.SIGINT)
defer cancel()

return run(ctx, listenAddr)
},
}

if err := app.RunContext(context.Background(), os.Args); err != nil {
slog.Error("exiting", slog.Any("error", err))
os.Exit(1)
}
}

func run(ctx context.Context, listenAddr string) error {
lis, err := net.Listen("tcp", listenAddr)
if err != nil {
return err
}

srv := grpc.NewServer()
echov1.RegisterEchoServiceServer(srv, &server{})

eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
return srv.Serve(lis)
})
eg.Go(func() error {
<-ctx.Done()
srv.GracefulStop()
return nil
})
return eg.Wait()
}

type server struct{}

func (s *server) Echo(ctx context.Context, request *echov1.EchoRequest) (*echov1.EchoResponse, error) {
return &echov1.EchoResponse{Message: request.Message}, nil
}
Loading

0 comments on commit 978093a

Please sign in to comment.