Skip to content

Instantly share code, notes, and snippets.

@jallum
Created January 20, 2021 15:20
Show Gist options
  • Save jallum/777a40ed7590b570e55e389a216c7a19 to your computer and use it in GitHub Desktop.
Save jallum/777a40ed7590b570e55e389a216c7a19 to your computer and use it in GitHub Desktop.
from flask import Flask
from flask import request
from flask import Response
from flask import send_file
from flask import abort
import json
import os
app = Flask(__name__)
MACHINE_ID = os.environ.get('BALENA_DEVICE_UUID', 'local-machine')
LOG_FILE_PATH = os.environ.get('LOG_FILE_PATH', 'log/analytics.json')
#print(LOG_FILE_PATH)
#GET DIRECTORY OF FILEPATH (IN OS LIBRARY
directory = os.path.dirname(LOG_FILE_PATH)
os.makedirs(directory, exist_ok = True) #if directory does not exist make one
@app.route('/', methods = ['GET','POST'])
def Log():
if request.method == 'POST':
data = request.get_json(force = True)
data['machine_id'] = MACHINE_ID
with open(LOG_FILE_PATH, 'a') as file:
file.write(json.dumps(data) + '\n')
return Response()
elif request.method == 'GET':
return Response()
@app.route('/getfile', methods=['GET'])
def Send():
try:
return send_file(LOG_FILE_PATH)
except OSError as e:
return '' #abort(404) <--
#this allows us to run our app
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment