feat(api): load config using shared helpers
- add django settings module documentation - use default for previously required fields BREAKING CHANGE: The API command line interface require the configuration file to be present. The default configuration file path is `/etc/airtime/airtime.conf`
This commit is contained in:
parent
9af717ef7f
commit
2dcc654b70
12 changed files with 119 additions and 146 deletions
7
api/libretime_api/settings/README.md
Normal file
7
api/libretime_api/settings/README.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
# Django settings
|
||||
|
||||
The structure of the django settings module is the following:
|
||||
|
||||
- the `__init__.py` (`libretime_api.settings`) module is the django settings entrypoint. The module contains bindings between the user configuration and the django settings. **Advanced users** may edit this file to better integrate the LibreTime API in their setup.
|
||||
- the `_internal.py` module contains application settings for django.
|
||||
- the `_schema.py` module contains the schema for the user configuration parsing and validation.
|
46
api/libretime_api/settings/__init__.py
Normal file
46
api/libretime_api/settings/__init__.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
# pylint: disable=unused-import
|
||||
from os import getenv
|
||||
|
||||
from ._internal import (
|
||||
AUTH_PASSWORD_VALIDATORS,
|
||||
AUTH_USER_MODEL,
|
||||
DEBUG,
|
||||
INSTALLED_APPS,
|
||||
MIDDLEWARE,
|
||||
REST_FRAMEWORK,
|
||||
ROOT_URLCONF,
|
||||
TEMPLATES,
|
||||
TEST_RUNNER,
|
||||
WSGI_APPLICATION,
|
||||
setup_logger,
|
||||
)
|
||||
from ._schema import Config
|
||||
|
||||
API_VERSION = "2.0.0"
|
||||
|
||||
LIBRETIME_LOG_FILEPATH = getenv("LIBRETIME_LOG_FILEPATH")
|
||||
LIBRETIME_CONFIG_FILEPATH = getenv("LIBRETIME_CONFIG_FILEPATH")
|
||||
|
||||
CONFIG = Config(filepath=LIBRETIME_CONFIG_FILEPATH)
|
||||
|
||||
SECRET_KEY = CONFIG.general.api_key
|
||||
ALLOWED_HOSTS = ["*"]
|
||||
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.postgresql",
|
||||
"HOST": CONFIG.database.host,
|
||||
"PORT": CONFIG.database.port,
|
||||
"NAME": CONFIG.database.name,
|
||||
"USER": CONFIG.database.user,
|
||||
"PASSWORD": CONFIG.database.password,
|
||||
}
|
||||
}
|
||||
|
||||
LANGUAGE_CODE = "en-us"
|
||||
TIME_ZONE = "UTC"
|
||||
USE_I18N = True
|
||||
USE_L10N = True
|
||||
USE_TZ = True
|
||||
|
||||
LOGGING = setup_logger(LIBRETIME_LOG_FILEPATH)
|
141
api/libretime_api/settings/_internal.py
Normal file
141
api/libretime_api/settings/_internal.py
Normal file
|
@ -0,0 +1,141 @@
|
|||
from os import getenv
|
||||
from typing import Optional
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = getenv("LIBRETIME_DEBUG")
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
"libretime_api.apps.LibreTimeAPIConfig",
|
||||
"django.contrib.auth",
|
||||
"django.contrib.contenttypes",
|
||||
"django.contrib.sessions",
|
||||
"django.contrib.messages",
|
||||
"django.contrib.staticfiles",
|
||||
"rest_framework",
|
||||
"django_filters",
|
||||
"drf_spectacular",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
]
|
||||
|
||||
ROOT_URLCONF = "libretime_api.urls"
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||
"DIRS": [],
|
||||
"APP_DIRS": True,
|
||||
"OPTIONS": {
|
||||
"context_processors": [
|
||||
"django.template.context_processors.debug",
|
||||
"django.template.context_processors.request",
|
||||
"django.contrib.auth.context_processors.auth",
|
||||
"django.contrib.messages.context_processors.messages",
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = "libretime_api.wsgi.application"
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
|
||||
},
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
|
||||
},
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
|
||||
},
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
|
||||
},
|
||||
]
|
||||
|
||||
# Rest Framework settings
|
||||
# https://www.django-rest-framework.org/api-guide/settings/
|
||||
|
||||
renderer_classes = ["rest_framework.renderers.JSONRenderer"]
|
||||
if DEBUG:
|
||||
renderer_classes += ["rest_framework.renderers.BrowsableAPIRenderer"]
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
"DEFAULT_RENDERER_CLASSES": renderer_classes,
|
||||
"DEFAULT_AUTHENTICATION_CLASSES": (
|
||||
"rest_framework.authentication.SessionAuthentication",
|
||||
"rest_framework.authentication.BasicAuthentication",
|
||||
),
|
||||
"DEFAULT_PERMISSION_CLASSES": [
|
||||
"libretime_api.permissions.IsSystemTokenOrUser",
|
||||
],
|
||||
"DEFAULT_FILTER_BACKENDS": [
|
||||
"django_filters.rest_framework.DjangoFilterBackend",
|
||||
],
|
||||
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
|
||||
"URL_FIELD_NAME": "item_url",
|
||||
}
|
||||
|
||||
AUTH_USER_MODEL = "libretime_api.User"
|
||||
|
||||
TEST_RUNNER = "libretime_api.tests.runners.ManagedModelTestRunner"
|
||||
|
||||
|
||||
# Logging
|
||||
def setup_logger(log_filepath: Optional[str]):
|
||||
logging_handlers = {
|
||||
"console": {
|
||||
"level": "INFO",
|
||||
"class": "logging.StreamHandler",
|
||||
"formatter": "simple",
|
||||
},
|
||||
}
|
||||
|
||||
if log_filepath is not None:
|
||||
logging_handlers["file"] = {
|
||||
"level": "DEBUG",
|
||||
"class": "logging.FileHandler",
|
||||
"filename": log_filepath,
|
||||
"formatter": "verbose",
|
||||
}
|
||||
|
||||
return {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"formatters": {
|
||||
"simple": {
|
||||
"format": "{levelname} {message}",
|
||||
"style": "{",
|
||||
},
|
||||
"verbose": {
|
||||
"format": "{asctime} {module} {levelname} {message}",
|
||||
"style": "{",
|
||||
},
|
||||
},
|
||||
"handlers": logging_handlers,
|
||||
"loggers": {
|
||||
"django": {
|
||||
"handlers": logging_handlers.keys(),
|
||||
"level": "INFO",
|
||||
"propagate": True,
|
||||
},
|
||||
"libretime_api": {
|
||||
"handlers": logging_handlers.keys(),
|
||||
"level": "INFO",
|
||||
"propagate": True,
|
||||
},
|
||||
},
|
||||
}
|
12
api/libretime_api/settings/_schema.py
Normal file
12
api/libretime_api/settings/_schema.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
from libretime_shared.config import (
|
||||
BaseConfig,
|
||||
DatabaseConfig,
|
||||
GeneralConfig,
|
||||
RabbitMQConfig,
|
||||
)
|
||||
|
||||
|
||||
class Config(BaseConfig):
|
||||
general: GeneralConfig
|
||||
database: DatabaseConfig = DatabaseConfig()
|
||||
rabbitmq: RabbitMQConfig = RabbitMQConfig()
|
Loading…
Add table
Add a link
Reference in a new issue