Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Reecepbcups/python-rpc-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Feb 21, 2023
2 parents c419c93 + 9072a86 commit 4830912
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
11 changes: 11 additions & 0 deletions CONFIG.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


load_dotenv(env_file)
USE_BACKUP = getenv("USE_BACKUP", "false").lower().startswith("t")

## == Helper == ##
REMOTE_CONFIG_TIME_FILE = getenv("REMOTE_CONFIG_TIME_FILE", "")
Expand Down Expand Up @@ -89,10 +90,18 @@ def get_config_file(filename: str):
RPC_PORT = int(getenv("RPC_PORT", 5001))
RPC_PREFIX = getenv("REDIS_RPC_PREFIX", "junorpc")


RPC_URL = getenv("RPC_URL", "https://juno-rpc.reece.sh:443")
BACKUP_RPC_URL = getenv("BACKUP_RPC_URL", "https://rpc.juno.strange.love:443")
if USE_BACKUP:
RPC_URL = BACKUP_RPC_URL

RPC_WEBSOCKET = getenv("RPC_WEBSOCKET", "ws://15.204.143.232:26657/websocket")
BACKUP_RPC_WEBSOCKET = getenv(
"BACKUP_RPC_WEBSOCKET", "ws://rpc.juno.strange.love:443/websocket"
)
if USE_BACKUP:
RPC_WEBSOCKET = BACKUP_RPC_WEBSOCKET

# ============
# === REST ===
Expand All @@ -104,6 +113,8 @@ def get_config_file(filename: str):

REST_URL = getenv("REST_URL", "https://juno-rest.reece.sh")
BACKUP_REST_URL = getenv("BACKUP_REST_URL", f"https://api.juno.strange.love")
if USE_BACKUP:
REST_URL = BACKUP_REST_URL

OPEN_API = f"{REST_URL}/static/openapi.yml"

Expand Down
17 changes: 15 additions & 2 deletions HELPERS.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import re
from os import getenv

import CONFIG
import httpx

import CONFIG
from CONFIG import REDIS_DB
from HELPERS_TYPES import CallType, Mode

Expand Down Expand Up @@ -112,8 +114,19 @@ def replace_rpc_text() -> str:
CLOSING_HTML = """</body></html>"""


def get_stats_html():
def get_config_values():
KVs = [item for item in dir(CONFIG) if not item.startswith("__")]
items = {item: getattr(CONFIG, item) for item in KVs}

return f"""
{INITIAL_HTML}
<h2>Config Values</h2>
<p>{items}</p>
{CLOSING_HTML}
"""


def get_stats_html():
updates_every = CONFIG.INC_EVERY

# gets information about the redis
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ This makes it possible to run on cloud providers like Akash, AWS, GCP, Azure, et

```sh
# System
sudo apt install redis-server
sudo apt install redis-server python3-pip

sudo pacman -Sy redis-server

Expand Down
4 changes: 3 additions & 1 deletion configs/.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ STATS_PASSWORD="" # blank = no password for https://network.rest.website.com/sta
# ====================================
# = RPCS & REST ENDPOINTS (TO QUERY) =
# ====================================

USE_BACKUP=true
# Note: RPC_URL can be localhost if you run on the machine itself. Or a direct IP address & port.
RPC_URL="http://127.0.0.1:26657"
BACKUP_RPC_URL="https://rpc.juno.strange.love"

# set to "" if you do not wish to use the websocket
RPC_WEBSOCKET="ws://15.204.143.232:26657/websocket"
BACKUP_RPC_WEBSOCKET="ws://rpc.juno.strange.love:443/websocket"

# REST API
REST_URL="http://127.0.0.1:1317"
BACKUP_REST_URL="https://api.juno.strange.love"
DISABLE_SWAGGER_UI=false


# === Security ===
# Hides value in the /status endpoint of the RPC
# "" = normal value shown on query - https://youtu.be/5MKV7EDJiS4
Expand Down
6 changes: 3 additions & 3 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ flask-cors
uwsgi
gunicorn

# May not need anymore
Werkzeug==0.14.1
uWSGI==2.0.17.1
# breaks older python installs. Is this needed for docker?
# Werkzeug==0.14.1
# uWSGI==2.0.17.1

pycoingecko
16 changes: 14 additions & 2 deletions rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import json

import CONFIG as CONFIG
from CONFIG import REDIS_DB
from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin

import CONFIG as CONFIG
from CONFIG import REDIS_DB
from HELPERS import (
Mode,
download_openapi_locally,
get_config_values,
get_stats_html,
get_swagger_code_from_source,
increment_call_value,
Expand Down Expand Up @@ -68,6 +70,16 @@ def get_rest(path):

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()

args = request.args
headers = request.headers

Expand Down

0 comments on commit 4830912

Please sign in to comment.