Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start static directory support #1024

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ def serve_directory(
"""
self.directories.append(Directory(route, directory_path, show_files_listing, index_file))

def set_static_files_directory(self, route: str = "static", static_directory_path: str = "static") -> None:
cwd = Path().resolve()
assert Path(static_directory_path).is_dir(), f"{static_directory_path} is not a directory of {cwd}"

self.serve_directory(route, static_directory_path)

def add_request_header(self, key: str, value: str) -> None:
self.request_headers.append(key, value)

Expand Down Expand Up @@ -620,6 +626,13 @@ def trace(self, endpoint: str, openapi_name: str = "", openapi_tags: List[str] =
def options(self, endpoint: str, openapi_name: str = "", openapi_tags: List[str] = ["options"]):
return super().options(endpoint=self.__add_prefix(endpoint), openapi_name=openapi_name, openapi_tags=openapi_tags)

def set_static_files_directory(self, route: str = "static", static_directory_path: str = "static") -> None:
cwd: str = Path().resolve()
static_path: str = f"./{self.prefix}/{static_directory_path}"
assert Path(static_path).is_dir(), f"{static_path} is not a directory of {cwd}"

self.serve_directory(f"{self.prefix}/route", static_path)


def ALLOW_CORS(app: Robyn, origins: Union[List[str], str]):
"""
Expand Down
13 changes: 13 additions & 0 deletions unit_tests/test_static_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from robyn import Robyn, SubRouter


def test_static_directory():
app = Robyn(__file__)
app.set_static_files_directory()


def test_subroute_static_directory():
app = Robyn(__file__)
subroute = SubRouter(__name__, prefix="/test-subroute")
app.include_router(subroute)
subroute.set_static_files_directory()
Loading