diff --git a/docs/guides/static.md b/docs/guides/static.md index cdf8e2154..f0c544a04 100644 --- a/docs/guides/static.md +++ b/docs/guides/static.md @@ -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(, , )` macro.
Or by using `app.static_file(, )` function of Crow::App.
diff --git a/include/crow/app.h b/include/crow/app.h index 7aa60522d..ebfcf4b79 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -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) { @@ -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_STATIC_ENDPOINT)([static_dir_](crow::response& res, std::string file_path_partial) { utility::sanitize_filename(file_path_partial); @@ -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(); } @@ -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_{};