Skip to content

Commit c5e2026

Browse files
authored
Merge pull request #2 from eigr/feat/try-go-api
feat: initial project structure
2 parents 23c7e8d + ee6bb12 commit c5e2026

39 files changed

+8875
-0
lines changed

Diff for: Dockerfile

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Step 1: Build the Go binary
2+
FROM golang:1.23-alpine as builder
3+
4+
# Installs dependencies for compilation and UPX
5+
RUN apk update && apk add --no-cache \
6+
build-base \
7+
upx \
8+
git \
9+
&& rm -rf /var/cache/apk/*
10+
11+
# Working directory where the Go code will be copied
12+
WORKDIR /app
13+
14+
# Copies go.mod and go.sum to resolve dependencies
15+
COPY examples/go.mod examples/go.sum ./
16+
17+
RUN go mod tidy
18+
19+
COPY examples/ .
20+
21+
# Compiles the Go binary in production-optimized mode
22+
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o /bin/examples/app .
23+
24+
# Step 2: Apply UPX to the binary in a separate intermediate step
25+
FROM alpine:latest as compress-step
26+
27+
# Install UPX
28+
RUN apk add --no-cache upx
29+
30+
# Copy the Go binary from the builder step and apply UPX
31+
COPY --from=builder /bin/examples/app /app/app
32+
RUN upx --best --ultra-brute /app/app
33+
34+
# Step 3: Creating the final image using scratch
35+
FROM scratch
36+
37+
# Create a non-root user and group with meaningful names
38+
USER nobody:nogroup
39+
40+
# Copy the optimized binary from the UPX step
41+
COPY --from=compress-step /app/app /app/app
42+
43+
# Sets the default command to run the binary
44+
CMD ["/app/app"]

Diff for: Makefile

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
ROOT_DIR=$(shell pwd)
2+
SPAWN_DIR=$(ROOT_DIR)/spawn
3+
SPAWN_PROTO_DIR=$(SPAWN_DIR)/protos
4+
SPAWN_PROTO_EXT_DIR=$(SPAWN_PROTO_DIR)/eigr/functions/protocol/actors
5+
GOOGLE_API_DIR=$(SPAWN_PROTO_DIR)/google/api
6+
GOOGLE_PROTOBUF_DIR=$(SPAWN_PROTO_DIR)/google/protobuf
7+
GRPC_DIR=$(SPAWN_PROTO_DIR)/grpc/reflection/v1alpha
8+
EXAMPLES_PROTO_DIR=$(ROOT_DIR)/examples/protos
9+
SPAWN_PROTO_OUT_DIR=$(SPAWN_DIR)
10+
EXAMPLES_PROTO_OUT_DIR=$(ROOT_DIR)/examples
11+
12+
IMAGE_NAME = eigr/spawn-example:latest
13+
DOCKERFILE = Dockerfile
14+
15+
GO_CMD=go
16+
GO_FLAGS=-v
17+
GO_BUILD_FLAGS_DEBUG=-gcflags="all=-N -l"
18+
19+
# Diretório de saída para binários
20+
BIN_DIR=./bin
21+
EXAMPLES_BIN=$(BIN_DIR)/examples/app
22+
EXAMPLES_DIR=./examples
23+
24+
PROTOC=protoc
25+
PROTOC_GEN_GO=$(shell go env GOPATH)/bin/protoc-gen-go
26+
27+
# Auxiliary variables
28+
SPAWN_PROTO_FILES=$(wildcard $(SPAWN_PROTO_EXT_DIR)/*.proto)
29+
EXAMPLES_PROTO_FILES=$(wildcard $(EXAMPLES_PROTO_DIR)/*.proto)
30+
31+
# Check if the Protobuf plugin is installed
32+
$(PROTOC_GEN_GO):
33+
@echo "Instalando protoc-gen-go..."
34+
@go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
35+
36+
# Protobuf code generation for spawn
37+
.PHONY: proto-spawn
38+
proto-spawn: $(PROTOC_GEN_GO)
39+
@echo "Generating Go code from spawn Protobuf files..."
40+
@$(PROTOC) \
41+
--proto_path=$(SPAWN_PROTO_DIR) \
42+
--proto_path=$(GOOGLE_API_DIR) \
43+
--proto_path=$(GOOGLE_PROTOBUF_DIR) \
44+
--proto_path=$(GRPC_DIR) \
45+
--go_out=$(SPAWN_PROTO_OUT_DIR) \
46+
--go_opt=paths=source_relative \
47+
$(SPAWN_PROTO_FILES)
48+
49+
# Protobuf code generation for examples
50+
.PHONY: proto-examples
51+
proto-examples: $(PROTOC_GEN_GO)
52+
@echo "Generating Go code from the examples' Protobuf files..."
53+
@$(PROTOC) \
54+
--proto_path=$(EXAMPLES_PROTO_DIR) \
55+
--proto_path=$(GOOGLE_API_DIR) \
56+
--proto_path=$(GOOGLE_PROTOBUF_DIR) \
57+
--go_out=$(EXAMPLES_PROTO_OUT_DIR) \
58+
--go_opt=paths=source_relative \
59+
$(EXAMPLES_PROTO_FILES)
60+
61+
# Protobuf code generation for the entire project
62+
.PHONY: proto
63+
proto: proto-spawn proto-examples
64+
65+
# Spawn directory setup
66+
.PHONY: setup-spawn
67+
setup-spawn:
68+
@echo "Setting dependencies for the spawn directory..."
69+
@cd spawn && go mod tidy
70+
71+
# Examples directory setup
72+
.PHONY: setup-examples
73+
setup-examples:
74+
@echo "Setting dependencies for the examples directory..."
75+
@cd examples && go mod tidy
76+
77+
# Complete project setup
78+
.PHONY: setup
79+
setup: setup-spawn setup-examples
80+
81+
# Cleaning up files generated at spawn
82+
.PHONY: clean-spawn
83+
clean-spawn:
84+
@echo "Removing files generated by Protobuf on spawn..."
85+
@rm -f $(SPAWN_PROTO_OUT_DIR)/*.pb.go
86+
87+
# Cleaning up files generated in the examples
88+
.PHONY: clean-examples
89+
clean-examples:
90+
@echo "Removing Protobuf generated files in the examples..."
91+
@rm -f $(EXAMPLES_PROTO_OUT_DIR)/*.pb.go
92+
93+
# Cleaning the entire project
94+
.PHONY: clean
95+
clean: clean-spawn clean-examples
96+
97+
# Bbuild for the examples directory in development mode (debug)
98+
build-debug:
99+
@echo "Compiling the examples directory in debug mode..."
100+
$(GO_CMD) build $(GO_FLAGS) $(GO_BUILD_FLAGS_DEBUG) -o $(EXAMPLES_BIN) $(EXAMPLES_DIR)
101+
@echo "Build de debug finalizado em $(EXAMPLES_BIN)"
102+
103+
# Standard task to compile the examples
104+
build: build-debug
105+
106+
# Task to run the generated binary
107+
run:
108+
@echo "Running the example binary..."
109+
$(EXAMPLES_BIN)
110+
111+
# Task to compile the Dockerfile and generate the final image
112+
docker-build:
113+
docker build -t $(IMAGE_NAME) -f $(DOCKERFILE) .
114+
115+
# Task to run the binary inside the container
116+
docker-run:
117+
docker run --rm $(IMAGE_NAME)

Diff for: bin/examples/app

4.82 MB
Binary file not shown.

0 commit comments

Comments
 (0)