service.py 1.06 KB
Newer Older
Nane Kratzke's avatar
Nane Kratzke committed
1
from flask import Flask, redirect, request, jsonify
Nane Kratzke's avatar
Nane Kratzke committed
2
import requests, os, json, logging
3
from elasticapm.contrib.flask import ElasticAPM
Nane Kratzke's avatar
Nane Kratzke committed
4

Nane Kratzke's avatar
Nane Kratzke committed
5
6
7
8
9
10
11
12
13
14
15
# A very boring and basic space-service to demonstrate 
# observability via tracing.

app = Flask(__name__)

@app.route('/')
def index():
    return redirect('/space')

@app.route('/space')
def space():
16
17
18
19
    try:
        lat = float(request.args.get('lat', "0.0001"))
        lon = float(request.args.get('lon', "0.0001"))
    except Exception as ex:
Nane Kratzke's avatar
Nane Kratzke committed
20
        logging.error(f"Bad request ({ ex })")
21
22
23
24
25
26
27
28
        return "Bad request (lat/lon not numeric)", 400
    
    queries = {
        "astros": "http://api.open-notify.org/astros.json",
        "position": "http://api.open-notify.org/iss-now.json",
        "passtime": f"http://api.open-notify.org/iss-pass.json?lat={ lat }&lon={ lon }"
    }

Nane Kratzke's avatar
Nane Kratzke committed
29
30
31
32
    result = {}
    for key, url in queries.items():
        logging.info(f"Fetching { url }")
        result[key] = json.loads(requests.get(url).content)
Nane Kratzke's avatar
Nane Kratzke committed
33

Nane Kratzke's avatar
Nane Kratzke committed
34
    logging.info(json.dumps(result))
Nane Kratzke's avatar
Nane Kratzke committed
35
36
    return jsonify(result)

37
app.run(host="0.0.0.0", port=80)