sghorbal commited on
Commit
a4ed0ef
·
1 Parent(s): c6c3b30

fixup: rework url_list handling

Browse files
Files changed (2) hide show
  1. src/app.py +8 -7
  2. src/utils.py +37 -5
src/app.py CHANGED
@@ -7,7 +7,12 @@ import threading
7
  import gradio as gr
8
  from fastapi.responses import JSONResponse, RedirectResponse
9
  from fastapi import FastAPI, Request, Response, status
10
- from src.utils import manage_urls, wake_up_space, get_wake_up_action_name
 
 
 
 
 
11
 
12
  # Charger les variables d'environnement
13
  load_dotenv()
@@ -31,10 +36,6 @@ async def basic_auth(request: Request, call_next):
31
 
32
  return await call_next(request)
33
 
34
- # Liste d'URLs à pinger
35
- default_urls = os.getenv("DEFAULT_URLS", "")
36
- url_list = [url.strip() for url in default_urls.split(",") if url.strip()]
37
-
38
  # Interface Gradio
39
  interface = gr.Interface(
40
  fn=manage_urls,
@@ -57,12 +58,12 @@ def root_redirect():
57
 
58
  @app.get("/check_health")
59
  def check_health():
60
- return JSONResponse(content={"status": "ok", "nb_urls": len(url_list)})
61
 
62
  # Background thread pour pinger les URLs toutes les heures
63
  def ping_loop():
64
  while True:
65
- for url in url_list:
66
  try:
67
  if get_wake_up_action_name(url):
68
  print(f"[wake-up] 🚀 {url}")
 
7
  import gradio as gr
8
  from fastapi.responses import JSONResponse, RedirectResponse
9
  from fastapi import FastAPI, Request, Response, status
10
+ from src.utils import (
11
+ manage_urls,
12
+ wake_up_space,
13
+ get_wake_up_action_name,
14
+ get_url_list,
15
+ )
16
 
17
  # Charger les variables d'environnement
18
  load_dotenv()
 
36
 
37
  return await call_next(request)
38
 
 
 
 
 
39
  # Interface Gradio
40
  interface = gr.Interface(
41
  fn=manage_urls,
 
58
 
59
  @app.get("/check_health")
60
  def check_health():
61
+ return JSONResponse(content={"status": "ok", "nb_urls": len(get_url_list())})
62
 
63
  # Background thread pour pinger les URLs toutes les heures
64
  def ping_loop():
65
  while True:
66
+ for url in get_url_list():
67
  try:
68
  if get_wake_up_action_name(url):
69
  print(f"[wake-up] 🚀 {url}")
src/utils.py CHANGED
@@ -3,21 +3,53 @@ from typing import Optional
3
  from bs4 import BeautifulSoup
4
  import time
5
  import re
 
 
6
 
7
- from src.app import url_list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Fonction Gradio pour ajouter / retirer des URLs
10
  def manage_urls(action, url):
11
- global url_list
 
12
  if action == "Ajouter":
13
  if url and url not in url_list:
14
- url_list.append(url)
15
  message = f"✅ Ajouté: {url}"
16
  else:
17
  message = "⚠️ URL déjà présente ou vide"
18
  elif action == "Supprimer":
19
  if url in url_list:
20
- url_list.remove(url)
21
  message = f"❌ Supprimé: {url}"
22
  else:
23
  message = "⚠️ URL non trouvée"
@@ -25,7 +57,7 @@ def manage_urls(action, url):
25
  message = "❓ Action invalide"
26
 
27
  # Toujours retourner la liste actuelle formatée
28
- return message, "\n".join(url_list)
29
 
30
  def wake_up_space(space_url: str):
31
  action_name = get_wake_up_action_name(space_url)
 
3
  from bs4 import BeautifulSoup
4
  import time
5
  import re
6
+ import os
7
+ from dotenv import load_dotenv
8
 
9
+ load_dotenv()
10
+
11
+ def get_url_list():
12
+ # Liste d'URLs à pinger
13
+ default_urls = os.getenv("DEFAULT_URLS", "")
14
+ url_list = [url.strip() for url in default_urls.split(",") if url.strip()]
15
+
16
+ return url_list
17
+
18
+ def add_url(url: str) -> None:
19
+ """
20
+ Add a new URL to the list of URLs.
21
+ """
22
+ if url not in get_url_list():
23
+ os.environ["DEFAULT_URLS"] = ",".join([os.environ["DEFAULT_URLS"], url])
24
+ print(f"Added URL: {url}")
25
+ else:
26
+ print(f"URL already exists: {url}")
27
+
28
+ def remove_url(url: str) -> None:
29
+ """
30
+ Remove a URL from the list of URLs.
31
+ """
32
+ url_list = get_url_list()
33
+ if url in url_list:
34
+ url_list.remove(url)
35
+ os.environ["DEFAULT_URLS"] = ",".join(url_list)
36
+ print(f"Removed URL: {url}")
37
+ else:
38
+ print(f"URL not found: {url}")
39
 
40
  # Fonction Gradio pour ajouter / retirer des URLs
41
  def manage_urls(action, url):
42
+ url_list = get_url_list()
43
+
44
  if action == "Ajouter":
45
  if url and url not in url_list:
46
+ add_url(url)
47
  message = f"✅ Ajouté: {url}"
48
  else:
49
  message = "⚠️ URL déjà présente ou vide"
50
  elif action == "Supprimer":
51
  if url in url_list:
52
+ remove_url(url)
53
  message = f"❌ Supprimé: {url}"
54
  else:
55
  message = "⚠️ URL non trouvée"
 
57
  message = "❓ Action invalide"
58
 
59
  # Toujours retourner la liste actuelle formatée
60
+ return message, "\n".join(get_url_list())
61
 
62
  def wake_up_space(space_url: str):
63
  action_name = get_wake_up_action_name(space_url)