From f171daa81a659abc9d85178249be8c0407121416 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 20 Feb 2024 09:51:50 +0800 Subject: [PATCH] feat: implement example-go service with Docker support - Add a new Makefile with build, docker-build, test, and clean targets, including cross-compilation support for Linux AMD64 and ARM64. - Introduce a new Go executable `example-go` with a basic HTTP server setup. - Create a new Dockerfile for an Alpine-based container including the `example-go` binary, with metadata labels and an entrypoint. Signed-off-by: Bo-Yi Wu --- Makefile | 87 ++++++++++++++++++++++++++++++++++++++++++ cmd/example-go/main.go | 13 +++++++ docker/Dockerfile | 21 ++++++++++ 3 files changed, 121 insertions(+) create mode 100644 Makefile create mode 100644 cmd/example-go/main.go create mode 100644 docker/Dockerfile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e496d24 --- /dev/null +++ b/Makefile @@ -0,0 +1,87 @@ +EXECUTABLE ?= example-go +SOURCES ?= $(shell find . -name "*.go" -type f) +DIST := dist +DIST_DIRS := $(DIST)/binaries $(DIST)/release +GO ?= go +TAGS ?= +HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" ) + +ifneq ($(shell uname), Darwin) + EXTLDFLAGS = -extldflags "-static" $(null) +else + EXTLDFLAGS = +endif + +ifeq ($(HAS_GO), GO) + GOPATH ?= $(shell $(GO) env GOPATH) + export PATH := $(GOPATH)/bin:$(PATH) + + CGO_EXTRA_CFLAGS := -DSQLITE_MAX_VARIABLE_NUMBER=32766 + CGO_CFLAGS ?= $(shell $(GO) env CGO_CFLAGS) $(CGO_EXTRA_CFLAGS) +endif + +ifeq ($(OS), Windows_NT) + GOFLAGS := -v -buildmode=exe + EXECUTABLE ?= $(EXECUTABLE).exe +else ifeq ($(OS), Windows) + GOFLAGS := -v -buildmode=exe + EXECUTABLE ?= $(EXECUTABLE).exe +else + GOFLAGS := -v + EXECUTABLE ?= $(EXECUTABLE) +endif + +STORED_VERSION_FILE := VERSION + +ifneq ($(DRONE_TAG),) + VERSION ?= $(subst v,,$(DRONE_TAG)) + RELASE_VERSION ?= $(VERSION) +else + ifneq ($(DRONE_BRANCH),) + VERSION ?= $(subst release/v,,$(DRONE_BRANCH)) + else + VERSION ?= main + endif + + STORED_VERSION=$(shell cat $(STORED_VERSION_FILE) 2>/dev/null) + ifneq ($(STORED_VERSION),) + RELASE_VERSION ?= $(STORED_VERSION) + else + RELASE_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//') + endif +endif + +LDFLAGS ?= -X 'main.Version=$(RELASE_VERSION)' + +build: $(EXECUTABLE) + +$(EXECUTABLE): $(SOURCES) + $(GO) build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o bin/$@ ./cmd/$(EXECUTABLE) + +docker-build: docker-build-linux-amd64 docker-build-linux-arm64 + +docker-build-linux-amd64: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/amd64/$(EXECUTABLE) ./cmd/$(EXECUTABLE) +docker-build-linux-arm64: + CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GO) build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/arm64/$(EXECUTABLE) ./cmd/$(EXECUTABLE) + +test: + @$(GO) test -cover -tags '$(TAGS)' ./... && echo "\n==>\033[32m Ok\033[m\n" || exit 1 + +# install air command +.PHONY: air +air: + @hash air > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) install github.com/cosmtrek/air@latest; \ + fi + +# run air +.PHONY: dev +dev: air + air --build.cmd "make build" --build.bin bin/$(EXECUTABLE) + +.PHONY: release-clean +release-clean: + rm -rf $(DIST) + +clean: release-clean diff --git a/cmd/example-go/main.go b/cmd/example-go/main.go new file mode 100644 index 0000000..6965235 --- /dev/null +++ b/cmd/example-go/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "gitea.com/action-examples/go/router" +) + +func main() { + r := router.New() + // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") + if err := r.Run(); err != nil { + panic(err) + } +} diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..729ea27 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,21 @@ +FROM alpine:3.19 + +ARG TARGETOS +ARG TARGETARCH + +LABEL maintainer="Bo-Yi Wu " \ + org.label-schema.name="api" \ + org.label-schema.vendor="Bo-Yi Wu" \ + org.label-schema.schema-version="1.0" \ + com.centurylinklabs.watchtower.stop-signal="SIGINT" \ + io.containers.autoupdate="registry" + +RUN apk update && apk add --no-cache ca-certificates + +EXPOSE 8080 + +ENV GODEBUG netdns=go + +COPY release/${TARGETOS}/${TARGETARCH}/example-go /bin/example-go + +ENTRYPOINT ["/bin/example-go"]