Last active
August 29, 2015 14:02
-
-
Save leviyehonatan/78aaf4ed58a39e9a37ab to your computer and use it in GitHub Desktop.
some location logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo | |
import json | |
import os | |
from datetime import datetime | |
from itertools import chain | |
from pprint import pprint | |
from flask import Flask, session, request, abort, make_response | |
app = Flask(__name__) | |
connection = pymongo.MongoClient("localhost", 27017) | |
# database | |
lazooz_db = connection.lazooz | |
locations = lazooz_db.locations | |
users = lazooz_db.users | |
lazooz_log = lazooz_db.log | |
def log(type, what): | |
lazooz_log.insert({'type': type, 'what': what, 'timestamp': datetime.now()}) | |
print("{timestamp} {type}: {what}".format(timestamp=datetime.now(), type=type, what=what)) | |
@app.route('/register') | |
def register(): | |
# decipher post data | |
public_key = request.args['public_key'] | |
print("/register with public_key={public_key}".format(public_key=public_key)) | |
# validate public key | |
if public_key is None: | |
# bad request | |
abort(401); | |
#create session | |
session['public_key'] = public_key | |
log('session-created', 'public key: {}'.format(session['public_key'])) | |
if not users.find({"public_key": session['public_key']}): | |
users.insert({"public_key": session['public_key'], "createdAt": datetime.now()}) | |
log('user-created', 'public key: {}'.format(session['public_key'])) | |
return '' | |
@app.route('/location', methods=['POST']) | |
def post_location(): | |
print(request.cookies) | |
print(session) | |
if session['public_key'] is None: | |
return 'renew-session' | |
posted_locations = json.loads(request.data.decode()) | |
print(posted_locations) | |
for location in posted_locations: | |
location.public_key = session['public_key'] | |
locations.save(location) | |
return None | |
if __name__ == '__main__': | |
app.secret_key = '\x17\x96e\x94]\xa0\xb8\x1e\x8b\xee\xdd\xe9\x91^\x9c\xda\x94\t\xe8S\xa1Oe_' #os.urandom(24) | |
app.run(host='0.0.0.0', debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment