Merge pull request #50 from mrozigor/static_dir

Added default "static/<path>" route for any static files
This commit is contained in:
Farook Al-Sammarraie 2020-11-11 22:25:45 +03:00 committed by GitHub
commit d00ec5e690
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 0 deletions

View File

@ -1,3 +1,5 @@
//#define CROW_STATIC_DRIECTORY "alternative_directory/"
//#define CROW_STATIC_ENDPOINT "/alternative_endpoint/<path>"
#include "crow.h" #include "crow.h"
int main() int main()
@ -20,3 +22,8 @@ CROW_ROUTE(app, "/")
return 0; 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)

View File

@ -161,7 +161,16 @@ namespace crow
///Run the server ///Run the server
void run() void run()
{ {
#ifndef CROW_DISABLE_STATIC_DIR
route<crow::black_magic::get_parameter_tag(CROW_STATIC_ENDPOINT)>(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(); validate();
#endif
#ifdef CROW_ENABLE_SSL #ifdef CROW_ENABLE_SSL
if (use_ssl_) if (use_ssl_)
{ {

View File

@ -25,6 +25,13 @@
#define CROW_LOG_LEVEL 1 #define CROW_LOG_LEVEL 1
#endif #endif
#ifndef CROW_STATIC_DIRECTORY
#define CROW_STATIC_DIRECTORY "static/"
#endif
#ifndef CROW_STATIC_ENDPOINT
#define CROW_STATIC_ENDPOINT "/static/<path>"
#endif
// compiler flags // compiler flags
#if __cplusplus >= 201402L #if __cplusplus >= 201402L