Development note
Please see note for detailed explainations:
django-school-api-drf-nested-router
Sample usage of Django, Django Rest Framework, drf-nested-routers DEUBG=True is intentionally left on, to disable change environmental variable ENV to anything other than staging This is intended to be backend, all non matched routes are 404 by default
Milestones
- Create Django App
- Use Postgres
- Use Pipenv
- Stores sensitive information in .env (and not committing them!)
- models: student, first_name, last_name, student identification id as UUID4() but only 20 chars
- models: school, name, max_student
- Use DRF
- Enable DRF API views
- use ModelViewSet
- use ModelSerializer
- endpoint /students/ GET, POST
- endpoint /schools/ GET, POST
- endpoint /students/:id PUT, PATCH, DELETE
- endpoint /schools/:id PUT, PATCH, DELETE
- Automatic UUID for student upon creation
- Disallow student to overflow school
- Use Django Nested Routers
- /schools/:id/students GET
- /schools/:id/students POST
- /schools/:id/students/:id GET, POST, PATCH, PUT, DELETE
- /students/ still works
Deployments
Heroku
https://django-school-api.herokuapp.com/api/v1/students/
AWS ECS Fargate
http://18.140.53.106:8000/api/v1/students/
docker-compose
Docker compose with quick tunnel to the internet for collaboration cloudflare argo tunnels your applications to the internet through Cloludflare without exposing any ports
docker compose --env-file ./src/app/app/.env up --build --force-recreate
Look for a url similar to https://savage-co**********cloudflare.com/api/v1/students/ in logs
pipenv with postgres in docker
cd postgres
docker compose up --build --force-recreate
cd src/app
conda activate <env>
pipenv run python manage.py runserver
Proceed to 127.0.0.1:8000/api/v1/students/
Notice
AWS and Heroku deployments will be taken down by the end of June. 💡 Paid services are already taken down.
Testings
usage
Make sure to change BASE_URL according to which deployment you want to test
conda create --name pytest python=3.9.7
conda activate pytest
pip install -r requirements.txt
cd tests
pytest -vv test_*
Bonus
- Notion development documentation
- Use deploy Postgres via docker with data persistence
- admin view for debugging/ quick edits
- Test driven design with pytest (with conda plus pip install -r requirements.txt)
- AWS Fargate serverless docker hosting
- Postgres deployed in AWS RDS
- USED AWS ECS Registry
- Docker compose local deploy with automatic exposing to the internet through cloudflare Argi (suitable for short client demo, team demo)
- Heroku database setup (does this count as a bonus ?)
- gunicorn was used in AWS deployment
- Serve static file without CDN/Bucket with Whitenoise
Time logging
- Preparing, reading some documentations, and thinking how to tackle this problem: about 1-2 days on, and off while driving, showering (best methods!)
- Setting up the development environment: 15 minutes, I do this pretty much everyday
- Writing Django models up to working api routes 30 minutes, I do this everyday, too, but with Django Ninja instead of DRF
- Django Nested Routers: about 2 hours of trying to understand the sparse documentations and how data is passed, I have never used this before. I think of this as a challenge to see how I learn new techs.
- Writing helper functions: less than 15 minutes
- Pytest normal cases and requirements, catching bugs: about 1.5 - 2 hours
- Pytest edge cases: 30 minutes
- Heroku deplotment: 30 minutes
- AWS deployment: 2 hours, built image on a Mac M1, forgot to specify amd64 architechture since AWS ECS Fargate is amd64 and M1 is arm64 :')
- Docker local deployment with tunnels to the internet: 30 minutes
Caviets
Some config files are duplicated to root to make Heroku happy.