9.3 KiB
Daily Journal Prompt Generator - Webapp Refactoring Plan
Overview
Refactor the existing Python CLI application into a modern web application with FastAPI backend and a lightweight frontend. The system will maintain all existing functionality while providing a web-based interface for easier access and better user experience.
Current Architecture Analysis
Existing CLI Application
- Language: Python 3.7+
- Core Dependencies: openai, python-dotenv, rich
- Data Storage: JSON files (
prompts_historic.json,prompts_pool.json) - Configuration:
.envfile for API keys,settings.cfgfor app settings - Functionality:
- AI-powered prompt generation using OpenAI-compatible APIs
- Smart repetition avoidance with 60-prompt history buffer
- Prompt pool system for offline usage
- Interactive CLI with rich formatting
Key Features to Preserve
- AI prompt generation with history awareness
- Prompt pool management (fill, draw, stats)
- Configuration via environment variables
- JSON-based data persistence
- All existing prompt generation logic As the user discards prompts, the themes will be very slowly steered, so it's okay to take some inspiration from the history.
Proposed Web Application Architecture
Backend: FastAPI
Rationale: FastAPI provides async capabilities, automatic OpenAPI documentation, and excellent performance. It's well-suited for AI API integrations.
Components:
-
API Endpoints:
GET /api/prompts/draw- Draw prompts from poolPOST /api/prompts/fill-pool- Fill prompt pool using AIGET /api/prompts/stats- Get pool and history statisticsGET /api/prompts/history- Get prompt historyPOST /api/prompts/select/{prompt_id}- Select a prompt for journaling
-
Core Services:
- PromptGeneratorService (adapted from existing logic)
- PromptPoolService (manages pool operations)
- HistoryService (manages 60-item cyclic buffer)
- AIClientService (OpenAI API integration)
-
Data Layer:
- Initial Approach: Keep JSON file storage (
prompts_historic.json,prompts_pool.json) - Docker Volume: Mount
./datadirectory to/app/datafor persistent JSON storage - Future Evolution: SQLite database migration path (optional later phase)
- Rationale: Maintains compatibility with existing CLI app, simple file-based persistence
- Initial Approach: Keep JSON file storage (
-
Configuration:
- Environment variables (API keys, settings)
- Pydantic models for validation
- Settings management with python-dotenv
Frontend Options Analysis
Option: Astro-erudite with React Components
Decision: Use astro-erudite (minimalist Astro flavor) with React components for interactive elements.
Rationale:
- astro-erudite: Minimalist flavor of Astro focused on simplicity and content-first approach
- React Components: Allows using React's rich component ecosystem for interactive elements
- Best of Both Worlds: Astro's performance with React's interactivity where needed
- Future Flexibility: Can add more React components as features expand
- Minimalist Philosophy: Aligns with the simple, focused nature of the prompt generator
Architecture:
- astro-erudite handles page routing and static content
- React components for interactive elements (prompt selection, admin controls)
- Partial hydration for optimal performance
- Minimal styling approach (Tailwind CSS optional, can use simple CSS)
Frontend Components:
- Prompt Display Component: Shows multiple prompts with selection
- Stats Dashboard: Shows pool/history statistics
- Admin Panel: Controls for filling pool, viewing history
- Responsive Design: Mobile-friendly interface
Docker & Docker Compose Setup
Multi-container Architecture
services:
backend:
build: ./backend
ports:
- "8000:8000"
volumes:
- ./backend:/app
- ./data:/app/data # For JSON file persistence
environment:
- DEEPSEEK_API_KEY=${DEEPSEEK_API_KEY}
- OPENAI_API_KEY=${OPENAI_API_KEY}
develop:
watch:
- action: sync
path: ./backend
target: /app
- action: rebuild
path: ./backend/requirements.txt
frontend:
build: ./frontend
ports:
- "3000:3000" # Development
- "80:80" # Production
volumes:
- ./frontend:/app
develop:
watch:
- action: sync
path: ./frontend/src
target: /app/src
- action: rebuild
path: ./frontend/package.json
Dockerfile Examples
Backend Dockerfile:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Frontend Dockerfile (Astro):
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json .
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Refactoring Strategy
Phase 1: Backend API Development
-
Setup FastAPI project structure
- Create
backend/directory - Set up virtual environment
- Install FastAPI, uvicorn, pydantic
- Create
-
Adapt existing Python logic
- Refactor
generate_prompts.pyinto services - Create API endpoints
- Add error handling and validation
- Refactor
-
Data persistence
- Keep JSON file storage initially
- Add file locking for concurrent access
- Plan SQLite migration
-
Testing
- Unit tests for services
- API integration tests
- Maintain existing test coverage
Phase 2: Frontend Development
-
Setup Astro project
- Create
frontend/directory - Initialize Astro project
- Install UI components (Tailwind CSS recommended)
- Create
-
Build UI components
- Prompt display and selection
- Statistics dashboard
- Admin controls
-
API integration
- Fetch data from FastAPI backend
- Handle user interactions
- Error states and loading indicators
Phase 3: Dockerization & Deployment
-
Docker configuration
- Create Dockerfiles for backend/frontend
- Create docker-compose.yml
- Configure development vs production builds
-
Environment setup
- Environment variable management
- Volume mounts for development
- Production optimization
-
Deployment preparation
- Health checks
- Logging configuration
- Monitoring setup
Technical Decisions
1. Authentication (Optional)
Current: None (single-user CLI) Webapp Option: Basic session-based auth or JWT Recommendation: Start without auth, add later if needed for multi-user
2. Data Storage Evolution
Phase 1: JSON files (maintain compatibility) Phase 2: SQLite with migration script Phase 3: Optional PostgreSQL for scalability
3. API Design Principles
- RESTful endpoints
- JSON responses
- Consistent error handling
- OpenAPI documentation
- Versioning (v1/ prefix)
4. Frontend State Management
Simple approach: React-like state with Astro components If complex: Consider lightweight state management (Zustand, Jotai) Initial: Component-level state sufficient
Development Workflow
Local Development
# Clone and setup
git clone <repo>
cd daily-journal-prompt-webapp
# Start with Docker Compose
docker-compose up --build
# Or develop separately
cd backend && uvicorn main:app --reload
cd frontend && npm run dev
Testing Strategy
- Backend: pytest with FastAPI TestClient
- Frontend: Vitest for unit tests, Playwright for E2E
- Integration: Docker Compose test environment
CI/CD Considerations
- GitHub Actions for testing
- Docker image building
- Deployment to cloud platform (Render, Railway, Fly.io)
Risk Assessment & Mitigation
Risks
- API Key exposure: Use environment variables, never commit to repo
- Data loss during migration: Backup JSON files, incremental migration
- Performance issues: Monitor API response times, optimize database queries
- Browser compatibility: Use modern CSS/JS, test on target browsers
Mitigations
- Comprehensive testing
- Gradual rollout
- Monitoring and logging
- Regular backups
Success Metrics
- Functionality: All CLI features available in webapp
- Performance: API response < 200ms, page load < 2s
- Usability: Intuitive UI, mobile-responsive
- Reliability: 99.9% uptime, error rate < 1%
- Maintainability: Clean code, good test coverage, documented APIs
Next Steps
Immediate Actions
- Create project structure with backend/frontend directories
- Set up FastAPI backend skeleton
- Begin refactoring core prompt generation logic
- Create basic Astro frontend
- Implement Docker configuration
Future Enhancements
- User accounts and prompt history per user
- Prompt customization options
- Export functionality (PDF, Markdown)
- Mobile app (React Native)
- Social features (share prompts, community)
Conclusion
The refactoring from CLI to webapp will significantly improve accessibility and user experience while maintaining all existing functionality. The proposed architecture using FastAPI + Astro provides a modern, performant, and maintainable foundation for future enhancements.
The phased approach allows for incremental development with clear milestones and risk mitigation at each step.