Skip to content

Commit

Permalink
move app_settings to app.state
Browse files Browse the repository at this point in the history
  • Loading branch information
jstzwj committed Nov 25, 2024
1 parent a3a55bb commit e76c553
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/olah/proxy/commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def commits_generator(
headers["authorization"] = authorization

# save
repos_path = app.app_settings.config.repos_path
repos_path = app.state.app_settings.config.repos_path
save_dir = os.path.join(
repos_path, f"api/{repo_type}/{org}/{repo}/commits/{commit}"
)
Expand All @@ -92,7 +92,7 @@ async def commits_generator(

org_repo = get_org_repo(org, repo)
commits_url = urljoin(
app.app_settings.config.hf_url_base(),
app.state.app_settings.config.hf_url_base(),
f"/api/{repo_type}/{org_repo}/commits/{commit}",
)
# proxy
Expand Down
26 changes: 13 additions & 13 deletions src/olah/proxy/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ async def _file_chunk_head(
allow_cache: bool,
file_size: int,
):
if not app.app_settings.config.offline:
if not app.state.app_settings.config.offline:
async with client.stream(
method=method,
url=url,
Expand Down Expand Up @@ -333,17 +333,17 @@ async def _file_realtime_stream(
)
else:
hf_url = urljoin(
app.app_settings.config.hf_lfs_url_base(), get_url_tail(clean_url)
app.state.app_settings.config.hf_lfs_url_base(), get_url_tail(clean_url)
)
else:
if urlparse(url).netloc in [
app.app_settings.config.hf_netloc,
app.app_settings.config.hf_lfs_netloc,
app.state.app_settings.config.hf_netloc,
app.state.app_settings.config.hf_lfs_netloc,
]:
hf_url = url
else:
hf_url = urljoin(
app.app_settings.config.hf_lfs_url_base(), get_url_tail(url)
app.state.app_settings.config.hf_lfs_url_base(), get_url_tail(url)
)

request_headers = {k: v for k, v in request.headers.items()}
Expand Down Expand Up @@ -409,7 +409,7 @@ async def _file_realtime_stream(
etag = await _resource_etag(
hf_url=hf_url,
authorization=request.headers.get("authorization", None),
offline=app.app_settings.config.offline,
offline=app.state.app_settings.config.offline,
)
response_headers["etag"] = etag

Expand Down Expand Up @@ -466,7 +466,7 @@ async def file_get_generator(
):
org_repo = get_org_repo(org, repo)
# save
repos_path = app.app_settings.config.repos_path
repos_path = app.state.app_settings.config.repos_path
head_path = os.path.join(
repos_path, f"heads/{repo_type}/{org}/{repo}/resolve/{commit}/{file_path}"
)
Expand All @@ -482,12 +482,12 @@ async def file_get_generator(
# proxy
if repo_type == "models":
url = urljoin(
app.app_settings.config.hf_url_base(),
app.state.app_settings.config.hf_url_base(),
f"/{org_repo}/resolve/{commit}/{file_path}",
)
else:
url = urljoin(
app.app_settings.config.hf_url_base(),
app.state.app_settings.config.hf_url_base(),
f"/{repo_type}/{org_repo}/resolve/{commit}/{file_path}",
)
return _file_realtime_stream(
Expand Down Expand Up @@ -520,7 +520,7 @@ async def cdn_file_get_generator(

org_repo = get_org_repo(org, repo)
# save
repos_path = app.app_settings.config.repos_path
repos_path = app.state.app_settings.config.repos_path
head_path = os.path.join(
repos_path, f"heads/{repo_type}/{org}/{repo}/cdn/{file_hash}"
)
Expand All @@ -535,10 +535,10 @@ async def cdn_file_get_generator(

# proxy
# request_url = urlparse(str(request.url))
# if request_url.netloc == app.app_settings.config.hf_lfs_netloc:
# redirected_url = urljoin(app.app_settings.config.mirror_lfs_url_base(), get_url_tail(request_url))
# if request_url.netloc == app.state.app_settings.config.hf_lfs_netloc:
# redirected_url = urljoin(app.state.app_settings.config.mirror_lfs_url_base(), get_url_tail(request_url))
# else:
# redirected_url = urljoin(app.app_settings.config.mirror_url_base(), get_url_tail(request_url))
# redirected_url = urljoin(app.state.app_settings.config.mirror_url_base(), get_url_tail(request_url))

return _file_realtime_stream(
app=app,
Expand Down
4 changes: 2 additions & 2 deletions src/olah/proxy/lfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def lfs_head_generator(
app, dir1: str, dir2: str, hash_repo: str, hash_file: str, request: Request
):
# save
repos_path = app.app_settings.config.repos_path
repos_path = app.state.app_settings.config.repos_path
head_path = os.path.join(
repos_path, f"lfs/heads/{dir1}/{dir2}/{hash_repo}/{hash_file}"
)
Expand Down Expand Up @@ -47,7 +47,7 @@ async def lfs_get_generator(
app, dir1: str, dir2: str, hash_repo: str, hash_file: str, request: Request
):
# save
repos_path = app.app_settings.config.repos_path
repos_path = app.state.app_settings.config.repos_path
head_path = os.path.join(
repos_path, f"lfs/heads/{dir1}/{dir2}/{hash_repo}/{hash_file}"
)
Expand Down
4 changes: 2 additions & 2 deletions src/olah/proxy/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async def meta_generator(
headers["authorization"] = authorization

# save
repos_path = app.app_settings.config.repos_path
repos_path = app.state.app_settings.config.repos_path
save_dir = os.path.join(
repos_path, f"api/{repo_type}/{org}/{repo}/revision/{commit}"
)
Expand All @@ -89,7 +89,7 @@ async def meta_generator(

org_repo = get_org_repo(org, repo)
meta_url = urljoin(
app.app_settings.config.hf_url_base(),
app.state.app_settings.config.hf_url_base(),
f"/api/{repo_type}/{org_repo}/revision/{commit}",
)
# proxy
Expand Down
4 changes: 2 additions & 2 deletions src/olah/proxy/pathsinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def pathsinfo_generator(
headers["authorization"] = authorization

# save
repos_path = app.app_settings.config.repos_path
repos_path = app.state.app_settings.config.repos_path

final_content = []
for path in paths:
Expand All @@ -88,7 +88,7 @@ async def pathsinfo_generator(

org_repo = get_org_repo(org, repo)
pathsinfo_url = urljoin(
app.app_settings.config.hf_url_base(),
app.state.app_settings.config.hf_url_base(),
f"/api/{repo_type}/{org_repo}/paths-info/{commit}",
)
# proxy
Expand Down
4 changes: 2 additions & 2 deletions src/olah/proxy/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def tree_generator(
headers["authorization"] = authorization

# save
repos_path = app.app_settings.config.repos_path
repos_path = app.state.app_settings.config.repos_path
save_dir = os.path.join(
repos_path, f"api/{repo_type}/{org}/{repo}/tree/{commit}/{path}"
)
Expand All @@ -94,7 +94,7 @@ async def tree_generator(

org_repo = get_org_repo(org, repo)
tree_url = urljoin(
app.app_settings.config.hf_url_base(),
app.state.app_settings.config.hf_url_base(),
f"/api/{repo_type}/{org_repo}/tree/{commit}/{path}",
)
# proxy
Expand Down
Loading

0 comments on commit e76c553

Please sign in to comment.