Skip to content

Commit

Permalink
Chat App with api and frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
richeshgupta committed Mar 18, 2021
1 parent dde8974 commit 9ec6553
Show file tree
Hide file tree
Showing 39,075 changed files with 3,228,858 additions and 2 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 0 additions & 1 deletion api
Submodule api deleted from 8b0a51
27 changes: 27 additions & 0 deletions api/.env.example
@@ -0,0 +1,27 @@



AWS_S3_ACCESS_KEY_ID = 'AKIAIYD4ZMBV6W2DK6KQ'
AWS_S3_SECRET_ACCESS_KEY = 'nsd86pZaEDhOAXUJ24B/79sHESkvriLJsPtA2fyH'
AWS_STORAGE_BUCKET_NAME = 'chatapprepo-richesh'
AWS_HOST_REGION='s3.amazonaws.com'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
S3_BUCKET_URL = AWS_S3_CUSTOM_DOMAIN
AWS_LOCATION = 'static'


STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'


SOCKET_SERVER = '127.0.0.1:9000"





DB_NAME=securebit-db
DB_USER=postgres
DB_PASSWORD=admin123
DB_HOST=127.0.0.1
DB_PORT=5432

3 changes: 3 additions & 0 deletions api/.vscode/settings.json
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/home/adefemigreat/.virtualenvs/chatapp_env/bin/python"
}
24 changes: 24 additions & 0 deletions api/DatabaseManagement/docker-compose.yml
@@ -0,0 +1,24 @@
version: '3'

services:
postgres:
container_name: postgres_container
image: postgres
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "admin123"
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped

networks:
postgres:
driver: bridge

volumes:
postgres:
12 changes: 12 additions & 0 deletions api/Dockerfile
@@ -0,0 +1,12 @@
FROM python:3.6

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN mkdir /chatapi

WORKDIR /chatapi

COPY . /chatapi/

RUN pip install --upgrade pip && pip install pip-tools && pip install -r requirements.txt
1 change: 1 addition & 0 deletions api/README.md
@@ -0,0 +1 @@
# This is a simple chat api to with socket.io
Empty file added api/chatapi/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions api/chatapi/asgi.py
@@ -0,0 +1,16 @@
"""
ASGI config for chatapi project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chatapi.settings')

application = get_asgi_application()
46 changes: 46 additions & 0 deletions api/chatapi/custom_methods.py
@@ -0,0 +1,46 @@
from rest_framework.permissions import BasePermission, SAFE_METHODS
from django.utils import timezone
from rest_framework.views import exception_handler
from rest_framework.response import Response
from django.contrib.auth import authenticate


class IsAuthenticatedCustom(BasePermission):

def has_permission(self, request, view):
from user_control.views import decodeJWT
user = decodeJWT(request.META['HTTP_AUTHORIZATION'])
if not user:
return False
request.user = user
if request.user and request.user.is_authenticated:
from user_control.models import CustomUser
CustomUser.objects.filter(id=request.user.id).update(
is_online=timezone.now())
return True
return False


class IsAuthenticatedOrReadCustom(BasePermission):
def has_permission(self, request, view):
if request.method in SAFE_METHODS:
return True

if request.user and request.user.is_authenticated:
from user_control.models import CustomUser
CustomUser.objects.filter(id=request.user.id).update(
is_online=timezone.now())
return True
return False


def custom_exception_handler(exc, context):

response = exception_handler(exc, context)

if response is not None:
return response

exc_list = str(exc).split("DETAIL: ")

return Response({"error": exc_list[-1]}, status=403)
191 changes: 191 additions & 0 deletions api/chatapi/settings.py
@@ -0,0 +1,191 @@
"""
Django settings for chatapi project.
Generated by 'django-admin startproject' using Django 3.1.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path
import os
from decouple import config

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '10y83$&yi^2_g_y*r^eeabge40t&0ioyd9=m-4h-sc!0aq4rjm'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

AUTH_USER_MODEL = "user_control.CustomUser"

REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'chatapi.custom_methods.custom_exception_handler',
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 20,
}


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'user_control',
'message_control',
'chatapi',
'corsheaders',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'chatapi.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'chatapi.wsgi.application'

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_HEADERS = (
'x-requested-with',
'content-type',
'accept',
'origin',
'authorization',
'accept-encoding',
'x-csrftoken',
'access-control-allow-origin',
'content-disposition'
)
CORS_ALLOW_CREDENTIALS = False
CORS_ALLOW_METHODS = ('GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS')


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DB_NAME = config("DB_NAME")
DB_USER = config("DB_USER")
DB_PASSWORD = config("DB_PASSWORD")
DB_HOST = config("DB_HOST")
DB_PORT = config("DB_PORT")

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': DB_HOST,
'PORT': DB_PORT,
}
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/


S3_BUCKET_URL = config('S3_BUCKET_URL')
STATIC_ROOT = 'staticfiles'

AWS_ACCESS_KEY_ID = config('AWS_S3_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_S3_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_HOST_REGION = config('AWS_HOST_REGION')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_DEFAULT_ACL = None

AWS_LOCATION = 'static'

MEDIA_URL = '/media/'


STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'chatapi/static'),
]
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'


AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}


DEFAULT_FILE_STORAGE = 'chatapi.storage_backends.MediaStorage'

SOCKET_SERVER = config("SOCKET_SERVER")
6 changes: 6 additions & 0 deletions api/chatapi/storage_backends.py
@@ -0,0 +1,6 @@
from storages.backends.s3boto3 import S3Boto3Storage


class MediaStorage(S3Boto3Storage):
location = 'media'
file_overwrite = False
11 changes: 11 additions & 0 deletions api/chatapi/urls.py
@@ -0,0 +1,11 @@

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
path('admin/', admin.site.urls),
path('user/', include('user_control.urls')),
path('message/', include('message_control.urls'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
16 changes: 16 additions & 0 deletions api/chatapi/wsgi.py
@@ -0,0 +1,16 @@
"""
WSGI config for chatapi project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chatapi.settings')

application = get_wsgi_application()
18 changes: 18 additions & 0 deletions api/docker-compose.yml
@@ -0,0 +1,18 @@
version: "3"

services:
api:
build: .
command: bash -c "python manage.py runserver 0.0.0.0:8000"
container_name: chatapi
restart: unless-stopped
volumes:
- .:/chatapi
ports:
- "8000:8000"
networks:
- postgres

networks:
postgres:
driver: bridge

0 comments on commit 9ec6553

Please sign in to comment.