# Serving Static File

This is useful for exposing assets such as stylesheets, JavaScript files, images, fonts, and other public files required by your application.

Static file serving is configured at the application level using `mount_static()`.

## Basic Usage

Create a directory for your static assets:

```
project/
├── main.py
├── static/
│   ├── style.css
│   └── logo.png
```

Configure Slush to serve files from that directory:

```python
from slush.app import Slush

app = Slush()

app.mount_static("/static", "./static")
```

After mounting the directory, files inside `./static` will be available under the `/static` URL prefix.

For example:

```
/static/style.css
/static/logo.png
```

## Serving a Static File

If your project contains this file:

```
static/style.css
```

You can access it in the browser at:

```
http://127.0.0.1:8000/static/style.css
```

Slush will read the file from disk and return it with the appropriate content type.

## URL Prefix and Directory

The `mount_static()` method accepts two arguments:

```python
app.mount_static(url_prefix="/static", directory="./static")
```

### `url_prefix`

The public URL prefix used to access static files.

```python
app.mount_static("/assets", "./static")
```

With this configuration, files are served from:

```
/assets/filename
```

### `directory`

The local directory on disk where static files are stored.

```python
app.mount_static("/static", "./public")
```

With this configuration, files are loaded from the `./public` directory.

## Example with HTML

Static files are commonly used together with templates or HTML responses.

```html
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/static/style.css">
</head>
<body>
    <h1>Hello from Slush</h1>
</body>
</html>
```

If `style.css` exists inside the configured static directory, Slush will serve it automatically.

## Multiple Static Directories

You can mount more than one static directory by using different URL prefixes.

```python
app.mount_static("/static", "./static")
app.mount_static("/media", "./media")
```

This allows you to organize different types of public files separately.

Example:

```
/static/style.css
/media/avatar.png
```

## Missing Files

If a requested static file does not exist, Slush returns a `404 Not Found` response.

Example:

```
/static/missing.css
```

If `missing.css` is not present in the configured static directory, the request will return a 404 response.

## Path Safety

Slush protects static file serving from path traversal attempts.

For example, requests such as:

```
/static/../secret.txt
```

are blocked and do not expose files outside the configured static directory.

## When to Use Static File Serving

Built-in static file serving is useful during development and for small applications where you want a simple way to expose assets.

For larger production deployments, you may prefer to serve static assets through a dedicated web server, reverse proxy, CDN, or object storage service.

## Summary

Static file serving in Slush allows you to:

* Mount a local directory under a public URL prefix
* Serve CSS, JavaScript, images, and other assets
* Use static files with templates or HTML responses
* Mount multiple static directories
* Safely prevent access to files outside the configured directory


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://slush.gitbook.io/docs/documentation/basics/serving-static-file.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
