-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 978093a
Showing
13 changed files
with
609 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.gitignore | ||
Dockerfile | ||
Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.