Skip to content

Social Links' goal is to create a backend using Django REST Framework to build API views and serializers

License

Notifications You must be signed in to change notification settings

mustafaakgul/social-links

Repository files navigation

Social Link Project

Technology Stack - Backend, DevOps

Technology Stack - Frontend

  • Language -> JavaScript
  • Framework -> React
  • State Management -> Redux
  • UI Framework -> Material UI
  • UI Components -> React Bootstrap
  • UI Design -> Figma
  • AI -> Locofy
  • IDE -> WebStorm - https://www.jetbrains.com/webstorm/
  • .env By Local, Dev, Prod
  • .gitignore
  • Package Installer -> NPM

Technology Stack - Mobile

  • Language -> JavaScript
  • Framework -> React, React Native
  • State Management -> Redux
  • Mobile Platform -> Expo to iOS, Android
  • Design -> Figma
  • AI -> Locofy
  • IDE -> WebStorm - https://www.jetbrains.com/webstorm/
  • .env By Local, Dev, Prod
  • .gitignore

Technology Stack - Project Management

  • Project Management -> Trello
  • Project Management Approach -> Agile in Trello
  • Agile Framework -> Scrum, Kanban
  • Agile Methodology -> Sprint
  • Agile Ceremonies -> Sprint Planning, Daily Scrum, Sprint Review, Sprint Retrospective
  • Agile Artifacts -> Product Backlog, Sprint Backlog, Burndown Chart
  • Agile Roles -> Product Owner, Scrum Master, Development Team
  • Communication -> Discord, Slack
  • Notification -> Slack
  • File Sharing -> Google Drive
  • Documentation -> Notion
  • Design & Boards -> Figma
  • Version Control -> Git
  • Githost -> GitHub

About Product

Link in Profile Apps like linktr.ee, linkme, heylink

Design Samples

Features

  • Health Check

Sources

Core

Github

Samples

Best Practices

  • Use Django & DRF Business Logic Layer (Services) to test better
  • Use Layers Presentation Layer (Views), Business Logic Layer (Services), Data Access Layer (Models)
  • Use Custom Response Model
  • Client-Server Architecture
  • Ensure that the API scales
  • Use an international design standard The OpenAPI v3
  • Cacheable -> Caching Responses -> Cache frequently requested API responses to improve performance and reduce the load on your server. DRF supports various caching strategies that can be easily integrated.
  • Stateless
  • Use Nouns Instead of Verbs in Endpoints -> https://mysite.com/posts not https://mysite.com/createPost
  • Don't use POST: /articles/createNewArticle/ Do use POST: /articles/
  • Name Collections with Plural Nouns -> So, instead of https://mysite.com/post/123, it should be https://mysite.com/posts/123, GET /cars/123, POST /cars, GET /cars
  • Use Status Codes in Error Handling -> Informational Responses, Redirects, Client-side errors, Server-side errors
  • Use Nesting on Endpoints to Show Relationships and Nested Serializers and Related Data -> https://mysite.com/posts/postId/comments, You should avoid nesting that is more than 3 levels deep as this can make the API less elegant and readable
    • /users // list all users
    • /users/123 // specific user
    • /users/123/orders // list of orders that belong to a specific user
    • /users/123/orders/0001 // specific order of a specific users order list
  • Use Filtering, Sorting, and Pagination to Retrieve the Data Requested -> https://mysite.com/posts?sortBy=createdAt&sortOrder=desc&limit=10&offset=0
  • Use SSL for Security -> https://mysite.com/posts
  • Return Error Details in the Response Body -> { "error": "Invalid payload.", "detail": { "surname": "This field is required." } }
  • Provide Accurate API Documentation -> Documentation with Swagger and API Docs for API Consumers
    • The documentation should contain:
      • relevant endpoints of the API
      • example requests of the endpoints
      • implementation in several programming languages
      • messages listed for different errors with their status codes
  • Tests should cover all API endpoints, be sure to use mock to mock external API calls, Be sure to include tests that cover all possible error conditions, Write comprehensive unit tests using DRF’s testing tools to validate the functionality of your API endpoints. Test-driven development (TDD) ensures a robust and bug-free API.
  • Check that valid data is returned for 201 or 200 responses, make sure the proper error codes/messages are being returned for 4xx responses
  • Use exception handling and custom response model
  • Response message with status codes { ‘status’:’success|error’, ‘data’:{ 'result':{} || [] , '' }, 200 OK — Success — GET/PUT — return resource/status message 201 Created — Success — POST — provide status message or return newly created object 204 No Content — Success — DELETE 304 Unchanged — Redirect — ALL — Indicates no changes since last request 400 Bad Request — Failure — GET/PUT/POST — invalid request, return error messages 401 Unauthorized — Failure — ALL — missing credentials/Authentication required 403 Forbidden — Failure — ALL — restricted content 404 Not Found — Failure — Resource not found 405 Method Not Allowed Failure — Failure — ALL — An invalid HTTP method was attempted
  • Versioning Your APIs -> https://mysite.com/v2 for version 2, Implement API versioning from the beginning to ensure backward compatibility as your API evolves. DRF provides easy-to-use tools for versioning, allowing you to handle changes gracefully.
  • Validation and Error Handling -> DRF provides comprehensive validation tools to ensure data integrity. Handle errors gracefully and provide meaningful error responses to API consumers.
  • Optimizing Database Queries -> Avoid the N+1 query problem by using DRF’s queryset optimization techniques like select_related and prefetch_related to minimize database queries.
  • Use UUIDS for Primary Keys -> Use UUIDs instead of auto-incrementing integers for primary keys to avoid exposing internal IDs to API consumers.

Business Logic Layer (Services)

Structure

myproject_website/ ├── commands/ ├── db_backups/ ├── mockups/ ├── src/ │ └── django-myproject/ │ ├── externals/ │ │ ├── apps/ │ │ │ └── README.md │ │ └── libs/ │ │ └── README.md │ ├── locale/ │ ├── media/ │ ├── myproject/ │ │ ├── apps/ │ │ │ ├── core/ │ │ │ │ ├── init.py │ │ │ │ └── versioning.py │ │ │ └── init.py │ │ ├── settings/ │ │ │ ├── init.py │ │ │ ├── _base.py │ │ │ ├── dev.py │ │ │ ├── production.py │ │ │ ├── sample_secrets.json │ │ │ ├── secrets.json │ │ │ ├── staging.py │ │ │ └── test.py │ │ ├── site_static/ │ │ │ └── site/ │ │ │ django-admin.py startproject myproject ├── css/ │ │ │ │ └── style.css │ │ │ ├── img/ │ │ │ │ ├── favicon-16x16.png │ │ │ │ ├── favicon-32x32.png │ │ │ │ └── favicon.ico │ │ │ ├── js/ │ │ │ │ └── main.js │ │ │ └── scss/ │ │ │ └── style.scss │ │ ├── templates/ │ │ │ ├── base.html │ │ │ └── index.html │ │ ├── init.py │ │ ├── urls.py │ │ └── wsgi.py │ ├── requirements/ │ │ ├── _base.txt │ │ ├── dev.txt │ │ ├── production.txt │ │ ├── staging.txt │ │ └── test.txt │ ├── static/ │ ├── LICENSE │ └── manage.py └── env/

Code Quality Tools

  • black -> pip install black, black views.py
  • flake8 -> pip install flake8, flake8 views.py

How to Run

Docker

  • Docker: docker-compose up --build, docker-compose -f docker-compose.dev.yml up --build
    • docker exec -it name_of_container pytest -rP -vv

DEV

  • Build the Docker image -> docker build -t nodeme .
  • Push the Docker image to a container registry ->
    • aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.eu-central-1.amazonaws.com
    • docker tag my-django-app:latest aws_account_id.dkr.ecr.us-west-2.amazonaws.com/my-django-app:latest
    • docker push aws_account_id.dkr.ecr.us-west-2.amazonaws.com/my-django-app:latest
  • Create an Elastic Beanstalk environment ->
    • eb init -p docker my-django-app --region us-west-2
    • eb create my-django-app-env --instance-type t2.micro --region us-west-2
  • Step 5: Deploy your Django application to Elastic Beanstalk -> eb deploy

Django

Local

  • python -m venv venv
  • source venv/bin/activate
  • pip install -r requirements.txt
  • python manage.py makemigrations
  • python manage.py migrate
  • python manage.py collectstatic
  • python manage.py createsuperuser
  • python manage.py runserver
  • Create .env file from env.base
  • Edit manage.py, wsgi.py, asgi.py for your environment

DEV in AWS Elastic Beanstalk

  • deactivate
  • eb --version
  • eb init --region eu-central-1 -p python-3.11 nodeme-backend
  • eb init
  • eb create nodeme-backend-env
  • eb logs
  • eb status -> CNAME in allowed_host
  • git commit -am "commit message", git push
  • eb deploy -> to deploy any changes, for instance changed allowed_host
  • eb open
  • To save instance hours and other AWS resources between development sessions, terminate your Elastic Beanstalk environment with eb terminate ->eb terminate nodeme-backend-env
  • eb console
  • eb ssh

TODOs

  • //TODO

About

Social Links' goal is to create a backend using Django REST Framework to build API views and serializers

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages