Get basic flask server running

This commit is contained in:
573dev
2020-10-13 13:00:28 -05:00
parent 2b4120ca2e
commit e95d5d2816
10 changed files with 101 additions and 34 deletions

View File

@@ -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.

View File

@@ -1,4 +0,0 @@
## Template
Explain Template Here

View File

@@ -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,

View File

@@ -1,7 +0,0 @@
from v8_server.template import function_test
def test_function_test():
data = 2
expected = 4
assert function_test(data) == expected

View File

@@ -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"]

14
v8_server/config.py Normal file
View File

@@ -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

View File

@@ -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

24
v8_server/utils.py Normal file
View File

@@ -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

View File

@@ -0,0 +1 @@
import v8_server.view.index # noqa: F401

6
v8_server/view/index.py Normal file
View File

@@ -0,0 +1,6 @@
from v8_server import app
@app.route("/")
def hello_world():
return "Hello, World!"