Skip to content

Commit c4f0ab1

Browse files
authored
feat(cli): add myfy start (#7)
1 parent 26dfb29 commit c4f0ab1

File tree

7 files changed

+500
-3
lines changed

7 files changed

+500
-3
lines changed

docs/api-reference/cli.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The `myfy` CLI provides the following commands:
1010

1111
### run
1212

13-
Start the development server.
13+
Start the development server with auto-reload enabled.
1414

1515
```bash
1616
myfy run [OPTIONS]
@@ -22,6 +22,48 @@ myfy run [OPTIONS]
2222
- `--reload / --no-reload`: Enable auto-reload (default: True)
2323
- `--app-path TEXT`: Path to app (e.g., main:app)
2424

25+
**Example:**
26+
```bash
27+
myfy run --host 0.0.0.0 --port 8080
28+
```
29+
30+
### start
31+
32+
Start the production server with optimizations enabled.
33+
34+
```bash
35+
myfy start [OPTIONS]
36+
```
37+
38+
**Options:**
39+
- `--host TEXT`: Server host (default: 127.0.0.1)
40+
- `--port INTEGER`: Server port (default: 8000)
41+
- `--workers INTEGER`: Number of worker processes (default: 1)
42+
- `--app-path TEXT`: Path to app (e.g., main:app)
43+
44+
**Features:**
45+
- Automatically sets `MYFY_FRONTEND_ENVIRONMENT=production`
46+
- Disables auto-reload for better performance
47+
- Verifies frontend assets are built (if FrontendModule is loaded)
48+
- Supports multiple worker processes via gunicorn
49+
50+
**Example:**
51+
```bash
52+
# Single worker
53+
myfy frontend build
54+
myfy start --host 0.0.0.0 --port 8000
55+
56+
# Multiple workers for better performance
57+
myfy start --host 0.0.0.0 --port 8000 --workers 4
58+
```
59+
60+
**Note:** For multi-worker support, install gunicorn:
61+
```bash
62+
pip install gunicorn
63+
# or
64+
uv add gunicorn
65+
```
66+
2567
### routes
2668

2769
List all registered routes.

docs/deployment.md

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
# Deployment Guide
2+
3+
This guide covers how to deploy myfy applications to production environments.
4+
5+
---
6+
7+
## Quick Start
8+
9+
For a basic production deployment:
10+
11+
```bash
12+
# 1. Build frontend assets (if using FrontendModule)
13+
myfy frontend build
14+
15+
# 2. Start production server
16+
myfy start --host 0.0.0.0 --port 8000 --workers 4
17+
```
18+
19+
---
20+
21+
## Production Server
22+
23+
### myfy start
24+
25+
The `myfy start` command is optimized for production deployments:
26+
27+
- **Automatic production mode**: Sets `MYFY_FRONTEND_ENVIRONMENT=production`
28+
- **No auto-reload**: Better performance and stability
29+
- **Asset verification**: Ensures frontend assets are built before starting
30+
- **Multi-worker support**: Uses gunicorn for horizontal scaling
31+
32+
### Single Worker
33+
34+
For simple deployments or development-like production:
35+
36+
```bash
37+
myfy start --host 0.0.0.0 --port 8000
38+
```
39+
40+
This uses uvicorn with a single worker process.
41+
42+
### Multiple Workers
43+
44+
For better performance and concurrency:
45+
46+
```bash
47+
myfy start --host 0.0.0.0 --port 8000 --workers 4
48+
```
49+
50+
This uses gunicorn with multiple uvicorn workers. Install gunicorn first:
51+
52+
```bash
53+
pip install gunicorn
54+
# or
55+
uv add gunicorn
56+
```
57+
58+
**Worker count recommendations:**
59+
- CPU-bound apps: `(2 × CPU cores) + 1`
60+
- I/O-bound apps: `(4 × CPU cores) + 1`
61+
- Start with 4 workers and adjust based on monitoring
62+
63+
---
64+
65+
## Frontend Assets
66+
67+
If your application uses `FrontendModule`, you must build assets before deploying:
68+
69+
```bash
70+
myfy frontend build
71+
```
72+
73+
This generates:
74+
- Minified JavaScript bundles
75+
- Minified CSS files
76+
- Asset manifest with content hashes
77+
- Files in `frontend/static/dist/`
78+
79+
**Important:** The `myfy start` command will verify that assets are built and fail if missing.
80+
81+
---
82+
83+
## Environment Variables
84+
85+
### Core Settings
86+
87+
```bash
88+
# Frontend environment (auto-set by myfy start)
89+
MYFY_FRONTEND_ENVIRONMENT=production
90+
91+
# Web server settings
92+
MYFY_WEB_HOST=0.0.0.0
93+
MYFY_WEB_PORT=8000
94+
```
95+
96+
### Frontend Settings
97+
98+
```bash
99+
# Static file serving
100+
MYFY_FRONTEND_STATIC_URL_PREFIX=/static
101+
102+
# Cache control
103+
MYFY_FRONTEND_CACHE_MAX_AGE=31536000 # 1 year default
104+
```
105+
106+
See [Configuration Reference](./api-reference/core.md) for all available settings.
107+
108+
---
109+
110+
## Deployment Options
111+
112+
### Docker
113+
114+
Create a `Dockerfile`:
115+
116+
```dockerfile
117+
FROM python:3.12-slim
118+
119+
WORKDIR /app
120+
121+
# Install system dependencies
122+
RUN apt-get update && apt-get install -y \
123+
nodejs npm \
124+
&& rm -rf /var/lib/apt/lists/*
125+
126+
# Copy application code
127+
COPY . .
128+
129+
# Install Python dependencies
130+
RUN pip install -e .
131+
RUN pip install gunicorn
132+
133+
# Build frontend assets
134+
RUN myfy frontend build
135+
136+
# Expose port
137+
EXPOSE 8000
138+
139+
# Start production server
140+
CMD ["myfy", "start", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]
141+
```
142+
143+
Build and run:
144+
145+
```bash
146+
docker build -t myfy-app .
147+
docker run -p 8000:8000 myfy-app
148+
```
149+
150+
### Docker Compose
151+
152+
Create a `docker-compose.yml`:
153+
154+
```yaml
155+
version: '3.8'
156+
157+
services:
158+
web:
159+
build: .
160+
ports:
161+
- "8000:8000"
162+
environment:
163+
- MYFY_FRONTEND_ENVIRONMENT=production
164+
restart: unless-stopped
165+
```
166+
167+
Run:
168+
169+
```bash
170+
docker-compose up -d
171+
```
172+
173+
174+
## Health Checks
175+
176+
Add a health check endpoint to your application:
177+
178+
```python
179+
from myfy.web import route
180+
181+
@route.get("/health")
182+
async def health_check():
183+
return {"status": "healthy"}
184+
```
185+
186+
Configure your load balancer or orchestrator to check this endpoint.
187+
188+
---
189+
190+
## Monitoring
191+
192+
### Logging
193+
194+
myfy uses Python's standard logging. Configure it in your application:
195+
196+
```python
197+
import logging
198+
199+
logging.basicConfig(
200+
level=logging.INFO,
201+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
202+
)
203+
```
204+
---
205+
206+
## Troubleshooting
207+
208+
### Frontend assets not found
209+
210+
**Error:** "Frontend assets not built"
211+
212+
**Solution:** Run `myfy frontend build` before starting the server.
213+
214+
### Gunicorn not found
215+
216+
**Error:** "gunicorn not installed"
217+
218+
**Solution:** Install gunicorn:
219+
```bash
220+
pip install gunicorn
221+
```
222+
223+
### Port already in use
224+
225+
**Error:** "Address already in use"
226+
227+
**Solution:** Change the port or stop the conflicting service:
228+
```bash
229+
myfy start --port 8080
230+
```
231+
232+
### Worker timeout
233+
234+
If workers are timing out, increase the timeout:
235+
```bash
236+
gunicorn app:app \
237+
--worker-class uvicorn.workers.UvicornWorker \
238+
--timeout 120
239+
```
240+
241+
---
242+
243+
## Next Steps
244+
245+
- [CLI Reference](./api-reference/cli.md) - Full CLI command reference
246+
- [Configuration](./api-reference/core.md) - All configuration options
247+
- [Frontend Module](./modules/frontend.md) - Frontend integration guide

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ nav:
103103
- Modules: core-concepts/modules.md
104104
- Configuration: core-concepts/configuration.md
105105
- Lifecycle: core-concepts/lifecycle.md
106+
- Deployment: deployment.md
106107
- API Reference:
107108
- Core: api-reference/core.md
108109
- Web: api-reference/web.md

0 commit comments

Comments
 (0)