-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
72 lines (56 loc) · 1.94 KB
/
lambda_function.py
File metadata and controls
72 lines (56 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import sys
import pymysql
import logging
import json
host = os.environ["DB_HOST"]
user = os.environ["DB_USERNAME"]
password = os.environ["DB_PASSWORD"]
db = os.environ["DB_NAME"]
logger = logging.getLogger()
logger.setLevel(logging.INFO)
try:
conn = pymysql.connect(host=host, user=user, passwd=password, db=db, connect_timeout=5)
except pymysql.MySQLError as e:
logger.error("연결 실패!")
logger.error(e)
sys.exit()
logger.info("연결 성공!")
def returnHeaders(self=""):
return {
"Access-Control-Allow-Headers":
"Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Methods":
"DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT",
"Access-Control-Allow-Origin":
"*"
}
def lambda_handler(event, context):
if event["httpMethod"] == "GET":
high = event['queryStringParameters']['high']
low = event['queryStringParameters']['low']
cur = conn.cursor()
sql_string = f"select singer, song, highest_pitch, lowest_pitch, youtube_url, youtube_image, youtube_listen_url \
from music where highest_pitch <= {high} and lowest_pitch >= {low} \
order by highest_pitch desc, lowest_pitch asc;"
cur.execute(sql_string)
rows = cur.fetchall()
result = []
logger.info(rows)
for row in rows:
result.append({
"singer": row[0],
"song": row[1],
"high": row[2],
"low": row[3],
"image": row[5],
"url" : {
"listen" : row[4],
"sing" : row[6]
}
})
return {
"statusCode": 200,
"body": json.dumps(result),
"headers": returnHeaders(),
}