feat(api): split api into multiple apps (#1626)

Fixes #1622

- split the api into 4 apps: core, history, schedule, storage
- exploded the settings into testing/prod
This commit is contained in:
Jonas L 2022-04-04 14:38:50 +02:00 committed by GitHub
parent 87d2da9d84
commit fce988aef1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
120 changed files with 1499 additions and 1078 deletions

View file

@ -1,7 +1,11 @@
# Django settings
For more information on django settings, see https://docs.djangoproject.com/en/3.2/topics/settings/.
For the full list of settings and their values, see https://docs.djangoproject.com/en/3.2/ref/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.
- the `prod.py` (`libretime_api.settings.prod`) 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 `testing.py` (`libretime_api.settings.testing`) module is the testing django settings entrypoint.

View file

@ -1,53 +0,0 @@
# 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,
STATIC_URL,
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)
SPECTACULAR_SETTINGS = {
"TITLE": "LibreTime API",
"DESCRIPTION": "Radio Broadcast & Automation Platform",
"VERSION": API_VERSION,
}

View file

@ -1,13 +1,18 @@
from os import getenv
from typing import Optional
API_VERSION = "2.0.0"
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = getenv("LIBRETIME_DEBUG")
DEBUG = getenv("LIBRETIME_DEBUG", "false").lower() == "true"
# Application definition
INSTALLED_APPS = [
"libretime_api.apps.LibreTimeAPIConfig",
"libretime_api.core",
"libretime_api.history",
"libretime_api.storage",
"libretime_api.schedule",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
@ -48,8 +53,19 @@ TEMPLATES = [
WSGI_APPLICATION = "libretime_api.wsgi.application"
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = "/api/v2/static/"
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
@ -66,39 +82,11 @@ AUTH_PASSWORD_VALIDATORS = [
},
]
# 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",
}
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = "/api/v2/static/"
AUTH_USER_MODEL = "libretime_api.User"
TEST_RUNNER = "libretime_api.tests.runners.ManagedModelTestRunner"
# Logging
# https://docs.djangoproject.com/en/3.2/topics/logging/#configuring-logging
def setup_logger(log_filepath: Optional[str]):
logging_handlers = {
"console": {
@ -143,3 +131,41 @@ def setup_logger(log_filepath: Optional[str]):
},
},
}
# Rest Framework
# 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
# https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#substituting-a-custom-user-model
AUTH_USER_MODEL = "core.User"
# Spectacular
# https://drf-spectacular.readthedocs.io/en/latest/settings.html
SPECTACULAR_SETTINGS = {
"TITLE": "LibreTime API",
"DESCRIPTION": "Radio Broadcast & Automation Platform",
"VERSION": API_VERSION,
}

View file

@ -0,0 +1,55 @@
from os import getenv
# pylint: disable=unused-import
from ._internal import (
API_VERSION,
AUTH_PASSWORD_VALIDATORS,
AUTH_USER_MODEL,
DEBUG,
DEFAULT_AUTO_FIELD,
INSTALLED_APPS,
MIDDLEWARE,
REST_FRAMEWORK,
ROOT_URLCONF,
SPECTACULAR_SETTINGS,
STATIC_URL,
TEMPLATES,
WSGI_APPLICATION,
setup_logger,
)
from ._schema import Config
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 = ["*"]
LOGGING = setup_logger(LIBRETIME_LOG_FILEPATH)
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
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,
}
}
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True

View file

@ -0,0 +1,35 @@
import os
os.environ.setdefault("LIBRETIME_DEBUG", "true")
os.environ.setdefault("LIBRETIME_GENERAL_API_KEY", "testing")
# pylint: disable=wrong-import-position,unused-import
from .prod import (
ALLOWED_HOSTS,
API_VERSION,
AUTH_PASSWORD_VALIDATORS,
AUTH_USER_MODEL,
CONFIG,
DATABASES,
DEBUG,
DEFAULT_AUTO_FIELD,
INSTALLED_APPS,
LANGUAGE_CODE,
LOGGING,
MIDDLEWARE,
REST_FRAMEWORK,
ROOT_URLCONF,
SECRET_KEY,
STATIC_URL,
TEMPLATES,
TIME_ZONE,
USE_I18N,
USE_L10N,
USE_TZ,
WSGI_APPLICATION,
)
# Testing
# https://docs.djangoproject.com/en/3.2/ref/settings/#test-runner
TEST_RUNNER = "libretime_api.tests.runner.ManagedModelTestRunner"