Compare commits

..

5 Commits

3 changed files with 35 additions and 4 deletions

View File

@ -1,3 +1,4 @@
from argparse import ArgumentParser
from typing import Optional from typing import Optional
import os import os
@ -9,8 +10,8 @@ class AmanuensisConfig:
# config values defined on the config object itself. # config values defined on the config object itself.
CONFIG_FILE: Optional[str] = None CONFIG_FILE: Optional[str] = None
STATIC_ROOT: Optional[str] = "static" STATIC_ROOT: Optional[str] = "static"
SECRET_KEY: Optional[str] = None SECRET_KEY: Optional[str] = "secret"
DATABASE_URI: Optional[str] = None DATABASE_URI: Optional[str] = "sqlite:///:memory:"
TESTING: bool = False TESTING: bool = False
@ -24,3 +25,21 @@ class EnvironmentConfig(AmanuensisConfig):
"AMANUENSIS_DATABASE_URI", AmanuensisConfig.DATABASE_URI "AMANUENSIS_DATABASE_URI", AmanuensisConfig.DATABASE_URI
) )
TESTING = os.environ.get("AMANUENSIS_TESTING", "").lower() in ("true", "1") TESTING = os.environ.get("AMANUENSIS_TESTING", "").lower() in ("true", "1")
class CommandLineConfig(AmanuensisConfig):
"""Loads config values from command line arguments."""
def __init__(self) -> None:
parser = ArgumentParser()
parser.add_argument("--config-file", default=AmanuensisConfig.CONFIG_FILE)
parser.add_argument("--static-root", default=AmanuensisConfig.STATIC_ROOT)
parser.add_argument("--secret-key", default=AmanuensisConfig.SECRET_KEY)
parser.add_argument("--database-uri", default=AmanuensisConfig.DATABASE_URI)
parser.add_argument("--debug", action="store_true")
args = parser.parse_args()
self.CONFIG_FILE = args.config_file
self.STATIC_ROOT = args.static_root
self.SECRET_KEY = args.secret_key
self.DATABASE_URI = args.database_uri
self.TESTING = args.debug

View File

@ -2,11 +2,14 @@ import json
from flask import Flask, g from flask import Flask, g
from amanuensis.config import AmanuensisConfig from amanuensis.config import AmanuensisConfig, CommandLineConfig
from amanuensis.db import DbContext from amanuensis.db import DbContext
def get_app(config: AmanuensisConfig, db: DbContext = None,) -> Flask: def get_app(
config: AmanuensisConfig,
db: DbContext = None,
) -> Flask:
"""Application factory""" """Application factory"""
# Create the Flask object # Create the Flask object
app = Flask(__name__, template_folder=".", static_folder=config.STATIC_ROOT) app = Flask(__name__, template_folder=".", static_folder=config.STATIC_ROOT)
@ -51,3 +54,9 @@ def get_app(config: AmanuensisConfig, db: DbContext = None,) -> Flask:
app.route("/")(test) app.route("/")(test)
return app return app
def run():
"""Run the server, populating the config from the command line."""
config = CommandLineConfig()
get_app(config).run(debug=config.TESTING)

View File

@ -16,6 +16,9 @@ pytest = "^5.2"
black = "^21.5b2" black = "^21.5b2"
mypy = "^0.812" mypy = "^0.812"
[tool.poetry.scripts]
amanuensis-server = "amanuensis.server:run"
[tool.black] [tool.black]
extend-exclude = "^/amanuensis/cli/.*|^/amanuensis/config/.*|^/amanuensis/lexicon/.*|^/amanuensis/log/.*|^/amanuensis/models/.*|^/amanuensis/resources/.*|^/amanuensis/server/.*|^/amanuensis/user/.*|^/amanuensis/__main__.py" extend-exclude = "^/amanuensis/cli/.*|^/amanuensis/config/.*|^/amanuensis/lexicon/.*|^/amanuensis/log/.*|^/amanuensis/models/.*|^/amanuensis/resources/.*|^/amanuensis/server/.*|^/amanuensis/user/.*|^/amanuensis/__main__.py"