Skip to content

Commit 9ec6553

Browse files
committed
Chat App with api and frontend
1 parent dde8974 commit 9ec6553

File tree

39,075 files changed

+3228858
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

39,075 files changed

+3228858
-2
lines changed

api

Lines changed: 0 additions & 1 deletion
This file was deleted.

api/.env.example

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
4+
AWS_S3_ACCESS_KEY_ID = 'AKIAIYD4ZMBV6W2DK6KQ'
5+
AWS_S3_SECRET_ACCESS_KEY = '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'
6+
AWS_STORAGE_BUCKET_NAME = 'chatapprepo-richesh'
7+
AWS_HOST_REGION='s3.amazonaws.com'
8+
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
9+
S3_BUCKET_URL = AWS_S3_CUSTOM_DOMAIN
10+
AWS_LOCATION = 'static'
11+
12+
13+
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
14+
15+
16+
SOCKET_SERVER = '127.0.0.1:9000"
17+
18+
19+
20+
21+
22+
DB_NAME=securebit-db
23+
DB_USER=postgres
24+
DB_PASSWORD=admin123
25+
DB_HOST=127.0.0.1
26+
DB_PORT=5432
27+

api/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "/home/adefemigreat/.virtualenvs/chatapp_env/bin/python"
3+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '3'
2+
3+
services:
4+
postgres:
5+
container_name: postgres_container
6+
image: postgres
7+
environment:
8+
POSTGRES_USER: "postgres"
9+
POSTGRES_PASSWORD: "admin123"
10+
PGDATA: /data/postgres
11+
volumes:
12+
- postgres:/data/postgres
13+
ports:
14+
- "5432:5432"
15+
networks:
16+
- postgres
17+
restart: unless-stopped
18+
19+
networks:
20+
postgres:
21+
driver: bridge
22+
23+
volumes:
24+
postgres:

api/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.6
2+
3+
ENV PYTHONDONTWRITEBYTECODE 1
4+
ENV PYTHONUNBUFFERED 1
5+
6+
RUN mkdir /chatapi
7+
8+
WORKDIR /chatapi
9+
10+
COPY . /chatapi/
11+
12+
RUN pip install --upgrade pip && pip install pip-tools && pip install -r requirements.txt

api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a simple chat api to with socket.io

api/chatapi/__init__.py

Whitespace-only changes.

api/chatapi/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for chatapi project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chatapi.settings')
15+
16+
application = get_asgi_application()

api/chatapi/custom_methods.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from rest_framework.permissions import BasePermission, SAFE_METHODS
2+
from django.utils import timezone
3+
from rest_framework.views import exception_handler
4+
from rest_framework.response import Response
5+
from django.contrib.auth import authenticate
6+
7+
8+
class IsAuthenticatedCustom(BasePermission):
9+
10+
def has_permission(self, request, view):
11+
from user_control.views import decodeJWT
12+
user = decodeJWT(request.META['HTTP_AUTHORIZATION'])
13+
if not user:
14+
return False
15+
request.user = user
16+
if request.user and request.user.is_authenticated:
17+
from user_control.models import CustomUser
18+
CustomUser.objects.filter(id=request.user.id).update(
19+
is_online=timezone.now())
20+
return True
21+
return False
22+
23+
24+
class IsAuthenticatedOrReadCustom(BasePermission):
25+
def has_permission(self, request, view):
26+
if request.method in SAFE_METHODS:
27+
return True
28+
29+
if request.user and request.user.is_authenticated:
30+
from user_control.models import CustomUser
31+
CustomUser.objects.filter(id=request.user.id).update(
32+
is_online=timezone.now())
33+
return True
34+
return False
35+
36+
37+
def custom_exception_handler(exc, context):
38+
39+
response = exception_handler(exc, context)
40+
41+
if response is not None:
42+
return response
43+
44+
exc_list = str(exc).split("DETAIL: ")
45+
46+
return Response({"error": exc_list[-1]}, status=403)

api/chatapi/settings.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
"""
2+
Django settings for chatapi project.
3+
4+
Generated by 'django-admin startproject' using Django 3.1.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.1/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/3.1/ref/settings/
11+
"""
12+
13+
from pathlib import Path
14+
import os
15+
from decouple import config
16+
17+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
18+
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
19+
20+
21+
# Quick-start development settings - unsuitable for production
22+
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
23+
24+
# SECURITY WARNING: keep the secret key used in production secret!
25+
SECRET_KEY = '10y83$&yi^2_g_y*r^eeabge40t&0ioyd9=m-4h-sc!0aq4rjm'
26+
27+
# SECURITY WARNING: don't run with debug turned on in production!
28+
DEBUG = True
29+
30+
ALLOWED_HOSTS = ['*']
31+
32+
AUTH_USER_MODEL = "user_control.CustomUser"
33+
34+
REST_FRAMEWORK = {
35+
'EXCEPTION_HANDLER': 'chatapi.custom_methods.custom_exception_handler',
36+
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
37+
"PAGE_SIZE": 20,
38+
}
39+
40+
41+
# Application definition
42+
43+
INSTALLED_APPS = [
44+
'django.contrib.admin',
45+
'django.contrib.auth',
46+
'django.contrib.contenttypes',
47+
'django.contrib.sessions',
48+
'django.contrib.messages',
49+
'django.contrib.staticfiles',
50+
'rest_framework',
51+
'user_control',
52+
'message_control',
53+
'chatapi',
54+
'corsheaders',
55+
]
56+
57+
MIDDLEWARE = [
58+
'django.middleware.security.SecurityMiddleware',
59+
'django.contrib.sessions.middleware.SessionMiddleware',
60+
'corsheaders.middleware.CorsMiddleware',
61+
'django.middleware.common.CommonMiddleware',
62+
'django.middleware.csrf.CsrfViewMiddleware',
63+
'django.contrib.auth.middleware.AuthenticationMiddleware',
64+
'django.contrib.messages.middleware.MessageMiddleware',
65+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
66+
]
67+
68+
ROOT_URLCONF = 'chatapi.urls'
69+
70+
TEMPLATES = [
71+
{
72+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
73+
'DIRS': [],
74+
'APP_DIRS': True,
75+
'OPTIONS': {
76+
'context_processors': [
77+
'django.template.context_processors.debug',
78+
'django.template.context_processors.request',
79+
'django.contrib.auth.context_processors.auth',
80+
'django.contrib.messages.context_processors.messages',
81+
],
82+
},
83+
},
84+
]
85+
86+
WSGI_APPLICATION = 'chatapi.wsgi.application'
87+
88+
CORS_ORIGIN_ALLOW_ALL = True
89+
CORS_ALLOW_HEADERS = (
90+
'x-requested-with',
91+
'content-type',
92+
'accept',
93+
'origin',
94+
'authorization',
95+
'accept-encoding',
96+
'x-csrftoken',
97+
'access-control-allow-origin',
98+
'content-disposition'
99+
)
100+
CORS_ALLOW_CREDENTIALS = False
101+
CORS_ALLOW_METHODS = ('GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS')
102+
103+
104+
# Database
105+
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
106+
107+
DB_NAME = config("DB_NAME")
108+
DB_USER = config("DB_USER")
109+
DB_PASSWORD = config("DB_PASSWORD")
110+
DB_HOST = config("DB_HOST")
111+
DB_PORT = config("DB_PORT")
112+
113+
DATABASES = {
114+
'default': {
115+
'ENGINE': 'django.db.backends.postgresql_psycopg2',
116+
'NAME': DB_NAME,
117+
'USER': DB_USER,
118+
'PASSWORD': DB_PASSWORD,
119+
'HOST': DB_HOST,
120+
'PORT': DB_PORT,
121+
}
122+
}
123+
124+
125+
# Password validation
126+
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
127+
128+
AUTH_PASSWORD_VALIDATORS = [
129+
{
130+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
131+
},
132+
{
133+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
134+
},
135+
{
136+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
137+
},
138+
{
139+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
140+
},
141+
]
142+
143+
144+
# Internationalization
145+
# https://docs.djangoproject.com/en/3.1/topics/i18n/
146+
147+
LANGUAGE_CODE = 'en-us'
148+
149+
TIME_ZONE = 'UTC'
150+
151+
USE_I18N = True
152+
153+
USE_L10N = True
154+
155+
USE_TZ = True
156+
157+
158+
# Static files (CSS, JavaScript, Images)
159+
# https://docs.djangoproject.com/en/3.1/howto/static-files/
160+
161+
162+
S3_BUCKET_URL = config('S3_BUCKET_URL')
163+
STATIC_ROOT = 'staticfiles'
164+
165+
AWS_ACCESS_KEY_ID = config('AWS_S3_ACCESS_KEY_ID')
166+
AWS_SECRET_ACCESS_KEY = config('AWS_S3_SECRET_ACCESS_KEY')
167+
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
168+
AWS_HOST_REGION = config('AWS_HOST_REGION')
169+
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
170+
AWS_DEFAULT_ACL = None
171+
172+
AWS_LOCATION = 'static'
173+
174+
MEDIA_URL = '/media/'
175+
176+
177+
STATICFILES_DIRS = [
178+
os.path.join(BASE_DIR, 'chatapi/static'),
179+
]
180+
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
181+
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
182+
183+
184+
AWS_S3_OBJECT_PARAMETERS = {
185+
'CacheControl': 'max-age=86400',
186+
}
187+
188+
189+
DEFAULT_FILE_STORAGE = 'chatapi.storage_backends.MediaStorage'
190+
191+
SOCKET_SERVER = config("SOCKET_SERVER")

api/chatapi/storage_backends.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from storages.backends.s3boto3 import S3Boto3Storage
2+
3+
4+
class MediaStorage(S3Boto3Storage):
5+
location = 'media'
6+
file_overwrite = False

api/chatapi/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
from django.contrib import admin
3+
from django.urls import path, include
4+
from django.conf.urls.static import static
5+
from django.conf import settings
6+
7+
urlpatterns = [
8+
path('admin/', admin.site.urls),
9+
path('user/', include('user_control.urls')),
10+
path('message/', include('message_control.urls'))
11+
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

api/chatapi/wsgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for chatapi project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chatapi.settings')
15+
16+
application = get_wsgi_application()

api/docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: "3"
2+
3+
services:
4+
api:
5+
build: .
6+
command: bash -c "python manage.py runserver 0.0.0.0:8000"
7+
container_name: chatapi
8+
restart: unless-stopped
9+
volumes:
10+
- .:/chatapi
11+
ports:
12+
- "8000:8000"
13+
networks:
14+
- postgres
15+
16+
networks:
17+
postgres:
18+
driver: bridge

0 commit comments

Comments
 (0)