add code to start the server
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
from my_modules.app.logger import logger
|
||||
|
||||
from dotenv import find_dotenv, load_dotenv, dotenv_values
|
||||
from pathlib import Path
|
||||
import os, asyncio
|
||||
|
||||
async def read_dot_file():
|
||||
if load_dotenv(find_dotenv()):
|
||||
dot_env_file = find_dotenv()
|
||||
await logger.info(f'Found dotenv File: {dot_env_file}')
|
||||
await logger.info(f'Loaded Content: {dict(dotenv_values(dot_env_file))}')
|
||||
|
||||
asyncio.run(read_dot_file())
|
||||
|
||||
WEB_DEBUG = os.getenv("WEB_DEBUG", False)
|
||||
|
||||
SECRET_KEY = os.getenv("FLASK_SECRET_KEY", "USE_ENV_das_ist_ein_geheimer_schlüssel_1")
|
||||
API_GROUP = os.getenv("API_GROUP", 'PICOSHARE')
|
||||
|
||||
UPLOAD_DIR = Path("uploads")
|
||||
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
||||
@@ -0,0 +1,12 @@
|
||||
from aiologger.formatters.base import Formatter
|
||||
from aiologger.handlers.streams import AsyncStreamHandler
|
||||
from aiologger import Logger
|
||||
import os
|
||||
import sys
|
||||
|
||||
formatter = Formatter(fmt="%(levelname)s %(module)s: %(message)s")
|
||||
handler = AsyncStreamHandler(stream=sys.stdout)
|
||||
handler.formatter = formatter
|
||||
|
||||
logger = Logger(name="my_webside_and_api", level="DEBUG" if os.getenv("WEB_DEBUG", False) == "true" else "INFO")
|
||||
logger.handlers = [handler]
|
||||
@@ -0,0 +1,80 @@
|
||||
from my_modules.functions import custom_limit_key
|
||||
from my_modules.app.constens import SECRET_KEY, UPLOAD_DIR
|
||||
from my_modules.AsyncCache import AsyncCache
|
||||
from my_modules.app.logger import logger
|
||||
from my_modules.EdgeDB import EdgeDB
|
||||
|
||||
from quart_session import Session
|
||||
from flask_limiter import Limiter
|
||||
|
||||
import redis.asyncio as aioredis
|
||||
from quart import Quart
|
||||
import os
|
||||
|
||||
app = Quart(__name__, template_folder="../../templates/side", static_folder="../../templates/files")
|
||||
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024
|
||||
|
||||
app.secret_key = SECRET_KEY
|
||||
app.upload_folder = UPLOAD_DIR
|
||||
|
||||
# Cache, Sessions and Limiter over Valkey
|
||||
if os.getenv("VALKEY_HOST", None) is not None:
|
||||
cache = AsyncCache(
|
||||
backend='redis',
|
||||
default_ttl=300,
|
||||
username=os.getenv('VALKEY_CACHE_USER', ''),
|
||||
password=os.getenv('VALKEY_CACHE_PASSWORD', ''),
|
||||
host=os.getenv('VALKEY_HOST'),
|
||||
port=os.getenv('VALKEY_PORT', 6379),
|
||||
db=os.getenv('VALKEY_DB', 0)
|
||||
)
|
||||
else:
|
||||
cache = AsyncCache(
|
||||
backend='memory',
|
||||
)
|
||||
|
||||
if os.getenv("VALKEY_HOST", None) is not None:
|
||||
app.config.from_mapping(
|
||||
SESSION_TYPE="redis",
|
||||
SESSION_PERMANENT=True,
|
||||
SESSION_USE_SIGNER=True,
|
||||
SESSION_REDIS = aioredis.Redis(
|
||||
username=os.getenv('VALKEY_SESSION_USER', None),
|
||||
password=os.getenv('VALKEY_SESSION_PASSWORD', None),
|
||||
host=os.getenv("VALKEY_HOST"),
|
||||
port=os.getenv("VALKEY_PORT", 6379),
|
||||
db=os.getenv("VALKEY_DB", 0),
|
||||
decode_responses=True
|
||||
)
|
||||
)
|
||||
else:
|
||||
app.config.from_mapping(
|
||||
SESSION_TYPE='memcached',
|
||||
)
|
||||
|
||||
Session(app)
|
||||
|
||||
LIMITER = Limiter(
|
||||
custom_limit_key,
|
||||
app=app,
|
||||
storage_uri=(
|
||||
f"redis://{os.getenv('VALKEY_LIMITER_USER', '')}:{os.getenv('VALKEY_LIMITER_PASSWORD', '')}"
|
||||
f"@{os.getenv("VALKEY_HOST")}:{os.getenv('VALKEY_PORT', 6379)}/{os.getenv('VALKEY_DB', 0)}"
|
||||
) if os.getenv("VALKEY_HOST") else None,
|
||||
default_limits=[],
|
||||
strategy='moving-window'
|
||||
)
|
||||
|
||||
@app.before_serving
|
||||
async def init_edgedb():
|
||||
app.edgedb = EdgeDB(
|
||||
database=os.getenv("EDGEDB_DATABASE"),
|
||||
tls_security=None if app.debug else 'insecure'
|
||||
)
|
||||
await app.edgedb.connect()
|
||||
|
||||
@app.after_serving
|
||||
async def close_edgedb():
|
||||
if app.edgedb:
|
||||
await app.edgedb.close()
|
||||
await logger.shutdown()
|
||||
Reference in New Issue
Block a user