Skip to content

Commit

Permalink
feat: add new api rule for cellguide cxgs (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
atarashansky authored Feb 23, 2024
1 parent 56f68fa commit 790682b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
8 changes: 5 additions & 3 deletions client/src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export const HEADER_HEIGHT_PX = 56;
export const QUERY_PARAM_EXPLAIN_NEW_TAB = "explainNewTab";

/**
* Matches "/" followed by "ONE_OR_MORE_ANY_CHAR/ONE_OR_MORE_ANY_CHAR_EXCEPT_FORWARD_SLASH/" and ending with "api". Must
* exclude forward slash to prevent matches on multiple path segments (e.g. /cellxgene/d/uuid.cxg).
* This regular expression is designed to match specific API path patterns. It looks for a leading slash "/",
* followed by any number of characters until another slash "/", then any characters except for a forward slash "/",
* ending with another slash "/" and the string "api". The pattern is careful to exclude forward slashes in certain
* positions to avoid matching across multiple path segments, such as in the example path "/cellxgene/d/uuid.cxg".
*/
const REGEX_PATHNAME = /(?<=\/)\w+\/[^/]+\/(?=api)/;
const REGEX_PATHNAME = /(?<=\/)\w+(\/[^/]+)*\/[^/]+\/(?=api)/;

/* Config links types */
export type ConfigLink = "about-dataset" | "collections-home-page";
Expand Down
28 changes: 21 additions & 7 deletions server/app/api/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def get_api_dataroot_resources(bp_dataroot, url_dataroot=None):

def add_resource(resource, url):
"""convenience function to make the outer function less verbose"""
api.add_resource(resource, url, resource_class_args=(url_dataroot,))
api.add_resource(resource, url, resource_class_args=(url_dataroot,), strict_slashes=False)

# Initialization routes
add_resource(S3URIAPI, "/s3_uri")
Expand All @@ -202,7 +202,7 @@ def get_api_s3uri_resources(bp_dataroot, s3uri_path):

def add_resource(resource, url):
"""convenience function to make the outer function less verbose"""
api.add_resource(resource, url, resource_class_args=(s3uri_path,))
api.add_resource(resource, url, resource_class_args=(s3uri_path,), strict_slashes=False)

# Initialization routes
add_resource(SchemaAPI, "/schema")
Expand All @@ -222,12 +222,12 @@ def add_resource(resource, url):
return api


def register_api_v3(app, app_config, api_url_prefix):
def register_api_v3(app, app_config, api_url_prefix, cellguide_api_url_prefix):
api_version = "/api/v0.3"

s3uri_api_path = "s3_uri"
bp_s3uri = Blueprint(
f"api_dataset_{s3uri_api_path}_{api_version.replace('.',',')}",
f"api_{s3uri_api_path}_{api_version.replace('.',',')}",
__name__,
url_prefix=(f"{api_url_prefix}/{s3uri_api_path}/<s3_uri>" + api_version).replace("//", "/"),
)
Expand All @@ -236,21 +236,35 @@ def register_api_v3(app, app_config, api_url_prefix):

Api(app).add_resource(VersionAPI, "/deployed_version")

# NOTE: These routes only allow the dataset to be in the directory
# of the dataroot, and not a subdirectory. We may want to change
# the route format at some point
for dataroot_dict in app_config.server__multi_dataset__dataroots.values():
url_dataroot = dataroot_dict["base_url"]

bp_dataroot = Blueprint(
name=f"api_dataset_{url_dataroot}_{api_version.replace('.',',')}",
import_name=__name__,
url_prefix=(f"{api_url_prefix}/{url_dataroot}/<string:dataset>" + api_version).replace("//", "/"),
)
dataroot_resources = get_api_dataroot_resources(bp_dataroot, url_dataroot)
app.register_blueprint(dataroot_resources.blueprint)

bp_dataroot_cg = Blueprint(
name=f"api_dataset_{url_dataroot}_cellguide_cxgs_{api_version.replace('.',',')}",
import_name=__name__,
url_prefix=(f"{api_url_prefix}/{url_dataroot}/<path:dataset>" + api_version).replace("//", "/"),
)

dataroot_resources_cg = get_api_dataroot_resources(bp_dataroot_cg, url_dataroot)
app.register_blueprint(dataroot_resources_cg.blueprint)

app.add_url_rule(
f"/{url_dataroot}/<string:dataset>/static/<path:filename>",
f"static_assets_{url_dataroot}",
view_func=lambda dataset, filename: send_from_directory("../common/web/static", filename),
methods=["GET"],
)
app.add_url_rule(
f"/{url_dataroot}/<path:dataset>/static/<path:filename>/",
f"static_assets_{url_dataroot}_cellguide_cxgs/",
view_func=lambda dataset, filename: send_from_directory("../common/web/static", filename),
methods=["GET"],
)
29 changes: 24 additions & 5 deletions server/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
from http import HTTPStatus
from urllib.parse import urlparse

from flask import Blueprint, Flask, Response, abort, current_app, g, make_response, redirect, render_template, request
from flask import (
Blueprint,
Flask,
Response,
abort,
current_app,
g,
make_response,
redirect,
render_template,
request,
)
from flask_restful import Api, Resource
from server_timing import Timing as ServerTiming

Expand Down Expand Up @@ -177,11 +188,13 @@ def __init__(self, app_config: AppConfig):
base_resources = get_api_base_resources(bp_base)
self.app.register_blueprint(base_resources.blueprint)

register_api_v3(app=self.app, app_config=app_config, api_url_prefix=api_url_prefix)
register_api_v3(
app=self.app,
app_config=app_config,
api_url_prefix=api_url_prefix,
cellguide_api_url_prefix=f"{api_url_prefix}cellguide-cxgs/",
)

# NOTE: These routes only allow the dataset to be in the directory
# of the dataroot, and not a subdirectory. We may want to change
# the route format at some point
for dataroot_dict in app_config.server__multi_dataset__dataroots.values():
url_dataroot = dataroot_dict["base_url"]
self.app.add_url_rule(
Expand All @@ -190,6 +203,12 @@ def __init__(self, app_config: AppConfig):
lambda dataset, url_dataroot=url_dataroot: dataset_index(url_dataroot, dataset),
methods=["GET"],
)
self.app.add_url_rule(
f"/{url_dataroot}/<path:dataset>.cxg/",
f"dataset_index_{url_dataroot}_cellguide_cxgs/",
lambda dataset, url_dataroot=url_dataroot: dataset_index(url_dataroot, f"{dataset}.cxg"),
methods=["GET"],
)

self.app.app_config = app_config

Expand Down

0 comments on commit 790682b

Please sign in to comment.