diff --git a/Makefile b/Makefile index e9814200..4aa2250a 100644 --- a/Makefile +++ b/Makefile @@ -1,63 +1,66 @@ -.PHONY: dev dev-down build test clean web-install dev-server dev-web build-web test-web typecheck-web lint-web generate-api prod-up prod-down +.PHONY: help dev dev-down build test clean web-install dev-server dev-web build-web test-web typecheck-web lint-web generate-api prod-up prod-down logs db-reset -# 启动本地开发环境(仅依赖服务) -dev: +help: ## 显示帮助 + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \ + awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' + +dev: ## 启动本地开发环境(仅依赖服务) docker compose up -d @echo "Waiting for services to be healthy..." @sleep 5 @echo "Services ready. Start backend with: make dev-server" @echo "Start frontend with: make dev-web" -dev-server: +dev-server: ## 启动后端开发服务器 cd server && ./mvnw spring-boot:run -Dspring-boot.run.profiles=local -# 停止本地开发环境 -dev-down: +dev-down: ## 停止本地开发环境 docker compose down -# 构建后端 -build: +build: ## 构建后端 cd server && ./mvnw clean package -DskipTests -# 运行测试 -test: +test: ## 运行后端测试 cd server && ./mvnw test -# 清理构建产物 -clean: +clean: ## 清理构建产物 cd server && ./mvnw clean docker compose down -v -# 生成 OpenAPI 类型(前端用) -generate-api: +generate-api: ## 生成 OpenAPI 类型(前端用) @echo "Generating OpenAPI types..." cd web && pnpm run generate-api -web-install: +web-install: ## 安装前端依赖 cd web && pnpm install -# 前端开发服务器 -dev-web: +dev-web: ## 启动前端开发服务器 cd web && pnpm run dev -# 构建前端 -build-web: +build-web: ## 构建前端 cd web && pnpm run build -# 前端测试 -test-web: +test-web: ## 运行前端测试 cd web && pnpm run test -# 前端类型检查 -typecheck-web: +typecheck-web: ## 前端类型检查 cd web && pnpm run typecheck -# 前端代码检查 -lint-web: +lint-web: ## 前端代码检查 cd web && pnpm run lint -prod-up: +prod-up: ## 一键启动全部服务(生产模式) docker compose -f docker-compose.prod.yml up -d --build -prod-down: +prod-down: ## 停止全部服务(生产模式) docker compose -f docker-compose.prod.yml down + +logs: ## 查看后端服务日志 + docker compose -f docker-compose.prod.yml logs -f server + +db-reset: ## 重置数据库 + docker compose down -v + docker compose up -d postgres + @echo "Waiting for postgres..." + @sleep 3 + cd server && ./mvnw flyway:migrate -pl skillhub-app diff --git a/server/.dockerignore b/server/.dockerignore new file mode 100644 index 00000000..b7da8964 --- /dev/null +++ b/server/.dockerignore @@ -0,0 +1,5 @@ +**/target/ +**/.idea/ +**/*.iml +.git/ +*.md diff --git a/server/Dockerfile b/server/Dockerfile index d55fc51b..dbac3589 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,14 +1,34 @@ +# ---- Build Stage ---- FROM eclipse-temurin:21-jdk-alpine AS build WORKDIR /app + +# Cache dependencies +COPY pom.xml . +COPY skillhub-domain/pom.xml skillhub-domain/ +COPY skillhub-auth/pom.xml skillhub-auth/ +COPY skillhub-search/pom.xml skillhub-search/ +COPY skillhub-infra/pom.xml skillhub-infra/ +COPY skillhub-storage/pom.xml skillhub-storage/ +COPY skillhub-app/pom.xml skillhub-app/ +COPY mvnw . +COPY .mvn .mvn +RUN ./mvnw dependency:go-offline -B + COPY . . RUN ./mvnw package -DskipTests -B +# ---- Runtime Stage ---- FROM eclipse-temurin:21-jre-alpine -WORKDIR /app -COPY --from=build /app/skillhub-app/target/*.jar app.jar RUN addgroup -S app && adduser -S app -G app +WORKDIR /app + +COPY --from=build /app/skillhub-app/target/*.jar app.jar + +RUN chown -R app:app /app USER app + EXPOSE 8080 HEALTHCHECK --interval=10s --timeout=3s \ CMD wget -qO- http://localhost:8080/actuator/health || exit 1 + ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75.0", "-jar", "app.jar"] diff --git a/server/skillhub-app/src/main/resources/application.yml b/server/skillhub-app/src/main/resources/application.yml index 6dc1aa03..a9e25284 100644 --- a/server/skillhub-app/src/main/resources/application.yml +++ b/server/skillhub-app/src/main/resources/application.yml @@ -71,3 +71,18 @@ management: endpoint: health: show-details: when-authorized + +--- +# Docker profile +spring: + config: + activate: + on-profile: docker + datasource: + url: jdbc:postgresql://${DB_HOST:postgres}:${DB_PORT:5432}/${DB_NAME:skillhub} + username: ${DB_USER:skillhub} + password: ${DB_PASS:skillhub_dev} + data: + redis: + host: ${REDIS_HOST:redis} + port: ${REDIS_PORT:6379} diff --git a/web/.dockerignore b/web/.dockerignore new file mode 100644 index 00000000..5f44e23a --- /dev/null +++ b/web/.dockerignore @@ -0,0 +1,4 @@ +node_modules/ +dist/ +.git/ +*.md