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 <appleboy.tw@gmail.com>
This commit is contained in:
parent
9d1d40c08a
commit
f171daa81a
87
Makefile
Normal file
87
Makefile
Normal file
@ -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
|
13
cmd/example-go/main.go
Normal file
13
cmd/example-go/main.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
21
docker/Dockerfile
Normal file
21
docker/Dockerfile
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
FROM alpine:3.19
|
||||||
|
|
||||||
|
ARG TARGETOS
|
||||||
|
ARG TARGETARCH
|
||||||
|
|
||||||
|
LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||||
|
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"]
|
Loading…
x
Reference in New Issue
Block a user