22import threading
33from typing import Callable , Optional
44
5- import pkg_resources
5+ from importlib_resources import files , as_file
66import uvicorn
77from fastapi import FastAPI , Response
88from fastapi .middleware .cors import CORSMiddleware
@@ -16,8 +16,10 @@ def get_app(
1616 get_registry_dump : Callable ,
1717 project_id : str ,
1818 registry_ttl_secs : int ,
19+ host : str ,
20+ port : int ,
1921):
20- ui_dir = pkg_resources . resource_filename ( __name__ , "ui/build/" )
22+ ui_dir = files ( __package__ ). joinpath ( "ui/build/" )
2123
2224 app = FastAPI ()
2325
@@ -53,25 +55,30 @@ def shutdown_event():
5355
5456 async_refresh ()
5557
56- @app .get ("/registry" )
57- def read_registry ():
58- return json .loads (registry_json )
59-
60- # Generate projects-list json that points to the current repo's project
61- # TODO(adchia): Enable users to also add project name + description fields in feature_store.yaml
62- @app .get ("/projects-list" )
63- def projects_list ():
64- projects = {
58+ # Initialize with the projects-list.json file
59+ try :
60+ f = ui_dir .joinpath ("projects-list.json" ).open (mode = "w" )
61+ projects_dict = {
6562 "projects" : [
6663 {
6764 "name" : "Project" ,
6865 "description" : "Test project" ,
6966 "id" : project_id ,
70- "registryPath" : "http://0.0.0.0:8888 /registry" ,
67+ "registryPath" : f "http://{ host } : { port } /registry" ,
7168 }
7269 ]
7370 }
74- return projects
71+ f .write (json .dumps (projects_dict ))
72+ except Exception as e :
73+ raise RuntimeError (
74+ "Failed to initialize projects-list.json with registry path"
75+ ) from e
76+ finally :
77+ f .close ()
78+
79+ @app .get ("/registry" )
80+ def read_registry ():
81+ return json .loads (registry_json )
7582
7683 # For all other paths (such as paths that would otherwise be handled by react router), pass to React
7784 @app .api_route ("/p/{path_name:path}" , methods = ["GET" ])
@@ -83,9 +90,10 @@ def catch_all():
8390
8491 return Response (content , media_type = "text/html" )
8592
86- app .mount (
87- "/" , StaticFiles (directory = ui_dir , html = True ), name = "site" ,
88- )
93+ with as_file (ui_dir ) as react_app_dir :
94+ app .mount (
95+ "/" , StaticFiles (directory = react_app_dir , html = True ), name = "site" ,
96+ )
8997
9098 return app
9199
@@ -98,5 +106,5 @@ def start_server(
98106 project_id : str ,
99107 registry_ttl_sec : int ,
100108):
101- app = get_app (store , get_registry_dump , project_id , registry_ttl_sec )
109+ app = get_app (store , get_registry_dump , project_id , registry_ttl_sec , host , port )
102110 uvicorn .run (app , host = host , port = port )
0 commit comments