From e95d5d28163aa1cad8014e493bf77a7ffe728b74 Mon Sep 17 00:00:00 2001 From: 573dev <> Date: Tue, 13 Oct 2020 13:00:28 -0500 Subject: [PATCH] Get basic flask server running --- README.md | 22 ++++++++++++++++++++++ docs/general/gen_template.md | 4 ---- setup.py | 2 +- tests/test_template.py | 7 ------- v8_server/__init__.py | 34 +++++++++++++++++++++++++++++++++- v8_server/config.py | 14 ++++++++++++++ v8_server/template.py | 21 --------------------- v8_server/utils.py | 24 ++++++++++++++++++++++++ v8_server/view/__init__.py | 1 + v8_server/view/index.py | 6 ++++++ 10 files changed, 101 insertions(+), 34 deletions(-) delete mode 100644 docs/general/gen_template.md delete mode 100644 tests/test_template.py create mode 100644 v8_server/config.py delete mode 100644 v8_server/template.py create mode 100644 v8_server/utils.py create mode 100644 v8_server/view/__init__.py create mode 100644 v8_server/view/index.py diff --git a/README.md b/README.md index 57605bf..6fa889b 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,27 @@ Simlated eAmuse Server for GFDM V8 +## Development + +To run this in development mode, you should do the following: + +```python +# Create a venv and install the package +python -m venv venv +. venv/bin/activate +pip install --upgrade pip +pip install -e . + +# Set the development environment variables +export FLASK_APP=v8_server +export FLASK_ENV=development + +# Run the server +flask run + +# To run in ssl mode +flask run --cert=adhoc +``` + ## License v8\_server is provided under an MIT License. diff --git a/docs/general/gen_template.md b/docs/general/gen_template.md deleted file mode 100644 index 3a4ef30..0000000 --- a/docs/general/gen_template.md +++ /dev/null @@ -1,4 +0,0 @@ -## Template - -Explain Template Here - diff --git a/setup.py b/setup.py index b205083..d3ef36f 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ from setuptools import find_packages, setup TEST_DEPS = ["coverage[toml]", "pytest", "pytest-cov"] DOCS_DEPS = ["sphinx", "sphinx-rtd-theme", "sphinx-autoapi", "recommonmark"] CHECK_DEPS = ["isort", "flake8", "flake8-quotes", "pep8-naming", "mypy", "black"] -REQUIREMENTS = ["flask"] +REQUIREMENTS = ["flask", "watchdog", "pyopenssl"] EXTRAS = { "test": TEST_DEPS, diff --git a/tests/test_template.py b/tests/test_template.py deleted file mode 100644 index 7953070..0000000 --- a/tests/test_template.py +++ /dev/null @@ -1,7 +0,0 @@ -from v8_server.template import function_test - - -def test_function_test(): - data = 2 - expected = 4 - assert function_test(data) == expected diff --git a/v8_server/__init__.py b/v8_server/__init__.py index f79dd92..82fbfb6 100644 --- a/v8_server/__init__.py +++ b/v8_server/__init__.py @@ -1,4 +1,36 @@ +import os +from pathlib import Path +from typing import Optional, Union + +from flask import Flask + +from v8_server.config import Development, Production +from v8_server.utils import generate_secret_key + from .version import __version__ -__all__ = ["__version__"] +# Set the proper config values +config: Optional[Union[Production, Development]] = None +if os.environ.get("ENV", "dev") == "prod": + config = Production() +else: + config = Development() + print(" * THIS APP IS IN DEV MODE") + +# Set the location for the static files and templates +# We might not even need this? +package_dir = Path(__file__).parent / "view" +template_dir = str(package_dir / "templates") +static_dir = str(package_dir / "static") + +# Initialize the flask app +app = Flask(__name__, template_folder=template_dir, static_folder=static_dir) +app.secret_key = generate_secret_key(config.SECRET_KEY_FILENAME) +app.config.from_object(config) + +# We need to import the views here specifically once the flask app has been initialized +import v8_server.view # noqa: F401, E402 + + +__all__ = ["__version__", "app"] diff --git a/v8_server/config.py b/v8_server/config.py new file mode 100644 index 0000000..29dac42 --- /dev/null +++ b/v8_server/config.py @@ -0,0 +1,14 @@ +class Config(object): + DEBUG = False + TESTING = False + DB_SERVER = "localhost" + SECRET_KEY_FILENAME = "v8_server.key" + + +class Development(Config): + DEBUG = True + SECRET_KEY_FILENAME = "dev_v8_server.key" + + +class Production(Config): + pass diff --git a/v8_server/template.py b/v8_server/template.py deleted file mode 100644 index e8530e7..0000000 --- a/v8_server/template.py +++ /dev/null @@ -1,21 +0,0 @@ -import logging - - -logger = logging.getLogger(__name__) - - -def function_test(x: int) -> int: - """ - Returns the input value multiplied by 2 - - Args: - x (int): Value to multiply - - Returns: - int: input value multiplied by 2 - - Example: - >>> function_test(2) - 4 - """ - return x * 2 diff --git a/v8_server/utils.py b/v8_server/utils.py new file mode 100644 index 0000000..851e155 --- /dev/null +++ b/v8_server/utils.py @@ -0,0 +1,24 @@ +import datetime +from os import urandom +from pathlib import Path +from tempfile import gettempdir + + +def generate_secret_key(secret_key_filename, expiry_delta=None): + secret_key_file = Path(gettempdir()) / secret_key_filename + secret_exists = secret_key_file.exists() + + expired = None + if secret_exists and expiry_delta is not None: + modified_date = datetime.fromtimestamp(secret_key_file.stat().st_mtime) + expired = datetime.now() - modified_date >= expiry_delta + + if not secret_exists or expired: + secret_key = urandom(24) + with secret_key_file.open("wb") as f: + f.write(secret_key) + else: + with secret_key_file.open("rb") as f: + secret_key = f.read() + + return secret_key diff --git a/v8_server/view/__init__.py b/v8_server/view/__init__.py new file mode 100644 index 0000000..7fac9a0 --- /dev/null +++ b/v8_server/view/__init__.py @@ -0,0 +1 @@ +import v8_server.view.index # noqa: F401 diff --git a/v8_server/view/index.py b/v8_server/view/index.py new file mode 100644 index 0000000..f7d21bc --- /dev/null +++ b/v8_server/view/index.py @@ -0,0 +1,6 @@ +from v8_server import app + + +@app.route("/") +def hello_world(): + return "Hello, World!"