1515
1616
1717def _custom_file_mtime (config : ServerConfig ) -> float | None :
18- custom = getattr (config , "custom_landing_page" , None )
19- if not custom :
18+ path = getattr (config , "custom_landing_page" , None )
19+ if not path :
2020 return None
21- p = Path (custom )
2221 try :
23- return p .resolve ().stat ().st_mtime
22+ return Path ( path ) .resolve ().stat ().st_mtime
2423 except FileNotFoundError :
2524 return None
2625
@@ -30,9 +29,8 @@ def render_landing_page(
3029) -> HTMLResponse :
3130 """Render and cache the landing page with a manual, hashable key."""
3231 cache_key = (id (config ), url , _custom_file_mtime (config ))
33- cached = _PAGE_CACHE .get (cache_key )
34- if cached is not None :
35- return cached
32+ if cache_key in _PAGE_CACHE :
33+ return _PAGE_CACHE [cache_key ]
3634
3735 meta = meta_values (
3836 config , url , 1 , 1 , more_data_available = False , schema = config .schema_url
@@ -43,66 +41,55 @@ def render_landing_page(
4341 if config .custom_landing_page :
4442 html = Path (config .custom_landing_page ).resolve ().read_text ()
4543 else :
46- template_dir = Path (__file__ ).parent .joinpath ("static" ).resolve ()
47- html = (template_dir / "landing_page.html" ).read_text ()
48-
49- # Build a dictionary that maps the old Jinja keys to the new simplified replacements
50- replacements = {
51- "api_version" : __api_version__ ,
52- }
44+ html = (
45+ (Path (__file__ ).parent / "static/landing_page.html" ).resolve ().read_text ()
46+ )
5347
48+ replacements = {"api_version" : __api_version__ }
5449 if meta .provider :
5550 replacements .update (
5651 {
5752 "provider.name" : meta .provider .name ,
5853 "provider.prefix" : meta .provider .prefix ,
5954 "provider.description" : meta .provider .description ,
60- # avoid "None" string leaking into HTML
61- "provider.homepage" : str (meta .provider .homepage )
62- if meta .provider .homepage
63- else "" ,
55+ "provider.homepage" : str (meta .provider .homepage or "" ),
6456 }
6557 )
66-
6758 if meta .implementation :
6859 replacements .update (
6960 {
7061 "implementation.name" : meta .implementation .name or "" ,
7162 "implementation.version" : meta .implementation .version or "" ,
72- "implementation.source_url" : str (meta .implementation .source_url )
73- if meta .implementation .source_url
74- else "" ,
63+ "implementation.source_url" : str (meta .implementation .source_url or "" ),
7564 }
7665 )
7766
7867 for k , v in replacements .items ():
7968 html = html .replace (f"{{{{ { k } }}}}" , v )
8069
81- # Build the list of endpoints. The template already opens and closes the <ul> tag.
82- endpoints_list = [
83- f'<li><a href="{ versioned_url } { endp } ">{ versioned_url } { endp } </a></li>'
84- for endp in list (entry_collections .keys ()) + ["info" ]
85- ]
86- html = html .replace ("{% ENDPOINTS %}" , "\n " .join (endpoints_list ))
87-
88- # If the index base URL has been configured, also list it
89- index_base_url_html = ""
90- if config .index_base_url :
91- index_base_url_html = f"""<h3>Index base URL:</h3>
92- <p><a href="{ config .index_base_url } ">{ config .index_base_url } </a></p>
93- """
94- html = html .replace ("{% INDEX_BASE_URL %}" , index_base_url_html )
70+ endpoints = "\n " .join (
71+ f'<li><a href="{ versioned_url } { e } ">{ versioned_url } { e } </a></li>'
72+ for e in [* entry_collections .keys (), "info" ]
73+ )
74+ html = html .replace ("{% ENDPOINTS %}" , endpoints )
75+
76+ index_html = (
77+ f"""<h3>Index base URL:</h3>\n <p><a href="{ config .index_base_url } ">{ config .index_base_url } </a></p>\n """
78+ if config .index_base_url
79+ else ""
80+ )
81+ html = html .replace ("{% INDEX_BASE_URL %}" , index_html )
9582
9683 resp = HTMLResponse (html )
9784 _PAGE_CACHE [cache_key ] = resp
9885 return resp
9986
10087
10188async def landing (request : Request ):
102- """Show a human-readable landing page when the base URL is accessed."""
103- config : ServerConfig = request . app . state . config
104- entry_collections = request .app .state .entry_collections
105- return render_landing_page ( config , entry_collections , str ( request . url ) )
89+ """Show landing page when the base URL is accessed."""
90+ return render_landing_page (
91+ request .app .state .config , request . app . state . entry_collections , str ( request . url )
92+ )
10693
10794
10895router = Router (routes = [Route ("/" , endpoint = landing )])
0 commit comments