diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a7cb7d..f0c4018 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,5 +8,12 @@ set(CMAKE_BUILD_TYPE Debug) set(CMAKE_CXX_FLAGS "-Wall -g -fsanitize=address") add_executable(Webserver "src/Entry.cpp" "src/Webserver.cpp" "src/HTTP.cpp" - "libs/QuickDigest5/quickdigest5.cpp") + "libs/QuickDigest5/quickdigest5.cpp" "src/Helpers.cpp") + +add_custom_command( + TARGET Webserver + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/www + $/www) + target_link_libraries(Webserver PRIVATE) diff --git a/src/Entry.cpp b/src/Entry.cpp index aa4cb7d..de7ae3e 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -1,4 +1,6 @@ #include "main.hpp" +#include +#include // Entry point a.k.a "main.cpp" // You do not want to program here 99% of the time diff --git a/src/HTTP.cpp b/src/HTTP.cpp index a1ad841..a156ecb 100644 --- a/src/HTTP.cpp +++ b/src/HTTP.cpp @@ -1,10 +1,5 @@ #include "Helpers.hpp" #include "main.hpp" -#include -#include -#include -#include -#include void HTTPrequest::start() { // Possible Logging here @@ -67,9 +62,7 @@ void HTTPrequest::processRequest( switch (Helpers::Pathhash(requestPath)) { case "/"_hash: responseStream << "200 OK" << responseMeta - << "Hello " - "you!

Test

pretty " - "cool

"; + << Helpers::ReadFile("www/index.html"); break; } writeData(responseStream.str()); diff --git a/src/Helpers.cpp b/src/Helpers.cpp new file mode 100644 index 0000000..aaf1138 --- /dev/null +++ b/src/Helpers.cpp @@ -0,0 +1,11 @@ +#include "Helpers.hpp" +#include +#include + +// Should add caching here +std::string Helpers::ReadFile(std::string Path) { + std::ifstream file(Path); + std::stringstream contents; + contents << file.rdbuf(); + return contents.str(); +} diff --git a/src/Helpers.hpp b/src/Helpers.hpp index 3a28b2c..43c2a43 100644 --- a/src/Helpers.hpp +++ b/src/Helpers.hpp @@ -2,10 +2,16 @@ // https://medium.com/@ryan_forrester_/using-switch-statements-with-strings-in-c-a-complete-guide-efa12f64a59d #include #include +#include #include -// This is called a polynomial rolling hash, prob going to collide namespace Helpers { +std::string ReadFile(std::string Path); + +// =========== +// Hashing +// =========== +// This is called a polynomial rolling hash, prob going to collide constexpr uint64_t Pathhash(std::string_view s) { uint64_t res = 0; for (uint8_t c : s) { diff --git a/src/Webserver.cpp b/src/Webserver.cpp index 3bafb83..9a49d47 100644 --- a/src/Webserver.cpp +++ b/src/Webserver.cpp @@ -1,4 +1,5 @@ #include "main.hpp" +#include // Webserver is defined at main.hpp, you probably do not want to code here Webserver::Webserver(asio::io_context &context) diff --git a/src/main.hpp b/src/main.hpp index 8b217e5..5925f63 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -10,11 +10,7 @@ #include #include -#include -#include -#include #include -#include #include #define IP "127.0.0.1" diff --git a/www/index.html b/www/index.html index 3a26ee9..a6792bf 100644 --- a/www/index.html +++ b/www/index.html @@ -8,7 +8,7 @@ - +

Test