forked from Reecepbcups/cosmos-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.py
130 lines (100 loc) · 3.39 KB
/
rest.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Reece Williams | https://reece.sh | Jan 2023
import json
from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin
import CONFIG as CONFIG
from CONFIG import KV_STORE
from HELPERS import (
Mode,
download_openapi_locally,
get_config_values,
get_stats_html,
get_swagger_code_from_source,
increment_call_value,
ttl_block_only,
)
from HELPERS_TYPES import CallType
from RequestsHandler import RestApiHandler
app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
REST_SWAGGER_HTML = ""
REST_HANDLER: RestApiHandler
@app.before_first_request
def before_first_request():
global REST_HANDLER
CONFIG.update_cache_times()
download_openapi_locally()
REST_HANDLER = RestApiHandler()
# not used yet here
# if len(CONFIG.RPC_WEBSOCKET) > 0:
# tmrpc = TendermintRPCWebSocket(enableSignal=False, logLevel=logging.DEBUG)
# t = threading.Thread(target=tmrpc.ws.run_forever)
# t.daemon = True
# t.start()
@app.route("/", methods=["GET"])
@cross_origin()
def root():
global REST_SWAGGER_HTML
if CONFIG.DISABLE_SWAGGER_UI:
return jsonify({"code": 12, "message": "Not Implemented", "details": []})
if len(REST_SWAGGER_HTML) > 0:
return REST_SWAGGER_HTML
REST_SWAGGER_HTML = get_swagger_code_from_source()
return REST_SWAGGER_HTML
@app.route("/<path:path>", methods=["GET"])
@cross_origin()
def get_rest(path):
if path == "stats":
# https://url/stats?password=123
if (
len(CONFIG.STATS_PASSWORD) > 0
and request.args.get("password") != CONFIG.STATS_PASSWORD
):
return "Invalid password"
return get_stats_html()
if path == "config":
# https://url/config?password=123
if (
len(CONFIG.STATS_PASSWORD) > 0
and request.args.get("password") != CONFIG.STATS_PASSWORD
):
return "Invalid password"
return get_config_values()
if path == "debug":
return jsonify(KV_STORE.to_json())
args = request.args
cache_seconds = CONFIG.get_cache_time_seconds(path, is_rpc=False)
if cache_seconds == Mode.DISABLED.value:
return jsonify(
{
"error": f"cosmos endpoint cache: The path '{path}' is disabled on this node..."
}
)
# print(cache_seconds)
# Every rest requests is an hset because of diff arguments
key = f"rest;{ttl_block_only(cache_seconds)};{path};"
v = KV_STORE.hget(key, str(args))
if CONFIG.DEBUGGING:
print(f"get_rest. Key: {key} | value: {v}")
if v:
increment_call_value(CallType.REST_GET_CACHE.value)
return jsonify(json.loads(v))
return jsonify(
REST_HANDLER.handle_single_rest_get_requests(
path, key, cache_seconds, args, request.headers
)
)
@app.route("/<path:path>", methods=["POST"])
@cross_origin()
def post_rest(path):
# REQ_DATA = json.loads(json.dumps(request.get_json(), separators=(",", ":")))
# print(type(REQ_DATA))
# return jsonify(REST_HANDLER.handle_single_rest_post_requests(path, REQ_DATA))
return jsonify(
{
"error": f"cosmos endpoint cache: The path '{path}' does not yet have support on this REST API..."
}
)
if __name__ == "__main__":
before_first_request()
app.run(debug=True, host="0.0.0.0", port=CONFIG.REST_PORT)