annotate weather_server/server.py @ 3:b42c4bfe57c7

server: Use a "preamble" object in the POST to auth.
author Paul Fisher <paul@pfish.zone>
date Sun, 29 Sep 2019 11:52:48 -0400
parents cda47993a193
children e7c8dcc5fc15
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 import bson
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2 import flask
3
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
3 import hmac
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5 from . import common
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 from . import locations
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7 from . import types
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 def build_app(root_directory: str) -> flask.Flask:
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 locs = locations.Locations(root_directory)
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12 app = flask.Flask(__name__)
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 @app.route('/favicon.ico')
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 def favicon():
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 return flask.send_file('static/favicon.ico')
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 @app.route('/')
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 def home():
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 return 'Weather server'
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 @app.route('/_submit', methods=['POST'])
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 def submit():
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 req = flask.request
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 reader = bson.decode_file_iter(
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 req.stream, codec_options=common.BSON_OPTIONS)
3
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
27 try:
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
28 preamble = next(reader)
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
29 loc_name = preamble['location']
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
30 password = str(preamble['password'])
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
31 loc, logger = locs.get(loc_name)
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
32 if not hmac.compare_digest(password, loc.password):
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
33 flask.abort(400)
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
34 entries = [
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
35 types.Reading.from_now(
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
36 sample_time=item['sample_time'],
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
37 temp_c=item['temp_c'],
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
38 rh_pct=item['rh_pct'],
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
39 )
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
40 for item in reader
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
41 ]
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
42 except (KeyError, bson.InvalidBSON):
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
43 flask.abort(400)
2
cda47993a193 server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents: 1
diff changeset
44 logger.write_rows(entries)
cda47993a193 server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents: 1
diff changeset
45 return flask.jsonify({'status': 'OK'})
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 @app.route('/<location>')
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 def show(location):
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
49 try:
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 loc, logger = locs.get(location)
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 except KeyError:
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 flask.abort(404)
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
53 data = logger.data
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 if data:
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 last_reading = data[-1]
2
cda47993a193 server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents: 1
diff changeset
56 tz = loc.timezone()
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 date = tz.normalize(last_reading.sample_time.astimezone(tz))
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 else:
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 last_reading = None
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 date = None
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 return flask.render_template(
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 'location.html',
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 location=loc,
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 last_reading=last_reading,
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 date=date)
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 return app