From 8cdb8d87567a5595ae3c944a1c2d10ff91f9f029 Mon Sep 17 00:00:00 2001 From: Dave Warnock Date: Mon, 11 Nov 2024 11:06:46 +0000 Subject: [PATCH 1/5] Start static directory support --- robyn/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/robyn/__init__.py b/robyn/__init__.py index f5222acd..62798b8c 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -196,6 +196,9 @@ def serve_directory( """ self.directories.append(Directory(route, directory_path, show_files_listing, index_file)) + def add_static_files_directory(self, route: str, directory_path: str) -> None: + pass + def add_request_header(self, key: str, value: str) -> None: self.request_headers.append(key, value) From 5c070574d72b7e8df8ebec06344e306fbb21a862 Mon Sep 17 00:00:00 2001 From: Dave Warnock Date: Thu, 21 Nov 2024 01:17:00 +0000 Subject: [PATCH 2/5] first working static file support --- robyn/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/robyn/__init__.py b/robyn/__init__.py index 14f0c118..bdbcacd0 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -196,8 +196,11 @@ def serve_directory( """ self.directories.append(Directory(route, directory_path, show_files_listing, index_file)) - def add_static_files_directory(self, route: str, directory_path: str) -> None: - pass + 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) From f79026b0d7962d2679b339a7049eddde9ba812cd Mon Sep 17 00:00:00 2001 From: Dave Warnock Date: Thu, 21 Nov 2024 01:17:28 +0000 Subject: [PATCH 3/5] test first working static file support --- unit_tests/test_static_directory.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 unit_tests/test_static_directory.py diff --git a/unit_tests/test_static_directory.py b/unit_tests/test_static_directory.py new file mode 100644 index 00000000..180a0d8a --- /dev/null +++ b/unit_tests/test_static_directory.py @@ -0,0 +1,6 @@ +from robyn import Robyn + + +def test_static_directory(): + app = Robyn(__file__) + app.set_static_files_directory() From 82daa5a4ed55faf57ebc10f5f1ec63c67367ea82 Mon Sep 17 00:00:00 2001 From: Dave Warnock Date: Thu, 21 Nov 2024 01:39:30 +0000 Subject: [PATCH 4/5] Subrouter static directory --- unit_tests/test_static_directory.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/unit_tests/test_static_directory.py b/unit_tests/test_static_directory.py index 180a0d8a..cef34cf5 100644 --- a/unit_tests/test_static_directory.py +++ b/unit_tests/test_static_directory.py @@ -1,6 +1,13 @@ -from robyn import Robyn +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() From 4a8c16435671a31bb0d4e61c3480682aa3316de4 Mon Sep 17 00:00:00 2001 From: Dave Warnock Date: Thu, 21 Nov 2024 01:39:46 +0000 Subject: [PATCH 5/5] subrouter static dir support --- robyn/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/robyn/__init__.py b/robyn/__init__.py index bdbcacd0..3f7af5ee 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -626,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]): """