From c1e4c7ae0596a600f4cf40678979515acceb8f42 Mon Sep 17 00:00:00 2001 From: chengqian Date: Mon, 11 May 2026 17:54:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AE=8C=E5=96=84make=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81windows=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 31 +++++++++++++++++++++++++------ README.md | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 3fcad6e..b7407d8 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,18 @@ ROOT_DIR = $(shell pwd) DEPLOY_NAME = "team-api" DOCKER_NAME = "team-api" VERSION ?= $(shell cat VERSION 2>/dev/null | tr -d '[:space:]') +LDFLAGS = -X github.com/qianfree/team-api/internal/consts.Version=$(VERSION) + +# Cross-compile: make build GOOS=windows GOARCH=amd64 +GOOS ?= $(shell go env GOOS) +GOARCH ?= $(shell go env GOARCH) +export GOOS GOARCH + +ifeq ($(GOOS),windows) + BINARY = team-api.exe +else + BINARY = team-api +endif # DB_URL 设置方式(三选一): # 1. 创建 .env 文件(推荐): DB_URL = "host=... port=... user=... password=... dbname=... sslmode=disable" @@ -11,10 +23,7 @@ ifneq (,$(wildcard .env)) include .env endif -.PHONY: run build tidy migrate-up migrate-down migrate-status migrate-reset build-web build-all - -include ./hack/hack-cli.mk -include ./hack/hack.mk +.PHONY: run build build-web build-all tidy ctrl dao service migrate-up migrate-down migrate-status migrate-reset # GoFrame hot-reload dev server run: @@ -22,7 +31,7 @@ run: # Build production binary (backend only, no frontend embedded) build: - go build -ldflags "-X github.com/qianfree/team-api/internal/consts.Version=$(VERSION)" -o ./tmp/team-api main.go + go build -ldflags "$(LDFLAGS)" -o ./$(BINARY) main.go # Build frontend assets build-web: @@ -31,12 +40,22 @@ build-web: # Build all (frontend embedded into backend binary) build-all: build-web - go build -tags embedweb -ldflags "-X github.com/qianfree/team-api/internal/consts.Version=$(VERSION)" -o ./tmp/team-api main.go + go build -tags embedweb -ldflags "$(LDFLAGS)" -o ./$(BINARY) main.go # Tidy go modules tidy: go mod tidy +# GoFrame code generation +ctrl: + gf gen ctrl + +dao: + gf gen dao + +service: + gf gen service + # Goose migration commands migrate-up: goose -dir migrations postgres $(DB_URL) up diff --git a/README.md b/README.md index 3e47f30..0487282 100644 --- a/README.md +++ b/README.md @@ -195,14 +195,39 @@ bun dev ### 7. 生产构建 ```bash -# 构建后端二进制文件 +# 仅构建后端(前端由 Nginx/CDN 独立托管) make build -# 构建前端 -cd web/admin && bun run build -cd web/tenant && bun run build +# 前后端一体(前端嵌入二进制,单文件部署) +make build-all ``` +#### 交叉编译 + +通过 `GOOS` 和 `GOARCH` 参数指定目标平台,在任意系统上构建其他平台的二进制: + +```bash +# Linux x86_64 +make build GOOS=linux GOARCH=amd64 + +# Linux ARM64(树莓派、ARM 服务器) +make build GOOS=linux GOARCH=arm64 + +# macOS Apple Silicon +make build GOOS=darwin GOARCH=arm64 + +# macOS Intel +make build GOOS=darwin GOARCH=amd64 + +# Windows +make build GOOS=windows GOARCH=amd64 + +# 交叉编译 + 嵌入前端 +make build-all GOOS=linux GOARCH=amd64 +``` + +输出文件名自动适配:Windows 下为 `team-api.exe`,其他平台为 `team-api`。 + ## API 接口 ### 管理类接口 @@ -323,8 +348,13 @@ gf gen ctrl ```bash make run # 启动开发服务器(热编译) -make build # 构建生产二进制文件 +make build # 构建后端二进制(不含前端) +make build-web # 仅构建前端资源 +make build-all # 前后端一体构建(前端嵌入二进制) make tidy # 整理 Go 模块依赖 +make ctrl # 从 API 定义生成 Controller +make dao # 从数据库生成 DAO/DO/Entity +make service # 从 Logic 层生成 Service 接口 make migrate-up # 执行数据库迁移 make migrate-down # 回滚上一次迁移 make migrate-status # 查看迁移状态