How hsu022210/snippets-backend works
Project type and purpose
• This is a backend-only, RESTful API server built with Django and Django REST Framework.
• It provides CRUD operations for code snippets, user management, JWT authentication, password reset via email, syntax highlighting (Pygments), and auto-generated API docs (Swagger/OpenAPI).
File structure
• Top-level:
• manage.py – Django CLI entrypoint
• build.sh – deployment automation
• requirements.txt – Python dependencies
• config/ – Django project settings, ASGI/WSGI, URL router
• Apps:
• authentication/ – custom user model, serializers, views, URL patterns, email templates, token blacklisting, password reset utils
• snippets/ – snippet model, serializers, views, URL patterns, pagination, filtering, custom permissions
• Deployment & settings:
• config/deployment_settings.py – production overrides
• config/settings.py – core settings (DB config, CORS, installed apps)
• Tests: each app has a tests.py
• Migrations directories for both apps
README insights
• Tech stack: Django 4.2, DRF, SimpleJWT, Pygments, drf-spectacular, Gunicorn, Whitenoise, PostgreSQL (prod)/SQLite (dev), django-cors-headers
• Endpoints, environment-variable setup, build/deploy steps outlined
• Highlights security features (JWT, CSRF, CORS, SQL injection protection)
1. Client(s): Web frontend or third-party consumer
2. API Gateway (HTTP layer): Gunicorn → ASGI/WSGI → Django URL router
3. Django Apps:
• authentication app (register, login, logout, password reset)
• snippets app (CRUD snippet operations, pagination, filtering)
4. Serializers & Views: request/response translation
5. Models & ORM: Django models → SQLite/PostgreSQL database
6. External Services:
• Email SMTP server (password reset emails)
• Pygments library (syntax highlighting engine)
• Swagger UI / Redoc static documentation served via Whitenoise
7. Deployment & Static Files: Whitenoise for static assets, environment variables, build.sh script
b. Relationships and data flow
• Password reset: View triggers email util → SMTP → User email inbox
• Highlighting: Snippet view passes code to Pygments API/library → highlighted HTML → stored or returned
• Token flow: Login view issues JWT → client stores token → included in Authorization header on subsequent calls → authenticated middleware
c. Architectural patterns & principles
• Django’s Model-View-Template (MVT) / REST layering (Views, Serializers, Models)
• Modular “apps” for separation of concerns (auth vs. domain logic)
• Stateless API using JWT for horizontal scalability
• Configuration-as-code via settings modules and env vars
• Single Process Gunicorn workers behind a reverse proxy (not shown)
d. Key technologies to annotate
• Django, DRF (views, routers, serializers)
• djangorestframework-simplejwt (JWT authentication)
• Pygments (syntax highlighting)
• drf-spectacular (Swagger / OpenAPI schema generation)
• Gunicorn & Whitenoise (deployment)
• PostgreSQL / SQLite (database engine)
• django-cors-headers (CORS)