Skip to content

Commit

Permalink
Merge pull request #85 from felixfaisal/staging
Browse files Browse the repository at this point in the history
Adding Nginx Proxy
  • Loading branch information
felixfaisal authored Jul 19, 2021
2 parents b8d9f4d + c28e70c commit 58bbb67
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
12 changes: 5 additions & 7 deletions backend/API/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
from . import views

urlpatterns = [
path('oauth2/', views.index, name='index'),
path('oauth2/logout/', views.discord_logout, name='logout'),
path('oauth2/login/', views.discord_login, name='login'),
path('oauth2/login/redirect/', views.discord_login_redirect, name='loginredirect'),
path('api/form/list', views.formlist, name='formlist'),
path('api/responses/', views.responselist, name='responselist'),
path('api/form/create/', views.formcreateresponse, name='formcreateresponse'),
path('api/form/list', views.formlist, name='formlist'),
path('api/form/create/', views.formcreateresponse, name='formcreateresponse'),
path('api/form/response/<str:FormName>', views.formresponse, name='formresponse'),
path('api/user/create/', views.userCreate, name='userCreate'),
path('api/user/login/', views.userLogin, name='userLogin'),
path('api/user/logout/', views.userLogout, name='userLogout'),
path('api/user/server/', views.userServers, name='userServer'),
path('api/user/server/', views.userServers, name='userServer'),
path('api/user/channels/<str:ServerID>', views.serverChannels, name='serverChannels'),
path('api/user/information', views.userInformation, name='userInformation'),
path('api/bot/forms/<str:serverid>', views.botFormList, name='botFormList'),
path('api/user/information', views.userInformation, name='userInformation'),
path('api/bot/forms/<str:serverid>', views.botFormList, name='botFormList'),
path('api/bot/response/', views.botFormResponse, name='botFormResponse'),
path('api/user/dashboard', views.dashboardInformation, name='dashboardInformation'),
path('api/bot/form/response/<str:FormName>', views.botFormResponseList, name='botFormResponseList'),
Expand Down
52 changes: 18 additions & 34 deletions backend/API/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@

redirect_url_discord = os.getenv("REDIRECT_URL_DISCORD")

''' Redirects to Discord Login Page '''
def discord_login(request): # oauth2/login/redirect/
return redirect(redirect_url_discord)


''' Route for logging out '''
def discord_logout(request): # oauth2/logout/
logout(request)
return JsonResponse("Succesfully Logged out", safe=False)


''' Redirect route after discord oauth, Obtains access token to interact with Discord API '''
def discord_login_redirect(request): # oauth2/login/redirect/
code = request.GET.get('code')
access_token = getAccessToken(code)
Expand All @@ -56,17 +57,9 @@ def discord_login_redirect(request): # oauth2/login/redirect/
atoken.save()
print(token.key)
#redirect_url_react = 'http://localhost:3000/dashboard?token='+token.key
return redirect('http://localhost:3000?user='+str(token.key))


@login_required(login_url='login/')
@api_view(["GET"])
@authentication_classes([TokenAuthentication])
def index(request): # oauth2/
print(request.user)
return JsonResponse("Have false", safe=False)

return redirect('http://localhost?user='+str(token.key))

''' Provides list of forms present in the database '''
@api_view(["GET"])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
Expand All @@ -79,24 +72,15 @@ def formlist(request): # api/form/list

return Response("You are not logged in!")


@api_view(["GET"])
@authentication_classes([TokenAuthentication])
def responselist(request): # api/responses/
print(request.user)
response = FormResponse.objects.all()
serializer = FormResponseSerializer(response, many=True)
return Response(serializer.data)


''' Provides list of responses made by the user '''
@api_view(["GET"])
def formresponse(request, FormName): # api/form/response/<str:FormName>
form = FormCreate.objects.get(FormName=FormName, userid=request.user)
response = FormResponse.objects.filter(form_id=form.form_id)
serializer = FormResponseSerializer(response, many=True)
return Response(serializer.data)


''' To submit a response to a form '''
@api_view(["POST"])
@authentication_classes([TokenAuthentication])
def formcreateresponse(request): # api/form/create/
Expand All @@ -115,7 +99,7 @@ def formcreateresponse(request): # api/form/create/

return Response(serializer.data)


''' Creating user and storing in the database '''
@api_view(['GET', 'POST'])
def userCreate(request): # api/user/create/
access_token = request.data.get('access_token')
Expand All @@ -128,21 +112,21 @@ def userCreate(request): # api/user/create/
atoken = AccessTokenTable(user=discord_user, access_token=access_token)
atoken.save()


''' Custom logging in feature, It's a hacky mechanism '''
@api_view(['GET', 'POST'])
@authentication_classes([TokenAuthentication])
def userLogin(request): # api/user/login/
login = LoginTable.objects.get(user=request.user)
return JsonResponse(login.loggedIn, safe=False)


''' Custom logout feature, Also a hacky mechanism '''
@api_view(['GET', 'POST'])
@authentication_classes([TokenAuthentication])
def userLogout(request): # api/user/logout/
login = LoginTable.objects.get(user=request.user)
return JsonResponse('False', safe=False)


''' Fetch user information such as avatar and tag '''
@api_view(['GET', 'POST'])
@authentication_classes([TokenAuthentication])
def userInformation(request): # api/user/information
Expand All @@ -156,23 +140,23 @@ def userInformation(request): # api/user/information
}
return JsonResponse(jsondata, safe=False)


''' Fetch all the servers user is a part of '''
@api_view(['GET', 'POST'])
@authentication_classes([TokenAuthentication])
def userServers(request): # api/user/server/
access_token = AccessTokenTable.objects.get(user=request.user).access_token
servers = getUserServers(access_token)
return Response(servers)


''' Fetch all the responses made by the user '''
@api_view(['GET', 'POST'])
@authentication_classes([TokenAuthentication])
def userResponses(request): # api/user/responses
responses = FormResponse.objects.filter(user_id=request.user.id)
serializer = UserResponseSerializer(responses, many=True)
return Response(serializer.data)


''' Fetch all the channels present in a selected server'''
@api_view(['GET', 'POST'])
@authentication_classes([TokenAuthentication])
def serverChannels(request, ServerID): # api/user/channels/<str:ServerID>
Expand All @@ -181,14 +165,14 @@ def serverChannels(request, ServerID): # api/user/channels/<str:ServerID>
channels = getServerChannels(access_token, ServerID)
return Response(channels)


''' List of forms present in a given server '''
@api_view(['GET', 'POST'])
def botFormList(request, serverid): # api/bot/forms/<str:serverid>
forms = FormCreate.objects.filter(serverid=serverid)
serializer = FormBotCreateSerializer(forms, many=True)
return Response(serializer.data)


''' Submit response to a given form '''
@api_view(['GET', 'POST'])
def botFormResponse(request): # api/bot/response/
serializer = FormBotResponseSerializer(data=request.data, many=False)
Expand All @@ -203,15 +187,15 @@ def botFormResponse(request): # api/bot/response/
print(newformresponse)
return Response(serializer.data)


''' Fetch responses for a given form '''
@api_view(['GET', 'POST']) # api/bot/form/response/<str:FormName>
def botFormResponseList(request, FormName):
form = FormCreate.objects.get(FormName=FormName)
responses = FormResponse.objects.filter(form_id=form.form_id)
serializer = FormResponseSerializer(responses, many=True)
return Response(serializer.data)


''' Fetch dashboard information for the frontend '''
@api_view(['GET'])
@authentication_classes([TokenAuthentication]) # api/user/dashboard
def dashboardInformation(request):
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
version: "3.0"

services:
web:
image: nginx
ports:
- "80:80"
volumes:
- ./nginx/:/etc/nginx/conf.d
depends_on:
- frontend
- backend

frontend:
build: ./frontend
ports:
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/Utils/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const BASE_URL = "http://localhost:8000";
const BASE_URL = "http://localhost:80";

export const LOGIN_URL = `${BASE_URL}/oauth2/login`;
export const LOGIN_URL = `${BASE_URL}/api/oauth2/login`;

export const USER_URL = `${BASE_URL}/api/user`;
export const USER_INFORMATION_URL = `${USER_URL}/information`;
Expand Down
9 changes: 9 additions & 0 deletions nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server{
listen 80;
location / {
proxy_pass http://frontend:3000/;
}
location /api {
proxy_pass http://backend:8000/api;
}
}

0 comments on commit 58bbb67

Please sign in to comment.