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