Skip to content

Commit

Permalink
Merge pull request #2 from eigr/feat/try-go-api
Browse files Browse the repository at this point in the history
feat: initial project structure
  • Loading branch information
sleipnir authored Dec 9, 2024
2 parents 23c7e8d + ee6bb12 commit c5e2026
Show file tree
Hide file tree
Showing 39 changed files with 8,875 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Step 1: Build the Go binary
FROM golang:1.23-alpine as builder

# Installs dependencies for compilation and UPX
RUN apk update && apk add --no-cache \
build-base \
upx \
git \
&& rm -rf /var/cache/apk/*

# Working directory where the Go code will be copied
WORKDIR /app

# Copies go.mod and go.sum to resolve dependencies
COPY examples/go.mod examples/go.sum ./

RUN go mod tidy

COPY examples/ .

# Compiles the Go binary in production-optimized mode
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o /bin/examples/app .

# Step 2: Apply UPX to the binary in a separate intermediate step
FROM alpine:latest as compress-step

# Install UPX
RUN apk add --no-cache upx

# Copy the Go binary from the builder step and apply UPX
COPY --from=builder /bin/examples/app /app/app
RUN upx --best --ultra-brute /app/app

# Step 3: Creating the final image using scratch
FROM scratch

# Create a non-root user and group with meaningful names
USER nobody:nogroup

# Copy the optimized binary from the UPX step
COPY --from=compress-step /app/app /app/app

# Sets the default command to run the binary
CMD ["/app/app"]
117 changes: 117 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
ROOT_DIR=$(shell pwd)
SPAWN_DIR=$(ROOT_DIR)/spawn
SPAWN_PROTO_DIR=$(SPAWN_DIR)/protos
SPAWN_PROTO_EXT_DIR=$(SPAWN_PROTO_DIR)/eigr/functions/protocol/actors
GOOGLE_API_DIR=$(SPAWN_PROTO_DIR)/google/api
GOOGLE_PROTOBUF_DIR=$(SPAWN_PROTO_DIR)/google/protobuf
GRPC_DIR=$(SPAWN_PROTO_DIR)/grpc/reflection/v1alpha
EXAMPLES_PROTO_DIR=$(ROOT_DIR)/examples/protos
SPAWN_PROTO_OUT_DIR=$(SPAWN_DIR)
EXAMPLES_PROTO_OUT_DIR=$(ROOT_DIR)/examples

IMAGE_NAME = eigr/spawn-example:latest
DOCKERFILE = Dockerfile

GO_CMD=go
GO_FLAGS=-v
GO_BUILD_FLAGS_DEBUG=-gcflags="all=-N -l"

# Diretório de saída para binários
BIN_DIR=./bin
EXAMPLES_BIN=$(BIN_DIR)/examples/app
EXAMPLES_DIR=./examples

PROTOC=protoc
PROTOC_GEN_GO=$(shell go env GOPATH)/bin/protoc-gen-go

# Auxiliary variables
SPAWN_PROTO_FILES=$(wildcard $(SPAWN_PROTO_EXT_DIR)/*.proto)
EXAMPLES_PROTO_FILES=$(wildcard $(EXAMPLES_PROTO_DIR)/*.proto)

# Check if the Protobuf plugin is installed
$(PROTOC_GEN_GO):
@echo "Instalando protoc-gen-go..."
@go install google.golang.org/protobuf/cmd/protoc-gen-go@latest

# Protobuf code generation for spawn
.PHONY: proto-spawn
proto-spawn: $(PROTOC_GEN_GO)
@echo "Generating Go code from spawn Protobuf files..."
@$(PROTOC) \
--proto_path=$(SPAWN_PROTO_DIR) \
--proto_path=$(GOOGLE_API_DIR) \
--proto_path=$(GOOGLE_PROTOBUF_DIR) \
--proto_path=$(GRPC_DIR) \
--go_out=$(SPAWN_PROTO_OUT_DIR) \
--go_opt=paths=source_relative \
$(SPAWN_PROTO_FILES)

# Protobuf code generation for examples
.PHONY: proto-examples
proto-examples: $(PROTOC_GEN_GO)
@echo "Generating Go code from the examples' Protobuf files..."
@$(PROTOC) \
--proto_path=$(EXAMPLES_PROTO_DIR) \
--proto_path=$(GOOGLE_API_DIR) \
--proto_path=$(GOOGLE_PROTOBUF_DIR) \
--go_out=$(EXAMPLES_PROTO_OUT_DIR) \
--go_opt=paths=source_relative \
$(EXAMPLES_PROTO_FILES)

# Protobuf code generation for the entire project
.PHONY: proto
proto: proto-spawn proto-examples

# Spawn directory setup
.PHONY: setup-spawn
setup-spawn:
@echo "Setting dependencies for the spawn directory..."
@cd spawn && go mod tidy

# Examples directory setup
.PHONY: setup-examples
setup-examples:
@echo "Setting dependencies for the examples directory..."
@cd examples && go mod tidy

# Complete project setup
.PHONY: setup
setup: setup-spawn setup-examples

# Cleaning up files generated at spawn
.PHONY: clean-spawn
clean-spawn:
@echo "Removing files generated by Protobuf on spawn..."
@rm -f $(SPAWN_PROTO_OUT_DIR)/*.pb.go

# Cleaning up files generated in the examples
.PHONY: clean-examples
clean-examples:
@echo "Removing Protobuf generated files in the examples..."
@rm -f $(EXAMPLES_PROTO_OUT_DIR)/*.pb.go

# Cleaning the entire project
.PHONY: clean
clean: clean-spawn clean-examples

# Bbuild for the examples directory in development mode (debug)
build-debug:
@echo "Compiling the examples directory in debug mode..."
$(GO_CMD) build $(GO_FLAGS) $(GO_BUILD_FLAGS_DEBUG) -o $(EXAMPLES_BIN) $(EXAMPLES_DIR)
@echo "Build de debug finalizado em $(EXAMPLES_BIN)"

# Standard task to compile the examples
build: build-debug

# Task to run the generated binary
run:
@echo "Running the example binary..."
$(EXAMPLES_BIN)

# Task to compile the Dockerfile and generate the final image
docker-build:
docker build -t $(IMAGE_NAME) -f $(DOCKERFILE) .

# Task to run the binary inside the container
docker-run:
docker run --rm $(IMAGE_NAME)
Binary file added bin/examples/app
Binary file not shown.
Loading

0 comments on commit c5e2026

Please sign in to comment.