Skip to content

Commit

Permalink
"Added files"
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrivardhanGoenka committed Feb 6, 2024
1 parent 9c786cd commit cc8dcfb
Show file tree
Hide file tree
Showing 106 changed files with 951 additions and 1 deletion.
Binary file modified .DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions Lambda_Functions/private_library/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM public.ecr.aws/lambda/python:3.12

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install -r requirements.txt

# Copy function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.handler" ]
Binary file not shown.
5 changes: 5 additions & 0 deletions Lambda_Functions/private_library/buildscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
export num=1.0
docker build -t techfest-private-library:$num .
docker tag techfest-private-library:$num 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-private-library:$num
docker push 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-private-library:$num
112 changes: 112 additions & 0 deletions Lambda_Functions/private_library/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import psycopg2
import json
import boto3
import os

def handler(event, context):

if 'body' not in event:
return {
"statusCode": 400,
"body": json.dumps("Invalid request")
}

body = json.loads(event['body'])

if 'token' not in body:
return {
"statusCode": 400,
"body": json.dumps("Invalid request")
}

token = body['token']

uid = get_uid(token)

if not uid:
return {
"statusCode": 401,
"body": json.dumps("Unauthorized")
}

a = get_private_audio(uid)

key = os.getenv("AWS_ACCESS")
secret = os.getenv("AWS_SECRET")

s3 = boto3.client("s3", aws_access_key_id=key, aws_secret_access_key=secret, region_name="ap-south-1")
newlist = []
for audio in a:
s3key = audio['key'] + ".wav"
s3url = s3.generate_presigned_url('get_object', Params={'Bucket': 'techfest-mp3', 'Key': s3key}, ExpiresIn=3600)
audio['url'] = s3url
newlist.append(audio)

return {
"statusCode": 200,
"body": json.dumps(newlist)
}

def get_private_audio(uid):
conn = psycopg2.connect(
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT'),
sslmode='require'
)

cur = conn.cursor()

cur.execute("SELECT * FROM audio WHERE uid=%s", (uid,))

rows = cur.fetchall()

audio_public = []
for row in rows:
audio_dict = {'id': row[0], 'uid': row[1], 'key': row[2], 'private': row[3],
'name': row[4], 'genre': row[5], 'duration': row[6]}
audio_public.append(audio_dict)

conn.close()

return audio_public


def get_uid(token):
try:
# establish a connection
connection = psycopg2.connect(
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT'),
sslmode='require'
)

# create a cursor
cursor = connection.cursor()

# write your query
query = "SELECT * FROM current_sign_in WHERE token = %s;"

# execute the query
cursor.execute(query, (token,))

# fetch all the matching records
records = cursor.fetchall()

# close the cursor and the connection
cursor.close()
connection.close()

# check if any record exists
if records:
return records[0][0] # assuming uid column is the first one
else:
return False

except (Exception, psycopg2.DatabaseError) as error:
return False
2 changes: 2 additions & 0 deletions Lambda_Functions/private_library/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
psycopg2-binary
boto3
8 changes: 8 additions & 0 deletions Lambda_Functions/private_library/tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lambda_function import handler
import json

event = {"body": json.dumps({"token": "8dbb50fe6974e5e3eb574b7f0b3b15defd3bfbf812db9140f461b2d63f406bf94d66c7c2a29ad3459a60b62c96cc631d723d5d5593942efa7ee0c0d80a31c9d051193d2980d28a332ad3cfefd01ec3924016cd4a125b0a6a99fa0b6dd9243fa12d09fffea776aeb5240ae16d6abce77131fc00533e382c6c90ea1eb4c6b650"})}
context = None

response = handler(event, context)
print(json.loads(response["body"]))
13 changes: 13 additions & 0 deletions Lambda_Functions/public_library/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM public.ecr.aws/lambda/python:3.12

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install -r requirements.txt

# Copy function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.handler" ]
Binary file not shown.
5 changes: 5 additions & 0 deletions Lambda_Functions/public_library/buildscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
export num=1.0
docker build -t techfest-public-library:$num .
docker tag techfest-public-library:$num 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-public-library:$num
docker push 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-public-library:$num
50 changes: 50 additions & 0 deletions Lambda_Functions/public_library/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import psycopg2
import json
import os
import boto3

def handler(event, context):
a = get_public_audio()

key = os.getenv("AWS_ACCESS")
secret = os.getenv("AWS_SECRET")

s3 = boto3.client("s3", aws_access_key_id=key, aws_secret_access_key=secret, region_name="ap-south-1")
newlist = []
for audio in a:
s3key = audio['key'] + ".wav"
s3url = s3.generate_presigned_url('get_object', Params={'Bucket': 'techfest-mp3', 'Key': s3key}, ExpiresIn=3600)
audio['url'] = s3url
newlist.append(audio)

return {
"statusCode": 200,
"body": json.dumps(newlist)
}

def get_public_audio():
conn = psycopg2.connect(
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT'),
sslmode='require'
)

cur = conn.cursor()

cur.execute("SELECT * FROM audio WHERE private=0")

rows = cur.fetchall()

audio_public = []
for row in rows:
audio_dict = {'id': row[0], 'uid': row[1], 'key': row[2], 'private': row[3],
'name': row[4], 'genre': row[5], 'duration': row[6]}
audio_public.append(audio_dict)

conn.close()

return audio_public

2 changes: 2 additions & 0 deletions Lambda_Functions/public_library/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
psycopg2-binary
boto3
8 changes: 8 additions & 0 deletions Lambda_Functions/public_library/tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lambda_function import handler
import json

event = {}
context = None

response = handler(event, context)
print(json.loads(response["body"]))
13 changes: 13 additions & 0 deletions Lambda_Functions/sign_in/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM public.ecr.aws/lambda/python:3.12

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install -r requirements.txt

# Copy function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.handler" ]
4 changes: 4 additions & 0 deletions Lambda_Functions/sign_in/buildscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export num=1.1
docker build -t techfest-sign-in:$num .
docker tag techfest-sign-in:$num 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-sign-in:$num
docker push 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-sign-in:$num
50 changes: 50 additions & 0 deletions Lambda_Functions/sign_in/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import psycopg2

import psycopg2
import json
import secrets
import os

def handler(event, context):

if 'body' not in event:
return { "statusCode": 400, "body": json.dumps("Bad request") }

connection = psycopg2.connect(
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT'),
sslmode='require'
)
cursor = connection.cursor()

# Retrieve user data from the request body
body = json.loads(event['body'])

if 'email' not in body or 'password' not in body:
return { "statusCode": 400, "body": json.dumps("Bad request") }

email = body['email']
password = body['password']

# Check if the email and password exist in the "user" table
cursor.execute("SELECT * FROM users WHERE email = %s AND password = %s", (email, password))
user = cursor.fetchone()

if user is None:
return { "statusCode": 400, "body": json.dumps("Invalid credentials") }

uid = user[0] # Assuming the uid is the first column of the user table
token = secrets.token_hex(256)

# Insert the uid and token into the "current_sign_in" table
cursor.execute("INSERT INTO current_sign_in (uid, token) VALUES (%s, %s)", (uid, token))
connection.commit()

# Close the connection and the cursor
cursor.close()
connection.close()

return { "statusCode": 200, "body": json.dumps({ "token": token }) }
1 change: 1 addition & 0 deletions Lambda_Functions/sign_in/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
psycopg2-binary
13 changes: 13 additions & 0 deletions Lambda_Functions/sign_out/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM public.ecr.aws/lambda/python:3.12

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install -r requirements.txt

# Copy function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.handler" ]
Binary file not shown.
5 changes: 5 additions & 0 deletions Lambda_Functions/sign_out/buildscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
export num=1.0
docker build -t techfest-signout:$num .
docker tag techfest-signout:$num 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-signout:$num
docker push 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-signout:$num
38 changes: 38 additions & 0 deletions Lambda_Functions/sign_out/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import psycopg2
import json
import os

def handler(event, context):

if 'body' not in event:
return { "statusCode": 400, "body": json.dumps("Bad request") }

connection = psycopg2.connect(
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT'),
sslmode='require'
)
cursor = connection.cursor()

# Retrieve user data from the request body
body = json.loads(event['body'])

if 'token' not in body:
return { "statusCode": 400, "body": json.dumps("Bad request") }

token = body['token']

# delete record from current_sign_in where token = token
cursor.execute(f"DELETE FROM current_sign_in WHERE token = '{token}'")

connection.commit()
cursor.close()
connection.close()

return {
"statusCode": 200,
"body": json.dumps("Logged out successfully")
}
1 change: 1 addition & 0 deletions Lambda_Functions/sign_out/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
psycopg2-binary
13 changes: 13 additions & 0 deletions Lambda_Functions/sign_up/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM public.ecr.aws/lambda/python:3.12

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install -r requirements.txt

# Copy function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.handler" ]
Binary file not shown.
5 changes: 5 additions & 0 deletions Lambda_Functions/sign_up/buildscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
export num=1.1
docker build -t techfest-signup:$num .
docker tag techfest-signup:$num 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-signup:$num
docker push 020308349020.dkr.ecr.ap-south-1.amazonaws.com/techfest-signup:$num
Loading

0 comments on commit cc8dcfb

Please sign in to comment.