feat: refresh button and prompt settings
This commit is contained in:
42
server.py
42
server.py
@ -6,6 +6,7 @@ import logging
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
import urllib.parse
|
||||
|
||||
# Import functions from existing scripts
|
||||
from export_web_view import HTML_TEMPLATE, build_html
|
||||
@ -14,7 +15,6 @@ import api
|
||||
import config
|
||||
|
||||
PORT = int(os.getenv("PORT", 18080))
|
||||
CACHE_DURATION = 60 # Cache data for 60 seconds
|
||||
|
||||
# Globals to store cached HTML
|
||||
cached_html = None
|
||||
@ -28,15 +28,17 @@ class FeishuHandler(http.server.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
global cached_html, last_fetch_time
|
||||
|
||||
parsed_path = urllib.parse.urlparse(self.path)
|
||||
|
||||
# Serve dynamic HTML at root or outline_view.html
|
||||
if self.path == '/' or self.path == '/outline_view.html':
|
||||
if parsed_path.path == '/' or parsed_path.path == '/outline_view.html':
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html; charset=utf-8')
|
||||
self.end_headers()
|
||||
|
||||
current_time = time.time()
|
||||
if cached_html is None or (current_time - last_fetch_time) > CACHE_DURATION:
|
||||
logging.info("Fetching fresh records from API (Cache expired or missing)...")
|
||||
if cached_html is None:
|
||||
logging.info("Fetching fresh records from API (Cache missing)...")
|
||||
try:
|
||||
client = api.Client(config.LARK_HOST)
|
||||
access_token = client.get_tenant_access_token(config.APP_ID, config.APP_SECRET)
|
||||
@ -45,13 +47,35 @@ class FeishuHandler(http.server.SimpleHTTPRequestHandler):
|
||||
|
||||
cached_html = final_html.encode('utf-8')
|
||||
last_fetch_time = current_time
|
||||
logging.info(f"Successfully generated new HTML view. Cached for {CACHE_DURATION}s.")
|
||||
logging.info("Successfully generated new HTML view.")
|
||||
except Exception as e:
|
||||
logging.error(f"Error fetching data: {e}")
|
||||
if cached_html is None:
|
||||
error_msg = f"<h1>Internal Server Error: Could not fetch data</h1><p>{e}</p>".encode('utf-8')
|
||||
self.wfile.write(error_msg)
|
||||
return
|
||||
error_msg = f"<h1>Internal Server Error: Could not fetch data</h1><p>{e}</p>".encode('utf-8')
|
||||
self.wfile.write(error_msg)
|
||||
return
|
||||
|
||||
# Write response
|
||||
self.wfile.write(cached_html)
|
||||
elif parsed_path.path == '/api/refresh':
|
||||
logging.info("Ajax refresh requested. Fetching fresh records from API...")
|
||||
try:
|
||||
client = api.Client(config.LARK_HOST)
|
||||
access_token = client.get_tenant_access_token(config.APP_ID, config.APP_SECRET)
|
||||
|
||||
final_html = build_html(client, access_token)
|
||||
cached_html = final_html.encode('utf-8')
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'application/json; charset=utf-8')
|
||||
self.end_headers()
|
||||
self.wfile.write(b'{"status": "ok"}')
|
||||
except Exception as e:
|
||||
logging.error(f"Ajax Error fetching data: {e}")
|
||||
self.send_response(500)
|
||||
self.send_header('Content-type', 'application/json; charset=utf-8')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps({"status": "error", "message": str(e)}).encode('utf-8'))
|
||||
return
|
||||
|
||||
# Write response
|
||||
self.wfile.write(cached_html)
|
||||
|
||||
Reference in New Issue
Block a user