-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
72 lines (59 loc) · 1.92 KB
/
server.py
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
#imports
import os
import sqlite3
from flask import Flask, request, g
import json
from point import Point
from database import Database
import json
app = Flask(__name__)
app.config.from_object(__name__) # load config from this file ****
# Load default config and override config from an environment variable
app.config.update(dict(
DATABASE=os.path.join(app.root_path, 'database.db'),
SECRET_KEY='development key',
USERNAME='admin',
PASSWORD='default'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def get_sqlite():
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
db = Database()
# Get messages near the (lat, lon)
# Ex. curl localhost:5000/messages?lat=123,lon=456
@app.route('/messages', methods=['GET'])
def getPoints():
# We should sanity check here and return error if invalid
lat = float(request.args.get('lat', 0))
lon = float(request.args.get('lon', 0))
print('[Server] Getting messages for (' + str(lat) + ', ' + str(lon) + ')')
return json.dumps(db.query(get_sqlite(), lat, lon))
# Create a new message at the location
@app.route('/messages', methods=['POST'])
def createPoint():
# We should sanity check the JSON here before adding to the database
print(request.json)
db.create(get_sqlite(), request.json['lat'], request.json['lon'], request.json['message'])
return 'Successfully created message'
# Test to see if the server is up
@app.route('/ping', methods=['GET'])
def ping():
return 'Pong! :)'
@app.teardown_appcontext
def close_db(error):
db.close_db()
@app.cli.command('initdb')
def initdb_command():
with app.open_resource('schema.sql', mode='r') as f:
db = get_sqlite()
db.cursor().executescript(f.read())
db.commit()
print('Initialized the database.')
# @app.route('/')
# def show_entries():
# return str(db.show_entries(get_sqlite()))
# @app.route('/add', methods=['POST'])
# def add_entry():
# db.add_entry()