生成 自己喜欢 Fastapi 写法的文件和目录

4 阅读3分钟

CMD.bat

@echo off
setlocal enabledelayedexpansion

if not exist "data" (
    md data
    echo Created: data
) else (
    echo Skipped: data (already exists)
)

if not exist "static" (
    md static
    echo Created: static
) else (
    echo Skipped: static (already exists)
)

if not exist "uploads" (
    md uploads
    echo Created: uploads
) else (
    echo Skipped: uploads (already exists)
)

if not exist "backups" (
    md backups
    echo Created: backups
) else (
    echo Skipped: backups (already exists)
)

if not exist "docker-compose.yml" (
    (
        echo version: '3.8'
        echo.
        echo services:
        echo   my-app:
        echo     container_name: my-app-app
        echo     image: my-app-app:latest
        echo     build: .
        echo     ports:
        echo       - "8000:8000"
        echo     volumes:
        echo       - ./data:/app/data
        echo     restart: unless-stopped
    ) > docker-compose.yml
    echo Created: docker-compose.yml
) else (
    echo Skipped: docker-compose.yml (already exists)
)

if not exist "Dockerfile" (
    (
        echo FROM python:latest
        echo.
        echo WORKDIR /app
        echo.
        echo COPY requirements.txt .
        echo RUN pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
        echo.
        echo COPY . .
        echo EXPOSE 8000
        echo.
        echo CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
    ) > Dockerfile
    echo Created: Dockerfile
) else (
    echo Skipped: Dockerfile (already exists)
)

if not exist "index.html" (
    (
        echo ^<!DOCTYPE html^>
        echo ^<html lang="en"^>
        echo ^<head^>
        echo     ^<meta charset="UTF-8"^>
        echo     ^<meta name="viewport" content="width=device-width, initial-scale=1.0"^>
        echo     ^<title^>My App^</title^>
        echo ^</head^>
        echo ^<body^>
        echo     ^<h1^>Hello World My App^</h1^>
        echo ^</body^>
        echo ^</html^>
    ) > index.html
    echo Created: index.html
) else (
    echo Skipped: index.html (already exists)
)

if not exist "main.py" (
    (
        echo from fastapi import FastAPI
        echo from fastapi.staticfiles import StaticFiles
        echo from fastapi.responses import FileResponse
        echo.
        echo app = FastAPI^(^)
        echo.
        echo app.mount^("/static", StaticFiles^(directory="static"^), name="static"^)
        echo.
        echo @app.get^("/"^)
        echo async def root^(^):
        echo     return FileResponse^("index.html"^)
        echo.
        echo @app.get^("/api/hello"^)
        echo async def hello^(^):
        echo     return {"message": "Hello World"}
        echo.
        echo if __name__ == "__main__":
        echo     import uvicorn
        echo     uvicorn.run^(app, host="0.0.0.0", port=8000^)
    ) > main.py
    echo Created: main.py
) else (
    echo Skipped: main.py (already exists)
)

if not exist "requirements.txt" (
    (
        echo fastapi
        echo uvicorn
    ) > requirements.txt
    echo Created: requirements.txt
) else (
    echo Skipped: requirements.txt (already exists)
)

if not exist "README.md" (
    (
        echo # My App
        echo.
        echo ## Run
        echo.
        echo ```bash
        echo docker-compose up --build
        echo ```
        echo.
        echo ## Access
        echo.
        echo http://localhost:8000
        echo.
        echo ---
        echo.
        echo ## SKILL 调用方式
        echo.
        echo ### 1. 自动触发
        echo 当你的请求匹配 SKILL 的描述时,系统会自动提示调用。例如:
        echo - "创建一个 FastAPI 项目"
        echo - "初始化 FastAPI"
        echo - "帮我搭建 FastAPI 结构"
        echo.
        echo ### 2. 手动指定
        echo 你可以直接说要调用哪个 SKILL:
        echo - "调用 fastapi-init"
        echo - "使用 fastapi-init skill"
        echo.
        echo ### 3. 当前可用的 SKILL
        echo ```
        echo fastapi-init - 初始化 FastAPI 项目
        echo ```
    ) > README.md
    echo Created: README.md
) else (
    echo Skipped: README.md (already exists)
)

if not exist "cmd.txt" (
    (
        echo docker-compose up --build
        echo docker-compose down
        echo docker-compose logs -f
    ) > cmd.txt
    echo Created: cmd.txt
) else (
    echo Skipped: cmd.txt (already exists)
)

if not exist "run.bat" (
    (
        echo start http://localhost:8000
        echo python main.py
    ) > run.bat
    echo Created: run.bat
) else (
    echo Skipped: run.bat (already exists)
)

if not exist ".trae\skills\fastapi-init\SKILL.md" (
    if not exist ".trae\skills\fastapi-init" md .trae\skills\fastapi-init
    (
        echo ---
        echo name: "fastapi-init"
        echo description: "Initializes a FastAPI project with Docker support. Invoke when user wants to create a new FastAPI project or set up project structure."
        echo ---
        echo.
        echo # FastAPI Project Initializer
        echo.
        echo This skill creates a complete FastAPI project structure with Docker support.
        echo.
        echo ## Project Structure
        echo.
        echo ```
        echo project/
        echo +-- data/              # Data directory
        echo +-- static/            # Static files
        echo +-- uploads/           # Upload files
        echo +-- backups/           # Backup files
        echo +-- docker-compose.yml # Docker Compose config
        echo +-- Dockerfile         # Docker image config
        echo +-- index.html         # Frontend page
        echo +-- main.py            # FastAPI application
        echo +-- requirements.txt   # Python dependencies
        echo +-- README.md          # Project documentation
        echo +-- cmd.txt            # Common commands
        echo +-- run.bat            # Quick start script
        echo ```
        echo.
        echo ## Usage
        echo.
        echo 1. Create directories: data, static, uploads, backups
        echo 2. Create all files with default content
        echo 3. Run python main.py or run.bat to start server
        echo 4. Access http://localhost:8000
        echo.
        echo ## API Endpoints
        echo.
        echo - GET / - Returns index.html
        echo - GET /api/hello - Returns JSON message
        echo - GET /static/* - Serves static files
    ) > .trae\skills\fastapi-init\SKILL.md
    echo Created: .trae\skills\fastapi-init\SKILL.md
) else (
    echo Skipped: .trae\skills\fastapi-init\SKILL.md (already exists)
)

echo Done!
echo.
echo Starting server...
start http://localhost:8000
python main.py



SKILL.md


name: "fastapi-init" description: "Initializes a FastAPI project with Docker support. Invoke when user wants to create a new FastAPI project or set up project structure."

FastAPI Project Initializer

This skill creates a complete FastAPI project structure with Docker support.

Project Structure

project/
├── data/              # Data directory
├── static/            # Static files
├── uploads/           # Upload files
├── backups/           # Backup files
├── docker-compose.yml # Docker Compose config
├── Dockerfile         # Docker image config
├── index.html         # Frontend page
├── main.py            # FastAPI application
├── requirements.txt   # Python dependencies
├── README.md          # Project documentation
├── cmd.txt            # Common commands
└── run.bat            # Quick start script

Files Content

docker-compose.yml

version: '3.8'

services:
  my-app:
    container_name: my-app-app
    image: my-app-app:latest
    build: .
    ports:
      - "8000:8000"
    volumes:
      - ./data:/app/data
    restart: unless-stopped

Dockerfile

FROM python:latest

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

COPY . .
EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

main.py

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")

@app.get("/")
async def root():
    return FileResponse("index.html")

@app.get("/api/hello")
async def hello():
    return {"message": "Hello World"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My App</title>
</head>
<body>
    <h1>Hello World My App</h1>
</body>
</html>

requirements.txt

fastapi
uvicorn

README.md

# My App

## Run

\`\`\`bash
docker-compose up --build
\`\`\`

## Access

http://localhost:8000

cmd.txt

docker-compose up --build
docker-compose down
docker-compose logs -f

run.bat

start http://localhost:8000
python main.py

Usage

  1. Create directories: data, static, uploads, backups
  2. Create all files with default content
  3. Run python main.py or run.bat to start server
  4. Access http://localhost:8000

API Endpoints

  • GET / - Returns index.html
  • GET /api/hello - Returns JSON {"message": "Hello World"}
  • GET /static/* - Serves static files