diff --git a/code/common/db/options.go b/code/common/db/options.go new file mode 100644 index 0000000..e69de29 diff --git a/code/common/db/postgres.go b/code/common/db/postgres.go new file mode 100644 index 0000000..e69de29 diff --git a/code/common/go.mod b/code/common/go.mod new file mode 100644 index 0000000..7469f1f --- /dev/null +++ b/code/common/go.mod @@ -0,0 +1,11 @@ +module code/common + +go 1.25.7 + +require ( + github.com/gin-gonic/gin v1.9.1 + github.com/jmoiron/sqlx v1.3.5 + github.com/redis/go-redis/v9 v9.3.0 + go.uber.org/zap v1.26.0 + github.com/lib/pq v1.10.9 +) diff --git a/code/common/go.sum b/code/common/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/code/common/logger/logger.go b/code/common/logger/logger.go new file mode 100644 index 0000000..e69de29 diff --git a/code/common/middleware/cors.go b/code/common/middleware/cors.go new file mode 100644 index 0000000..e69de29 diff --git a/code/common/redis/redis.go b/code/common/redis/redis.go new file mode 100644 index 0000000..e69de29 diff --git a/code/common/utils/common.go b/code/common/utils/common.go new file mode 100644 index 0000000..e69de29 diff --git a/code/deploy/postgres/init.sql b/code/deploy/postgres/init.sql new file mode 100644 index 0000000..602418a --- /dev/null +++ b/code/deploy/postgres/init.sql @@ -0,0 +1,4 @@ +-- PostgresSQL全局初始化脚本 +-- 所有业务公共建库/建表语句写在这里,单独业务表建议在各自业务中处理 +CREATE DATABASE IF NOT EXISTS monorepo; +\c monorepo; diff --git a/code/deploy/redis/redis.conf b/code/deploy/redis/redis.conf new file mode 100644 index 0000000..5ecb4c1 --- /dev/null +++ b/code/deploy/redis/redis.conf @@ -0,0 +1,8 @@ +# Redis基础配置 +bind 0.0.0.0 +protected-mode no +port 6379 +daemonize no +requirepass 123456 +appendonly yes +appendfsync everysec diff --git a/code/docker-compose.yml b/code/docker-compose.yml new file mode 100644 index 0000000..3d70cab --- /dev/null +++ b/code/docker-compose.yml @@ -0,0 +1,68 @@ +version: '3.8' +services: + # 公共PostgresSQL + postgres: + image: postgres:18.1-alpine + container_name: trading-assistant-postgres + environment: + POSTGRES_USER: ${PG_USER:-root} + POSTGRES_PASSWORD: ${PG_PWD:-123456} + POSTGRES_DB: ${PG_DB:-monorepo} + ports: + - "${PG_PORT:-5432}:5432" + volumes: + - ./deploy/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql + - pg_data:/var/lib/postgresql/data + networks: + - monorepo-net + restart: always + + # 公共Redis + redis: + image: redis:8.4.-alpine + container_name: trading-assistant-redis + ports: + - "${REDIS_PORT:-6379}:6379" + volumes: + - ./deploy/redis/redis.conf:/etc/redis/redis.conf + - redis_data:/data + command: redis-server /etc/redis/redis.conf + networks: + - monorepo-net + restart: always + + # 【业务服务模板】新增业务时复制以下块修改 + # user: + # build: + # context: ./services/user + # dockerfile: Dockerfile + # container_name: trading-assistant-user + # environment: + # - GIN_MODE=release + # - PG_ADDR=postgres:5432 + # - REDIS_ADDR=redis:6379 + # ports: + # - "8080:8080" + # volumes: + # - ./logs/user:/app/logs + # networks: + # - monorepo-net + # restart: always + # depends_on: + # - postgres + # - redis + # deploy: + # resources: + # limits: + # cpus: "0.5" + # memory: "512M" + +# 全局网络 +networks: + monorepo-net: + driver: bridge + +# 全局数据卷 +volumes: + pg_data: + redis_data: diff --git a/init_monorepo.py b/init_bak/init_monorepo.py similarity index 94% rename from init_monorepo.py rename to init_bak/init_monorepo.py index f57e266..08a0791 100644 --- a/init_monorepo.py +++ b/init_bak/init_monorepo.py @@ -5,8 +5,8 @@ import sys from pathlib import Path # ===================== 可自定义配置(按需修改) ===================== -PROJECT_ROOT = "go-monorepo" # 项目根目录名 -GO_VERSION = "1.21" # 团队统一Go版本 +PROJECT_ROOT = "code" # 项目根目录名 +GO_VERSION = "1.25.7" # 团队统一Go版本 # ==================================================================== def create_dirs(base_path: Path): @@ -108,14 +108,15 @@ appendfsync everysec """ write_file(deploy_path / "redis/redis.conf", redis_conf) -def generate_docker_compose(base_path: Path): +# 【修复】增加project_name参数,接收外部传入的项目名 +def generate_docker_compose(base_path: Path, project_name: str): """生成根目录docker-compose.yml(聚合中间件+业务模板)""" compose_content = f"""version: '3.8' services: # 公共PostgresSQL postgres: image: postgres:16-alpine - container_name: {PROJECT_ROOT}-postgres + container_name: {project_name}-postgres environment: POSTGRES_USER: ${{PG_USER:-root}} POSTGRES_PASSWORD: ${{PG_PWD:-123456}} @@ -132,7 +133,7 @@ services: # 公共Redis redis: image: redis:7-alpine - container_name: {PROJECT_ROOT}-redis + container_name: {project_name}-redis ports: - "${{REDIS_PORT:-6379}}:6379" volumes: @@ -148,7 +149,7 @@ services: # build: # context: ./services/user # dockerfile: Dockerfile - # container_name: {PROJECT_ROOT}-user + # container_name: {project_name}-user # environment: # - GIN_MODE=release # - PG_ADDR=postgres:5432 @@ -275,8 +276,8 @@ def main(): generate_common_module(base_path) # 4. 生成部署资源文件 generate_deploy_files(base_path) - # 5. 生成Docker Compose - generate_docker_compose(base_path) + # 【修复】调用时传入PROJECT_ROOT作为project_name参数 + generate_docker_compose(base_path, PROJECT_ROOT) # 6. 生成全局配置(.env/Makefile) generate_global_config(base_path) diff --git a/init_monorepo.sh b/init_bak/init_monorepo.sh similarity index 99% rename from init_monorepo.sh rename to init_bak/init_monorepo.sh index 651b3f2..105a614 100644 --- a/init_monorepo.sh +++ b/init_bak/init_monorepo.sh @@ -4,7 +4,7 @@ set -euo pipefail export LANG=C.UTF-8 # 配置项(可按需修改) -PROJECT_NAME="go-monorepo" +PROJECT_NAME="code" PYTHON_IMAGE="python:3.11-alpine" # 轻量Python3镜像,仅80+MB PYTHON_SCRIPT="init_monorepo.py"