File size: 2,096 Bytes
2975db2
97f9e03
 
e566133
97f9e03
 
12e4bed
6ccc851
 
84b52da
97f9e03
 
8a3a45c
 
 
 
 
 
 
 
 
c283ccc
 
 
 
8a3a45c
 
 
 
 
 
 
 
 
 
 
 
 
 
e566133
e55d3e3
e566133
 
 
62d88b5
e566133
 
6ccc851
e566133
6ccc851
2975db2
c283ccc
588e561
c283ccc
e566133
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import asyncio
import logging
from aiohttp import web

#-----------------------Local Imports-------------------------#
from .API import api
from .APP import app
from .Authentication import auth
from .routes_main import routes
from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS

"""
from .Middlewares.jwt_middleware import jwt_middleware
from .Middlewares.app_token_middleware import app_token_middleware
from .Middlewares.logging_middleware import logging_middleware
#from .Middlewares.rate_limit_middleware import rate_limit_middleware
"""
# Set time to consider a client inactive (e.g., 10 minutes)
INACTIVITY_TIMEOUT = 180  # 3 minutes

# Define the path to the template (static) folder
template_folder = os.path.join(os.path.dirname(__file__), 'template')


async def clear_inactive_clients():
    """Clear inactive clients from ACTIVE_CLIENTS."""
    await asyncio.sleep(INACTIVITY_TIMEOUT)  # Check every INACTIVITY_TIMEOUT seconds
    now = asyncio.get_event_loop().time()  # Get current time 
    inactive_clients = [
        client_id for client_id, tg_connect in ACTIVE_CLIENTS.items()
        if now - tg_connect.last_activity > INACTIVITY_TIMEOUT  # Compare with last_activity timestamp
    ]
    for client in inactive_clients:
        # Log and clear the inactive client
        logging.info(f"** Clearing inactive client: {client.name}")
        del ACTIVE_CLIENTS[client]  # Remove inactive client from ACTIVE_CLIENTS


async def root_route_handler(request):
    return web.json_response({"status": "alive", "message": "Server is running","active_clients":[{"client_name": tg_connect.client.name} for client_id, tg_connect in ACTIVE_CLIENTS.items()],"public_ip":request.remote})


def web_server():
    web_app = web.Application(client_max_size=50)
    web_app.router.add_get('/', root_route_handler)
    web_app.add_routes(routes)
    web_app.add_subapp('/app', app)
    web_app.add_subapp('/api', api)
    web_app.add_subapp('/auth', auth)

    # Add static file handler
    web_app.router.add_static('/static/', template_folder)

    return web_app