diff --git a/.gitignore b/.gitignore index af1c9c4..d2bd107 100644 --- a/.gitignore +++ b/.gitignore @@ -129,5 +129,4 @@ dmypy.json .pyre/ # nodes -config.py data diff --git a/Makefile b/Makefile index 6eb4e77..1a296a2 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,20 @@ setup: python3 -m venv .venv .venv/bin/pip install -r requirements.txt - mkdir data - wget https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-City.mmdb -P data + mkdir -p data + wget https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-City.mmdb -P data --no-clobber up: docker-compose up -d dev: - ./bin/dev + ./manage.sh run prod: - ./bin/prod + ./manage.sh prod logs: docker-compose logs -f + +kill: + pkill -ef xmrnodes \ No newline at end of file diff --git a/README.md b/README.md index e253c0b..0b44c16 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,10 @@ The map portion of the service requires the GeoLite2 db...the `make setup` comma # install python virtual environment and install application dependencies make setup -# setup config -cp xmrnodes/config.example.py xmrnodes/config.py -vim xmrnodes/config.py +# default configs work out of the box, modify .env if needed +# setup .env +cp env-example .env +vim .env # run services (tor, i2p, etc) make up @@ -32,3 +33,15 @@ make dev # access at http://127.0.0.1:5000 ``` + +### Production + +For production, update `SERVER_NAME` in `.env` to your production URL/domain. Use `manage.sh` (or provided `Makefile`) to serve the Flask process using Gunicorn. + +``` +./manage.sh prod +``` + +Runs the Gunicorn process on port 4000. Setup a web server to proxy requests to that port. + +Kill production Gunicorn with `make kill`. diff --git a/bin/cmd b/bin/cmd deleted file mode 100755 index 2d248f4..0000000 --- a/bin/cmd +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -source .venv/bin/activate -export FLASK_APP=xmrnodes/app.py -export FLASK_SECRETS=config.py -export FLASK_DEBUG=1 -flask $1 diff --git a/bin/dev b/bin/dev deleted file mode 100755 index 2389ad5..0000000 --- a/bin/dev +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -source .venv/bin/activate -export FLASK_APP=xmrnodes/app.py -export FLASK_SECRETS=config.py -export FLASK_DEBUG=1 -flask run diff --git a/bin/prod b/bin/prod deleted file mode 100755 index 3319b65..0000000 --- a/bin/prod +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -BASE=data/gunicorn - -source .venv/bin/activate -export FLASK_APP=xmrnodes/app.py -export FLASK_SECRETS=config.py -export FLASK_DEBUG=0 -export FLASK_ENV=production - -mkdir -p $BASE - -gunicorn \ - --bind 127.0.0.1:4000 "xmrnodes.app:app" \ - --daemon \ - --log-file $BASE/gunicorn.log \ - --pid $BASE/gunicorn.pid \ - --access-logfile $BASE/access.log \ - --reload - -echo "Starting gunicorn with pid $(cat $BASE/gunicorn.pid)" diff --git a/env-example b/env-example new file mode 100644 index 0000000..43de794 --- /dev/null +++ b/env-example @@ -0,0 +1,7 @@ +SECRET_KEY=randomstringyoumakeup +SERVER_NAME=127.0.0.1:5000 +DATA_DIR=./data +TOR_HOST=127.0.0.1 +TOR_PORT=9050 +NODE_HOST=singapore.node.xmr.pm +NODE_PORT=18080 \ No newline at end of file diff --git a/manage.sh b/manage.sh new file mode 100755 index 0000000..6ff3f24 --- /dev/null +++ b/manage.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +source .venv/bin/activate +export FLASK_APP=xmrnodes/app.py +export FLASK_SECRETS=config.py +export FLASK_DEBUG=1 +export FLASK_ENV=development + +# override +source .env + +if [[ ${1} == "prod" ]]; +then + export FLASK_DEBUG=0 + export FLASK_ENV=production + export BASE=./data/gunicorn + mkdir -p $BASE + pgrep -F $BASE/gunicorn.pid + if [[ $? != 0 ]]; then + gunicorn \ + --bind 127.0.0.1:4000 "xmrnodes.app:app" \ + --daemon \ + --log-file $BASE/gunicorn.log \ + --pid $BASE/gunicorn.pid \ + --reload + sleep 2 + echo "Started gunicorn on 127.0.0.1:4000 with pid $(cat $BASE/gunicorn.pid)" + fi +else + flask $@ +fi diff --git a/requirements.txt b/requirements.txt index 3798115..f755c4c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ flask_wtf pysocks git+https://github.com/cdiv1e12/py-levin geoip2 +python-dotenv \ No newline at end of file diff --git a/xmrnodes/app.py b/xmrnodes/app.py index 9e72c3d..2bcb27b 100644 --- a/xmrnodes/app.py +++ b/xmrnodes/app.py @@ -164,9 +164,14 @@ def wow_nodes_json(): @app.route("/map") def map(): + try: + peers = rw_cache('map_peers') + except: + flash('Couldn\'t load the map. Try again later.') + return redirect('/') return render_template( "map.html", - peers=rw_cache('map_peers'), + peers=peers, source_node=config.NODE_HOST ) diff --git a/xmrnodes/config.py b/xmrnodes/config.py new file mode 100644 index 0000000..ef42adc --- /dev/null +++ b/xmrnodes/config.py @@ -0,0 +1,14 @@ +import os +from secrets import token_urlsafe + +from dotenv import load_dotenv + +load_dotenv() + +SECRET_KEY = os.environ.get('SECRET_KEY', token_urlsafe(14)) +SERVER_NAME = os.environ.get('SERVER_NAME', '127.0.0.1:5000') +DATA_DIR = os.environ.get('DATA_DIR', './data') +TOR_HOST = os.environ.get('TOR_HOST', '127.0.0.1') +TOR_PORT = os.environ.get('TOR_PORT', 9050) +NODE_HOST = os.environ.get('NODE_HOST', 'singapore.node.xmr.pm') +NODE_PORT = os.environ.get('NODE_PORT', 18080) \ No newline at end of file