Spaces:
Sleeping
Sleeping
File size: 5,654 Bytes
74de430 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# SwiftOps API - Templates Architecture
## ποΈ Architecture Overview
Following FastAPI best practices, we've separated concerns into distinct layers:
```
src/app/
βββ templates/ # Jinja2 HTML templates
β βββ base.html # Base template (layout)
β βββ index.html # Landing page
β βββ README.md # Template documentation
β
βββ static/ # Static assets
β βββ css/
β β βββ main.css # Styles (theme-aware)
β βββ js/
β βββ main.js # Client-side JavaScript
β
βββ api/v1/
β βββ pages.py # Page routes (HTML)
β βββ auth.py # API routes (JSON)
β βββ ... # Other API routes
β
βββ constants/
β βββ app_metadata.py # Centralized app config
β
βββ main.py # Application entry point
```
---
## π Design Principles
### 1. **Separation of Concerns**
- **Templates** (`templates/`) - HTML structure
- **Styles** (`static/css/`) - Visual presentation
- **Scripts** (`static/js/`) - Client behavior
- **Routes** (`api/v1/pages.py`) - Page logic
- **Config** (`constants/app_metadata.py`) - Metadata
### 2. **Template Inheritance**
```
base.html (layout)
β
index.html (content)
β
Future pages extend base.html
```
### 3. **Static File Serving**
```python
# Mounted in main.py
app.mount("/static", StaticFiles(directory="static"), name="static")
# Used in templates
<link rel="stylesheet" href="{{ url_for('static', path='/css/main.css') }}">
```
### 4. **Centralized Configuration**
```python
# constants/app_metadata.py
APP_NAME = "SwiftOps API"
APP_VERSION = "1.0.0"
# Used everywhere
from app.constants.app_metadata import APP_NAME, APP_VERSION
```
---
## π¨ Features
### Theme Support
- β
Automatic dark/light mode detection
- β
Respects system preferences
- β
Smooth transitions
- β
CSS custom properties (variables)
### Responsive Design
- β
Mobile-first approach
- β
Flexible grid layouts
- β
Touch-friendly interactions
- β
Breakpoints for all devices
### Performance
- β
Minimal CSS/JS
- β
No external dependencies
- β
Fast page loads
- β
Optimized assets
---
## π Adding New Pages
### Step 1: Create Template
```html
<!-- templates/about.html -->
{% extends "base.html" %}
{% block title %}About - {{ app_name }}{% endblock %}
{% block content %}
<div class="container">
<h1>About {{ app_name }}</h1>
<p>{{ app_description }}</p>
</div>
{% endblock %}
```
### Step 2: Add Route
```python
# api/v1/pages.py
@router.get("/about", response_class=HTMLResponse)
async def about(request: Request):
return templates.TemplateResponse("about.html", {
"request": request,
"app_name": APP_NAME,
"app_description": APP_DESCRIPTION
})
```
### Step 3: Update Navigation (if needed)
```html
<!-- templates/base.html -->
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
```
---
## π― Best Practices
### 1. **Keep Templates Simple**
- Logic in Python (routes)
- Presentation in HTML (templates)
- Styling in CSS (separate file)
### 2. **Use Template Inheritance**
```html
{% extends "base.html" %}
{% block content %}...{% endblock %}
```
### 3. **Centralize Configuration**
```python
# Don't hardcode
app_name = "SwiftOps API"
# Do this
from app.constants.app_metadata import APP_NAME
```
### 4. **Include Request Object**
```python
# Always pass request to templates
return templates.TemplateResponse("page.html", {
"request": request, # Required by Jinja2Templates
"data": data
})
```
### 5. **Use url_for for Static Files**
```html
<!-- Good -->
<link href="{{ url_for('static', path='/css/main.css') }}">
<!-- Avoid -->
<link href="/static/css/main.css">
```
---
## π§ Customization
### Update App Metadata
Edit `constants/app_metadata.py`:
```python
APP_NAME = "Your App Name"
APP_VERSION = "2.0.0"
APP_DESCRIPTION = "Your description"
```
### Add Custom Styles
Edit `static/css/main.css`:
```css
/* Add your custom styles */
.custom-class {
color: var(--accent-color);
}
```
### Add Custom JavaScript
Edit `static/js/main.js`:
```javascript
// Add your custom scripts
document.addEventListener('DOMContentLoaded', function() {
// Your code here
});
```
---
## π File Structure Benefits
### β
**Maintainability**
- Clear separation of concerns
- Easy to find and update files
- Consistent structure
### β
**Scalability**
- Add new pages easily
- Extend base template
- Reuse components
### β
**Testability**
- Routes are separate from templates
- Logic is in Python (testable)
- Templates are pure HTML
### β
**Performance**
- Static files cached by browser
- Minimal dependencies
- Fast rendering
---
## π Future Enhancements
### Planned Features
- [ ] Component library (reusable blocks)
- [ ] Admin dashboard template
- [ ] Error pages (404, 500)
- [ ] Email templates
- [ ] PDF generation templates
### Possible Additions
- [ ] Template fragments (HTMX support)
- [ ] Internationalization (i18n)
- [ ] Template caching
- [ ] Asset bundling/minification
---
## π Resources
- [FastAPI Templates](https://fastapi.tiangolo.com/advanced/templates/)
- [Jinja2 Documentation](https://jinja.palletsprojects.com/)
- [Static Files in FastAPI](https://fastapi.tiangolo.com/tutorial/static-files/)
- [CSS Custom Properties](https://developer.mozilla.org/en-US/docs/Web/CSS/--*)
---
**This architecture provides a solid foundation for building scalable, maintainable web pages in your FastAPI application!** π
|