Skip to content
Open
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
7 changes: 7 additions & 0 deletions docs/guides/static.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ The static folder or endpoint can be changed by defining the macros :
```
`CROW_STATIC_DIRECTORY` changes the directory in the server's file system, while `CROW_STATIC_ENDPOINT` changes the URL that the client needs to access.

The static directory can also be set at runtime (before `run()`), which is useful when the path is only known when the process starts:
```cpp
crow::SimpleApp app;
app.static_directory("alternative_directory/");
```
`CROW_STATIC_DIRECTORY` remains the default when `static_directory()` is not called.

## Explicit
You can return a static file by using the `CROW_STATIC_FILE(<app>, <EndPoint>, <FilePath>)` macro.</br>
Or by using `app.static_file(<EndPoint>, <FilePath>)` function of Crow::App.</br>
Expand Down
23 changes: 22 additions & 1 deletion include/crow/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,23 @@ namespace crow
return bindaddr_;
}

/// \brief Set the filesystem directory for implicit static file serving.
///
/// Defaults to \ref CROW_STATIC_DIRECTORY (`"static/"`). Must be called
/// before \ref run() / \ref run_async(). Does not change
/// \ref CROW_STATIC_ENDPOINT.
self_t& static_directory(std::string path)
{
static_directory_ = std::move(path);
return *this;
}

/// \brief Get the filesystem directory used for implicit static files
std::string static_directory() const
{
return static_directory_;
}

/// \brief Disable tcp/ip and use unix domain socket instead
self_t& local_socket_path(std::string path)
{
Expand Down Expand Up @@ -602,7 +619,7 @@ namespace crow
void add_static_dir()
{
if (are_static_routes_added()) return;
auto static_dir_ = crow::utility::normalize_path(CROW_STATIC_DIRECTORY);
auto static_dir_ = crow::utility::normalize_path(static_directory_);

route<crow::black_magic::get_parameter_tag(CROW_STATIC_ENDPOINT)>(CROW_STATIC_ENDPOINT)([static_dir_](crow::response& res, std::string file_path_partial) {
utility::sanitize_filename(file_path_partial);
Expand All @@ -615,6 +632,9 @@ namespace crow
/// \brief A wrapper for `validate()` in the router
void validate()
{
// Always validate blueprints so routes remain available when
// CROW_DISABLE_STATIC_DIR skips add_blueprint()/add_static_dir().
router_.validate_bp();
router_.validate();
}

Expand Down Expand Up @@ -916,6 +936,7 @@ namespace crow
uint64_t max_payload_{UINT64_MAX};
std::string server_name_ = std::string("Crow/") + VERSION;
std::string bindaddr_ = "0.0.0.0";
std::string static_directory_ = CROW_STATIC_DIRECTORY;
bool use_unix_ = false;
detail::socket::tcp_socket_options tcp_socket_options_{};
detail::socket::tcp_socket_options websocket_tcp_socket_options_{};
Expand Down
Loading