Mercurial > personal > weather-server
annotate weather_server/server.py @ 21:beb42c835c52
Make weather server handle arbitrary data:
- Make logfile record arbitrary BSONs
- Make server handlers OK with same
- Make location type a normal class rather than attrs;
have it handle its own logger.
- Bump version number.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sat, 19 Oct 2019 18:40:48 -0400 |
parents | 9a609bcf0809 |
children | 88249e451566 |
rev | line source |
---|---|
11
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
1 import datetime |
4
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
2 import hmac |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
3 import pathlib |
5
368f732f13d0
Actually make the server runnable.
Paul Fisher <paul@pfish.zone>
parents:
4
diff
changeset
|
4 import sys |
4
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
5 |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 import bson |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 import flask |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
8 import pytz |
1
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 from . import common |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 from . import locations |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
12 |
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 def build_app(root_directory: str) -> flask.Flask: |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
15 locs = locations.LocationFolder(pathlib.Path(root_directory)) |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
16 app = flask.Flask(__name__) |
8
d54155a199d8
Improve template; add stylesheet.
Paul Fisher <paul@pfish.zone>
parents:
5
diff
changeset
|
17 app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 |
1
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('/favicon.ico') |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
20 def favicon(): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
21 return flask.send_file('static/favicon.ico') |
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('/') |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
24 def home(): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
25 return 'Weather server' |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 @app.route('/_submit', methods=['POST']) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
28 def submit(): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
29 req = flask.request |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
30 reader = bson.decode_file_iter( |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
31 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
|
32 try: |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
33 preamble = next(reader) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
34 loc_name = preamble['location'] |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
35 password = str(preamble['password']) |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
36 loc = locs.get(loc_name) |
3
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
37 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
|
38 flask.abort(400) |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
39 entries = tuple(reader) |
3
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
40 except (KeyError, bson.InvalidBSON): |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
41 flask.abort(400) |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
42 now = datetime.datetime.now(tz=pytz.UTC) |
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
43 loc.record(entries, now) |
2
cda47993a193
server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
44 return flask.jsonify({'status': 'OK'}) |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
45 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
46 @app.route('/<location>') |
11
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
47 def show(location: str): |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
48 try: |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
49 loc = locs.get(location) |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
50 except KeyError: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
51 flask.abort(404) |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
52 last_reading = loc.latest() |
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
53 if last_reading: |
2
cda47993a193
server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
54 tz = loc.timezone() |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
55 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
|
56 else: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
57 date = None |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
58 return flask.render_template( |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
59 'location.html', |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
60 location=loc, |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 last_reading=last_reading, |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 date=date) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 |
11
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
64 @app.route('/<location>/recent') |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
65 def recent(location: str): |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
66 try: |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
67 loc = locs.get(location) |
11
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
68 except KeyError: |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
69 flask.abort(404) |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
70 req = flask.request |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
71 |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
72 try: |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
73 seconds = int(req.args['seconds']) |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
74 except (KeyError, ValueError): |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
75 flask.abort(400) |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
76 |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
77 start = common.utc_now() - datetime.timedelta(seconds=seconds) |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
78 |
21
beb42c835c52
Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents:
16
diff
changeset
|
79 readings = [r for r in loc.entries if start < r['sample_time']] |
11
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
80 resp = flask.Response() |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
81 resp.content_type = 'application/json' |
16
9a609bcf0809
Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
82 resp.data = common.json_dumps({ |
9a609bcf0809
Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
83 'timezone': loc.tz_name, |
9a609bcf0809
Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
84 'readings': readings, |
9a609bcf0809
Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
85 }) |
11
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
86 return resp |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
87 |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
88 return app |
4
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
89 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
90 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
91 def main(argv): |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
92 """Main function for a simple local demo.""" |
5
368f732f13d0
Actually make the server runnable.
Paul Fisher <paul@pfish.zone>
parents:
4
diff
changeset
|
93 app = build_app(argv[0]) |
4
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
94 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
|
95 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
96 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
97 if __name__ == '__main__': |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
98 main(sys.argv[1:]) |