python: simplify server configuration

This commit is contained in:
vanyauhalin
2023-07-13 16:17:29 +04:00
parent 4ea0760803
commit 988da27e3f
6 changed files with 83 additions and 184 deletions

View File

@ -1,21 +1,87 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
from os import environ
from pathlib import Path
from sys import argv
from uuid import uuid1
from django import setup
from django.conf import settings
from django.core.management import execute_from_command_line
from django.core.management.commands.runserver import Command as RunServer
from django.conf.urls.static import static
from django.urls import path
from src.common import string
from src.views import actions, index
def debug():
env = environ.get('DEBUG')
return string.boolean(env, True)
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
def address():
if settings.DEBUG:
return RunServer.default_addr
return '0.0.0.0'
def port():
env = environ.get('PORT')
if env:
return int(env)
return RunServer.default_port
def configuration():
file = Path(__file__)
base_dir = file.parent
static_root = base_dir.joinpath('static')
static_url = f'{static_root.name}/'
return {
'ALLOWED_HOSTS': [
'*'
],
'BASE_DIR': f'{base_dir}',
'DEBUG': debug(),
'MIDDLEWARE': [
'src.utils.historyManager.CorsHeaderMiddleware'
],
'ROOT_URLCONF': __name__,
'SECRET_KEY': uuid1(),
'STATIC_ROOT': f'{static_root}',
'STATIC_URL': static_url,
'TEMPLATES': [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'templates'
]
}
]
}
def routers():
main = [
path('', index.default),
path('convert', actions.convert),
path('create', actions.createNew),
path('csv', actions.csv),
path('download', actions.download),
path('downloadhistory', actions.downloadhistory),
path('edit', actions.edit),
path('files', actions.files),
path('reference', actions.reference),
path('remove', actions.remove),
path('rename', actions.rename),
path('saveas', actions.saveAs),
path('track', actions.track),
path('upload', actions.upload)
]
main += static(
settings.STATIC_URL,
document_root=settings.STATIC_ROOT
)
return main
settings.configure(**configuration())
urlpatterns = routers()
RunServer.default_addr = address()
RunServer.default_port = port()
setup()
if __name__ == '__main__':
main()
execute_from_command_line(argv)