static-hosting

This commit is contained in:
朱潮 2026-05-18 22:17:40 +08:00
parent add8a1fd18
commit 26f244183c
2 changed files with 82 additions and 0 deletions

View File

@ -186,6 +186,9 @@ init_with_fastapi(app)
# Mount the public directory as static files
app.mount("/public", StaticFiles(directory="public"), name="static")
# Mount robot projects directory as static files (supports HTML/CSS/JS/images)
app.mount("/robots", StaticFiles(directory="projects/robot", html=True), name="robots")
# Add CORS middleware for frontend pages
app.add_middleware(
CORSMiddleware,

View File

@ -0,0 +1,79 @@
---
name: static-hosting
description: Serve static HTML/CSS/JS/images from robot project directories via the built-in FastAPI static file server. Use when generating web pages, reports, or interactive content for a bot.
---
# Static Hosting
Provide external access to static files (HTML, CSS, JS, images, fonts, etc.) stored under each robot's project directory.
## Use when
- Generate HTML pages, reports, or dashboards for a bot and need a URL to access them
- Create interactive web content (with CSS/JS) that users can open in a browser
- Need to host images or other assets referenced by bot-generated HTML
## How it works
FastAPI mounts `projects/robot/` at the `/robots` URL path with `html=True` enabled.
```
/app/projects/robot/{bot_id}/ --> {FASTAPI_URL}/robots/{bot_id}/
/app/projects/robot/{bot_id}/foo.html --> {FASTAPI_URL}/robots/{bot_id}/foo.html
```
`FASTAPI_URL` : `http://127.0.0.1:8001`.
## URL rules
| Local path | External URL | Notes |
|---|---|---|
| `/app/projects/robot/{bot_id}/index.html` | `/robots/{bot_id}/` | `html=True` auto-resolves `index.html` |
| `/app/projects/robot/{bot_id}/page.html` | `/robots/{bot_id}/page.html` | Direct HTML access |
| `/app/projects/robot/{bot_id}/css/style.css` | `/robots/{bot_id}/css/style.css` | CSS files |
| `/app/projects/robot/{bot_id}/js/app.js` | `/robots/{bot_id}/js/app.js` | JavaScript files |
| `/app/projects/robot/{bot_id}/images/logo.png` | `/robots/{bot_id}/images/logo.png` | Images (png/jpg/svg/etc.) |
| `/app/projects/robot/{bot_id}/fonts/custom.woff2` | `/robots/{bot_id}/fonts/custom.woff2` | Font files |
## Recommended directory structure
```
/app/projects/robot/{bot_id}/
├── index.html # Entry page (auto-served at /robots/{bot_id}/)
├── css/
│ └── style.css
├── js/
│ └── app.js
├── images/
│ └── logo.png
└── fonts/
└── custom.woff2
```
## HTML referencing rules
Inside HTML files, use **relative paths** to reference local assets:
```html
<!-- In projects/robot/{bot_id}/index.html -->
<link rel="stylesheet" href="css/style.css">
<script src="js/app.js"></script>
<img src="images/logo.png" alt="logo">
```
Do NOT use absolute paths like `/css/style.css` — this would resolve to the server root, not the bot directory.
## Quick example
To generate and serve a simple page for bot `63069654-7750-409d-9a58-a0960d899a20`:
1. Write HTML to `/app/projects/robot/63069654-7750-409d-9a58-a0960d899a20/index.html`
2. Access at `{FASTAPI_URL}/robots/63069654-7750-409d-9a58-a0960d899a20/`
## Notes
- Content-Type is auto-detected from file extension
- CORS is already configured (allow all origins) so frontend JS can fetch APIs on the same server
- Subdirectories of any depth are supported
- No authentication — all files under `/robots/` are publicly accessible
- The mount is defined in `fastapi_app.py`