Mercurial > personal > weather-server
annotate weather_server/server.py @ 4:e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 29 Sep 2019 15:10:18 -0400 |
parents | b42c4bfe57c7 |
children | 368f732f13d0 |
rev | line source |
---|---|
4
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
1 import hmac |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
2 |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
3 import bson |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
4 import flask |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
5 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 from . import common |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 from . import locations |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
8 from . import types |
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 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 def build_app(root_directory: str) -> flask.Flask: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
12 locs = locations.Locations(root_directory) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
13 app = flask.Flask(__name__) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
14 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
15 @app.route('/favicon.ico') |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
16 def favicon(): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
17 return flask.send_file('static/favicon.ico') |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
18 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
19 @app.route('/') |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
20 def home(): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
21 return 'Weather server' |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
22 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
23 @app.route('/_submit', methods=['POST']) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
24 def submit(): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
25 req = flask.request |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 reader = bson.decode_file_iter( |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 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
|
28 try: |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
29 preamble = next(reader) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
30 loc_name = preamble['location'] |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
31 password = str(preamble['password']) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
32 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
|
33 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
|
34 flask.abort(400) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
35 entries = [ |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
36 types.Reading.from_now( |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 ) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
41 for item in reader |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
42 ] |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
43 except (KeyError, bson.InvalidBSON): |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
44 flask.abort(400) |
2
cda47993a193
server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
45 logger.write_rows(entries) |
cda47993a193
server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
46 return flask.jsonify({'status': 'OK'}) |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
47 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
48 @app.route('/<location>') |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
49 def show(location): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
50 try: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
51 loc, logger = locs.get(location) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
52 except KeyError: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
53 flask.abort(404) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
54 data = logger.data |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
55 if data: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 last_reading = data[-1] |
2
cda47993a193
server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
57 tz = loc.timezone() |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
58 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
|
59 else: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
60 last_reading = None |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 date = None |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 return flask.render_template( |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 'location.html', |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 location=loc, |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 last_reading=last_reading, |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 date=date) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
68 return app |
4
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
69 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
70 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
71 def main(argv): |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
72 """Main function for a simple local demo.""" |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
73 app = server.build_app(argv[0]) |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
74 app.run(host='0.0.0.0') |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
75 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
76 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
77 if __name__ == '__main__': |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
78 main(sys.argv[1:]) |