From 5ac57a9e1a56f222a8b1e7edbb76e08f87ef23d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Wed, 11 Apr 2018 14:55:20 +0200 Subject: [PATCH] chg: Move the querying module, add very simple web interface --- bgpranking/libs/helpers.py | 5 ++++- api/api.py => bgpranking/querying.py | 6 +++--- website/__init__.py | 0 website/readme.md | 9 +++++++++ website/requirements.txt | 3 +++ website/web/__init__.py | 22 ++++++++++++++++++++++ website/web/templates/index.html | 21 +++++++++++++++++++++ website/web/templates/main.html | 13 +++++++++++++ 8 files changed, 75 insertions(+), 4 deletions(-) rename api/api.py => bgpranking/querying.py (96%) create mode 100644 website/__init__.py create mode 100644 website/readme.md create mode 100644 website/requirements.txt create mode 100644 website/web/__init__.py create mode 100644 website/web/templates/index.html create mode 100644 website/web/templates/main.html diff --git a/bgpranking/libs/helpers.py b/bgpranking/libs/helpers.py index bd43899..89a54e4 100644 --- a/bgpranking/libs/helpers.py +++ b/bgpranking/libs/helpers.py @@ -40,7 +40,10 @@ def get_list_storage_path(): def get_homedir(): if not os.environ.get('BGPRANKING_HOME'): - raise MissingEnv("BGPRANKING_HOME is missing. Run the following from the home directory of the repository: export BGPRANKING_HOME='./'") + guessed_home = Path(__file__).resolve().parent.parent.parent + raise MissingEnv(f"BGPRANKING_HOME is missing. \ +Run the following command (assuming you run the code from the clonned repository):\ + export BGPRANKING_HOME='{guessed_home}'") return Path(os.environ['BGPRANKING_HOME']) diff --git a/api/api.py b/bgpranking/querying.py similarity index 96% rename from api/api.py rename to bgpranking/querying.py index 3527525..d59f2fe 100644 --- a/api/api.py +++ b/bgpranking/querying.py @@ -9,8 +9,8 @@ from dateutil.parser import parse import logging from redis import StrictRedis -from bgpranking.libs.helpers import get_socket_path -from bgpranking.libs.exceptions import InvalidDateFormat +from .libs.helpers import get_socket_path +from .libs.exceptions import InvalidDateFormat Dates = TypeVar('Dates', datetime.datetime, datetime.date, str) @@ -20,7 +20,7 @@ class IPVersion(Enum): v6 = 'v6' -class BGPRanking(): +class Querying(): def __init__(self, loglevel: int=logging.DEBUG): self.__init_logger(loglevel) diff --git a/website/__init__.py b/website/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/website/readme.md b/website/readme.md new file mode 100644 index 0000000..06313f5 --- /dev/null +++ b/website/readme.md @@ -0,0 +1,9 @@ +# Usage + +Install the dependencies, run + +```bash +export FLASK_APP=${BGPRANKING_HOME}/website/web/__init__.py +flask run --port 5005 +``` + diff --git a/website/requirements.txt b/website/requirements.txt new file mode 100644 index 0000000..5b00a4c --- /dev/null +++ b/website/requirements.txt @@ -0,0 +1,3 @@ +# Web thing +flask +flask-bootstrap diff --git a/website/web/__init__.py b/website/web/__init__.py new file mode 100644 index 0000000..86c038d --- /dev/null +++ b/website/web/__init__.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +from flask import Flask, render_template +from flask_bootstrap import Bootstrap + +from bgpranking.querying import Querying + +app = Flask(__name__) + +Bootstrap(app) +app.config['BOOTSTRAP_SERVE_LOCAL'] = True + +app.debug = True + + +@app.route('/', methods=['GET']) +def index(): + q = Querying() + ranks = q.asns_global_ranking(limit=-1) + return render_template('index.html', ranks=ranks) diff --git a/website/web/templates/index.html b/website/web/templates/index.html new file mode 100644 index 0000000..1194d98 --- /dev/null +++ b/website/web/templates/index.html @@ -0,0 +1,21 @@ +{% extends "main.html" %} + +{% block title %}BGP Ranking{% endblock %} + +{% block content %} +
+

BGP Ranking



+
+ + + + + + {% for asn, rank in ranks %} + + + + + {% endfor %} +
ASNRank
{{asn}}{{rank}}
+{% endblock %} diff --git a/website/web/templates/main.html b/website/web/templates/main.html new file mode 100644 index 0000000..a81793b --- /dev/null +++ b/website/web/templates/main.html @@ -0,0 +1,13 @@ +{% extends "bootstrap/base.html" %} + +{% block scripts %} + {{ super() }} +{% endblock %} + +{% block head %} + {{ super() }} +{% endblock %} + +{% block content %} + {{ super() }} +{% endblock %}