From 72aeabd5e4beb259ff75593fde5543e647d976e7 Mon Sep 17 00:00:00 2001 From: The-EDev Date: Tue, 10 Nov 2020 03:47:30 +0300 Subject: [PATCH 1/3] added default "/static/" route for any static files can be disabled via CROW_DISABLE_STATIC_DIR def --- include/crow/app.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/crow/app.h b/include/crow/app.h index e7969e164..cb112eddd 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")>("/static/") + ([](const crow::request&, crow::response& res, std::string file_path) + { + res.set_static_file_info(file_path); + res.end(); + }); validate(); +#endif + #ifdef CROW_ENABLE_SSL if (use_ssl_) { From c4d191a7b011fc7bca712b315575b1c712a4e5ed Mon Sep 17 00:00:00 2001 From: The-EDev Date: Tue, 10 Nov 2020 03:54:30 +0300 Subject: [PATCH 2/3] added static/ prefix to path --- include/crow/app.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/crow/app.h b/include/crow/app.h index cb112eddd..e727561cb 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -165,7 +165,7 @@ namespace crow route")>("/static/") ([](const crow::request&, crow::response& res, std::string file_path) { - res.set_static_file_info(file_path); + res.set_static_file_info("static/"+file_path); res.end(); }); validate(); From 24830648d2bab29fd6700f6c187f5e82d1c2460c Mon Sep 17 00:00:00 2001 From: The-EDev Date: Tue, 10 Nov 2020 23:56:27 +0300 Subject: [PATCH 3/3] added macros for static endpoint and directory (also put some info in the example) --- examples/example_static_file.cpp | 7 +++++++ include/crow/app.h | 6 +++--- include/crow/settings.h | 7 +++++++ 3 files changed, 17 insertions(+), 3 deletions(-) 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 e9d6cd846..718019179 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -162,10 +162,10 @@ namespace crow void run() { #ifndef CROW_DISABLE_STATIC_DIR - route")>("/static/") - ([](const crow::request&, crow::response& res, std::string file_path) + route(CROW_STATIC_ENDPOINT) + ([](const crow::request&, crow::response& res, std::string file_path_partial) { - res.set_static_file_info("static/"+file_path); + res.set_static_file_info(CROW_STATIC_DIRECTORY + file_path_partial); res.end(); }); validate(); 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