diff --git a/.gitignore b/.gitignore index 5a5900c..c896d55 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea -test/ \ No newline at end of file +src/test/ +src/config/api_config.yaml \ No newline at end of file diff --git a/requirments.txt b/requirments.txt new file mode 100644 index 0000000..d21d4b9 --- /dev/null +++ b/requirments.txt @@ -0,0 +1,3 @@ +flask +yaml +elasticsearch==7.14 diff --git a/src/config/example_config.yaml b/src/config/example_config.yaml new file mode 100644 index 0000000..344ff53 --- /dev/null +++ b/src/config/example_config.yaml @@ -0,0 +1,2 @@ +elasticsearch: + host: http://IP:PORT diff --git a/src/logic/Elasticsearch_handler.py b/src/logic/Elasticsearch_handler.py new file mode 100644 index 0000000..6580e7f --- /dev/null +++ b/src/logic/Elasticsearch_handler.py @@ -0,0 +1,14 @@ +from datetime import datetime, date +from elasticsearch import Elasticsearch +import json + + +def es_upload(server, index, log_message): + if type(log_message) is not dict: + log_message = {"steam_key": log_message} + log_message["timestamp"] = datetime.utcnow().isoformat() + es = Elasticsearch(server) + data = json.dumps(log_message) + response = es.index(index=index, body=data, headers={'Content-Type': 'application/json;charset=UTF-8'}) + document_id = response['_id'] + print("Uploaded dictionary with ID:", document_id) diff --git a/src/logic/handler.py b/src/logic/handler.py deleted file mode 100644 index b0cc26d..0000000 --- a/src/logic/handler.py +++ /dev/null @@ -1,5 +0,0 @@ - -def post_handler(data): - print("Post respond:") - print(data) - print("END OF RESPOND") \ No newline at end of file diff --git a/src/start_app.py b/src/start_app.py index bc8cd77..40c4cd2 100644 --- a/src/start_app.py +++ b/src/start_app.py @@ -1,34 +1,51 @@ +""" + +""" +# ------------------------------------------------------- # +# imports +# ------------------------------------------------------- # from flask import Flask, send_from_directory, request, jsonify from threading import Thread -from logic.handler import post_handler import os +import yaml + +from logic.Elasticsearch_handler import es_upload + + +# ------------------------------------------------------- # +# functions +# ------------------------------------------------------- # +def load_config(): + with open('config//api_config.yaml', 'r') as f: + config_file = yaml.safe_load(f) + return config_file + app = Flask(__name__) +@app.route("/") +def root(): + return "

Development Server for Death Garden API!

" + + @app.route('/favicon.ico') def favicon(): return send_from_directory(os.path.join(app.root_path, 'image'), 'favicon.ico', mimetype='image/vnd.microsoft.icon') -@app.route("/") -def hello_world(): - return "

Development Server for Death Garden API!

" - - -@app.route("/api/v1/services/tex") -def one(): - print("Responded to tex api call GET") - return jsonify({"status": "success", "online": "true", "Version": "te-18f25613-36778-ue4-374f864b", - "ProjectID": "F72FA5E64FA43E878DC72896AD677FB5", - "DefaultFactoryName": "HttpNetworkReplayStreaming", "ServeMatchDelayInMin": "30.0f"}) - - @app.route("/metrics/client/event", methods=["POST"]) def receive_event(): print("Responded to Metrics api call POST") data = request.get_json() - post_handler(data) + es_upload(server=es_server, index="client_event", log_message=data) + return jsonify({"status": "success"}) + + +@app.route("/metrics/httplog/event", methods=["POST"]) +def metrics_httplog_event(): + data = request.get_json() + es_upload(server=es_server, index="metrics_httplog_event", log_message=data) return jsonify({"status": "success"}) @@ -37,19 +54,19 @@ def healthcheck(): return jsonify({"status": "success", "online": "true"}) -@app.route("/metrics/httplog/event", methods=["POST"]) -def receive_event2(): - print("Responded to httplog api call POST") - data = request.get_json() - post_handler(data) - return jsonify({"status": "success"}) +@app.route("/api/v1/services/tex") +def services_tex(): + print("Responded to tex api call GET") + return jsonify({"status": "success", "online": "true", "Version": "te-18f25613-36778-ue4-374f864b", + "ProjectID": "F72FA5E64FA43E878DC72896AD677FB5", + "DefaultFactoryName": "HttpNetworkReplayStreaming", "ServeMatchDelayInMin": "30.0f"}) @app.route("/api/v1/gameDataAnalytics", methods=["POST"]) def analytics_post(): print("Responded to analytics api call POST") data = request.get_json() - post_handler(data) + es_upload(server=es_server, index="game_data_analytics", log_message=data) return jsonify({"status": "success"}) @@ -57,25 +74,24 @@ def analytics_post(): def analytics_batch_post(): print("Responded to analytics batch api call POST") data = request.get_json() - post_handler(data) + es_upload(server=es_server, index="game_data_analytics_batch", log_message=data) return jsonify({"status": "success"}) +@app.route("/api/v1/auth/provider/steam/login", methods=["POST"]) +def steam_login(): + steam_token = request.args.get('token') + es_upload(server=es_server, index="steam_login", log_message=steam_token) + # return jsonify({"clientData":{"catalogId": "3.6.0_281460live", "consentId": "3.6.0_281460live"}}) + return jsonify({"clientData": "3.0.1"}) + + # [Backend] @app.route("/tex", methods=["GET"]) def tex_get(): return jsonify({"status": "success"}) -@app.route("/api/v1/auth/provider/steam/login", methods=["POST"]) -def steam_login(): - # here we want to get the value of user (i.e. ?user=some-value) - steam_token = request.args.get('token') - print(steam_token) - # return jsonify({"clientData":{"catalogId": "3.6.0_281460live", "consentId": "3.6.0_281460live"}}) - return jsonify({"clientData": "3.0.1"}) - - def run(): app.run(host='0.0.0.0', port=8080) @@ -86,8 +102,17 @@ def keep_alive(): t.start() -try: - keep_alive() -except KeyboardInterrupt: - print("Exiting...") - exit(0) +# ------------------------------------------------------- # +# global variables +# ------------------------------------------------------- # + +config = load_config() +es_server = config['elasticsearch']['host'] + + +# ------------------------------------------------------- # +# main +# ------------------------------------------------------- # +keep_alive() +print("Exiting...") +exit(0)