diff --git a/api1.py b/api1.py index 3193569..6669cef 100644 --- a/api1.py +++ b/api1.py @@ -1,6 +1,6 @@ import flask import sys -from flask import request, jsonify, g +from flask import request, jsonify, g, make_response from flask_apscheduler import APScheduler from datetime import datetime,timedelta,timezone import sqlite3 @@ -69,7 +69,11 @@ def sendMail(topic,toAddr): server=smtplib.SMTP("localhost", port) sender_email = "deadswitch@kawomi.com" receiver_email = toAddr - message = f"""Subject: deadSwitch not pushed on topic {topic}\n\nNotification of deadSwitch not pushed on topic {topic}.""" + message = f"""Subject: deadSwitch not pushed on topic {topic}\n\nNotification of deadSwitch not pushed on topic {topic}. + + https://deadswitch.kawomi.com/api/v1?topic={topic} + + """ sendSelfTelegram(message) server.sendmail(sender_email, receiver_email, message) @@ -122,6 +126,14 @@ if __name__ == '__main__': """ + @app.route('/api/v1', methods=['OPTIONS']) + def api_returnOptions(): + response = make_response() + response.headers.add("Access-Control-Allow-Origin", "*") + response.headers.add('Access-Control-Allow-Headers', "*") + response.headers.add('Access-Control-Allow-Methods', "*") + return response + @app.route('/api/v1', methods=['GET']) def api_getByTopic(): if 'topic' in request.args: @@ -135,7 +147,7 @@ if __name__ == '__main__': if theRow is None: return jsonify({'success':False, 'error':f"No entry found for topic _{topic}_"}) - return jsonify({'success':True, 'topic':topic, 'active':bool(theRow[0]), 'expiry':theRow[1].strftime("%Y-%m-%d %H:%M:%S"), 'lastSeen':theRow[5].strftime("%Y-%m-%d %H:%M:%S"),'isExpired':bool(theRow[4]),'email':theRow[2], 'isNotified':bool(theRow[3])}) + return _corsify_actual_response(jsonify({'success':True, 'topic':topic, 'active':bool(theRow[0]), 'expiry':theRow[1].strftime("%Y-%m-%d %H:%M:%S"), 'lastSeen':theRow[5].strftime("%Y-%m-%d %H:%M:%S"),'isExpired':bool(theRow[4]),'email':theRow[2], 'isNotified':bool(theRow[3])})) @app.route('/api/v1', methods=['PUT','POST']) def api_setRecord(): @@ -166,4 +178,8 @@ if __name__ == '__main__': return jsonify({'success':True, 'expiry':expiryDateTime.strftime("%Y-%m-%d %H:%M:%S"), 'email':email}) + def _corsify_actual_response(response): + response.headers.add("Access-Control-Allow-Origin", "*") + return response + app.run(use_reloader=False,port=5123)