privateone commited on
Commit
32585f3
·
1 Parent(s): 97f9e03

Code Updates & Optimisations, Recreation of Server

Browse files
FileStream/server/__init__.py CHANGED
@@ -3,6 +3,9 @@ import logging
3
  from aiohttp import web
4
 
5
  #-----------------------Local Imports-------------------------#
 
 
 
6
  from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS
7
 
8
 
 
3
  from aiohttp import web
4
 
5
  #-----------------------Local Imports-------------------------#
6
+ from .routes_api import api
7
+ from .routes_main import routes
8
+ from .routes_app import sub_app
9
  from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS
10
 
11
 
Unused Codes/__init__old.py CHANGED
@@ -20,50 +20,3 @@ def web_server():
20
  web_app.add_subapp('/api', api)
21
  return web_app
22
 
23
- import asyncio
24
- import logging
25
- from aiohttp import web
26
-
27
- # Dictionary to store active clients, using client identifier (could be IP or session ID)
28
- ACTIVE_CLIENTS = {}
29
-
30
- # Set time to consider a client inactive (e.g., 5 minutes)
31
- INACTIVITY_TIMEOUT = 300 # 5 minutes
32
-
33
- async def clear_inactive_clients():
34
- """
35
- Periodically checks and clears inactive clients from ACTIVE_CLIENTS.
36
- """
37
- while True:
38
- await asyncio.sleep(INACTIVITY_TIMEOUT) # Check every 5 minutes
39
-
40
- # Find clients that are inactive and clear them
41
- now = asyncio.get_event_loop().time()
42
- inactive_clients = [
43
- client for client, last_activity in ACTIVE_CLIENTS.items()
44
- if now - last_activity > INACTIVITY_TIMEOUT
45
- ]
46
-
47
- for client in inactive_clients:
48
- # Log and clear the inactive client
49
- logging.info(f"Clearing inactive client: {client}")
50
- del ACTIVE_CLIENTS[client] # Clear the client
51
-
52
- async def root_route_handler(request):
53
- client_id = request.remote # You can use the client's IP or any identifier
54
- ACTIVE_CLIENTS[client_id] = asyncio.get_event_loop().time() # Update last activity time
55
-
56
- return web.json_response({"status": "alive", "message": "Server is running"})
57
-
58
- def web_server():
59
- web_app = web.Application(client_max_size=500)
60
- web_app.router.add_get('/', root_route_handler)
61
- web_app.add_routes(routes)
62
- web_app.add_subapp('/app', sub_app)
63
- web_app.add_subapp('/api', api)
64
-
65
- # Start the background task to clear inactive clients
66
- asyncio.create_task(clear_inactive_clients())
67
-
68
- return web_app
69
-
 
20
  web_app.add_subapp('/api', api)
21
  return web_app
22