Skip to content

Commit

Permalink
style: rename add_directory to serve_directory (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox authored Aug 22, 2024
1 parent e8d6155 commit ce8d886
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
50 changes: 48 additions & 2 deletions docs_src/src/pages/documentation/api_reference/file-uploads.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Speaking of HTML files, Batman wanted to serve simple HTML strings. He was sugge

<Row>
<Col>
Batman scaled his application across multiple cores for better performance. He used the following command:
Now, that Batman was able to serve HTML files, he wanted to serve other files like CSS, JS, and images. He was suggested to use the following code:
</Col>
<Col sticky>

Expand Down Expand Up @@ -214,11 +214,57 @@ Batman scaled his application across multiple cores for better performance. He u
</Row>


### Serving Directories

<Row>
<Col>
After serving other files, Batman wanted to serve directories, e.g. to serve a React build directory or just a simple HTML/CSS/JS directory. He was suggested to use the following code:
</Col>
<Col sticky>

<CodeGroup title="Request" tag="GET" label="/hello_world">

```python {{ title: 'untyped' }}
from robyn import Robyn, serve_file

app = Robyn(__file__)


app.serve_directory(
route="/test_dir",
directory_path=os.path.join(current_file_path, "build"),
index_file="index.html",
)

app.start(port=8080)

```

```python {{ title: 'typed' }}
from robyn import Robyn, serve_file, Request

app = Robyn(__file__)


app.serve_directory(
route="/test_dir",
directory_path=os.path.join(current_file_path, "build"),
index_file="index.html",
)

app.start(port=8080)

```

</CodeGroup>
</Col>
</Row>


## What's next?

Now, Batman was ready to learn about the advanced features of Robyn. He wanted to find a way to handle form data

- [Form Data](/documentation/api_reference/form_data)



2 changes: 1 addition & 1 deletion integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ def sample_openapi_endpoint():

def main():
app.set_response_header("server", "robyn")
app.add_directory(
app.serve_directory(
route="/test_dir",
directory_path=os.path.join(current_file_path, "build"),
index_file="index.html",
Expand Down
10 changes: 9 additions & 1 deletion robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,21 @@ def after_request(self, endpoint: Optional[str] = None) -> Callable[..., None]:
"""
return self.middleware_router.add_middleware(MiddlewareType.AFTER_REQUEST, endpoint)

def add_directory(
def serve_directory(
self,
route: str,
directory_path: str,
index_file: Optional[str] = None,
show_files_listing: bool = False,
):
"""
Serves a directory at the given route
:param route str: the route at which the directory is to be served
:param directory_path str: the path of the directory to be served
:param index_file str|None: the index file to be served
:param show_files_listing bool: if the files listing should be shown or not
"""
self.directories.append(Directory(route, directory_path, show_files_listing, index_file))

def add_request_header(self, key: str, value: str) -> None:
Expand Down

0 comments on commit ce8d886

Please sign in to comment.