diff --git a/examples/example_static_file.cpp b/examples/example_static_file.cpp index fbeadcce0..6d6f7de10 100644 --- a/examples/example_static_file.cpp +++ b/examples/example_static_file.cpp @@ -1,3 +1,5 @@ +//#define CROW_STATIC_DRIECTORY "alternative_directory/" +//#define CROW_STATIC_ENDPOINT "/alternative_endpoint/" #include "crow.h" int main() @@ -20,3 +22,8 @@ CROW_ROUTE(app, "/") return 0; } + +/// You can also use the `/static` directory and endpoint (the directory needs to have the same path as your executable). +/// Any file inside the static directory will have the same path it would in your filesystem. + +/// TO change the static directory or endpoint, use the macros above (replace `alternative_directory` and/or `alternative_endpoint` with your own) diff --git a/include/crow/app.h b/include/crow/app.h index fe9c5e5d9..718019179 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -161,7 +161,16 @@ namespace crow ///Run the server void run() { +#ifndef CROW_DISABLE_STATIC_DIR + route(CROW_STATIC_ENDPOINT) + ([](const crow::request&, crow::response& res, std::string file_path_partial) + { + res.set_static_file_info(CROW_STATIC_DIRECTORY + file_path_partial); + res.end(); + }); validate(); +#endif + #ifdef CROW_ENABLE_SSL if (use_ssl_) { diff --git a/include/crow/settings.h b/include/crow/settings.h index 5958e3544..729f4235c 100644 --- a/include/crow/settings.h +++ b/include/crow/settings.h @@ -25,6 +25,13 @@ #define CROW_LOG_LEVEL 1 #endif +#ifndef CROW_STATIC_DIRECTORY +#define CROW_STATIC_DIRECTORY "static/" +#endif +#ifndef CROW_STATIC_ENDPOINT +#define CROW_STATIC_ENDPOINT "/static/" +#endif + // compiler flags #if __cplusplus >= 201402L