Skip to content

Commit e4e762a

Browse files
committed
Add compilation file
1 parent ebf417f commit e4e762a

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Makefile

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# App name
2+
APP_NAME ?= go-api
3+
4+
# Timezone
5+
TZ ?= Asia/Shanghai
6+
7+
# Docker image name
8+
IMAGE_NAME ?= $(APP_NAME):latest
9+
10+
# Configuration directory
11+
CONFIG_DIR ?= $(shell pwd)/bin/configs
12+
13+
# Go build flags
14+
GO_FLAGS = -ldflags="-s -w"
15+
16+
# Run environment
17+
RUN_ENV ?= local
18+
19+
# Targets
20+
.PHONY: all test build run docker-build docker-run clean
21+
22+
# Default target that includes formatting, linting, testing, and building
23+
all: fmt test build
24+
25+
# Format the source code
26+
fmt:
27+
@echo "Running gofmt..."
28+
@gofmt -w . # Format all Go files in the current directory
29+
@echo "Running goimports..."
30+
@goimports -w . # Run goimports to organize imports
31+
32+
# Run tests
33+
test:
34+
@echo "Running tests..."
35+
@go test -v ./... # Run tests with verbose output
36+
37+
# Build the executable
38+
build: fmt
39+
@echo "Building binary..."
40+
@mkdir -p ./bin # Ensure the bin directory exists
41+
@go build $(GO_FLAGS) -o ./bin/$(APP_NAME) ./main.go # Build the Go binary
42+
43+
# Run the application
44+
run:
45+
@echo "Running application..."
46+
@./bin/$(APP_NAME) # Run the compiled binary
47+
48+
# Build the Docker image
49+
docker-build:
50+
@echo "Building Docker image..."
51+
@docker build --build-arg TZ=$(TZ) -t $(IMAGE_NAME) .
52+
53+
# Run the Docker container
54+
docker-run: docker-clean
55+
@echo "Running Docker container..."
56+
@docker run -d --name $(APP_NAME) \
57+
-p 8080:8080 \
58+
-it \
59+
-v $(CONFIG_DIR):/bin/configs \
60+
-e APP_NAME=$(APP_NAME) \
61+
-e RUN_ENV=$(RUN_ENV) \
62+
--restart always \
63+
$(IMAGE_NAME)
64+
65+
# Stop and remove existing Docker container with the same name
66+
docker-clean:
67+
@echo "Stopping and removing existing Docker container..."
68+
@docker stop $(APP_NAME) 2>/dev/null || true
69+
@docker rm -f $(APP_NAME) 2>/dev/null || true
70+
71+
# Clean up build artifacts
72+
clean:
73+
@echo "Cleaning up..."
74+
@rm -rf ./bin/$(APP_NAME) # Remove the bin directory

0 commit comments

Comments
 (0)