1
0
forked from cat/WebBase

Reorganising further

This commit is contained in:
2025-06-26 01:59:18 +03:00
parent 095dd8ddf9
commit ea8b8c884e
8 changed files with 31 additions and 15 deletions

View File

@@ -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
$<TARGET_FILE_DIR:Webserver>/www)
target_link_libraries(Webserver PRIVATE)

View File

@@ -1,4 +1,6 @@
#include "main.hpp"
#include <exception>
#include <iostream>
// Entry point a.k.a "main.cpp"
// You do not want to program here 99% of the time

View File

@@ -1,10 +1,5 @@
#include "Helpers.hpp"
#include "main.hpp"
#include <asio/impl/write.hpp>
#include <cstddef>
#include <sstream>
#include <string>
#include <system_error>
void HTTPrequest::start() {
// Possible Logging here
@@ -67,9 +62,7 @@ void HTTPrequest::processRequest(
switch (Helpers::Pathhash(requestPath)) {
case "/"_hash:
responseStream << "200 OK" << responseMeta
<< "<html><head><title>Hello "
"you!</title></head><body><h1>Test</h1><p>pretty "
"cool</p></body></html>";
<< Helpers::ReadFile("www/index.html");
break;
}
writeData(responseStream.str());

11
src/Helpers.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include "Helpers.hpp"
#include <fstream>
#include <sstream>
// Should add caching here
std::string Helpers::ReadFile(std::string Path) {
std::ifstream file(Path);
std::stringstream contents;
contents << file.rdbuf();
return contents.str();
}

View File

@@ -2,10 +2,16 @@
// https://medium.com/@ryan_forrester_/using-switch-statements-with-strings-in-c-a-complete-guide-efa12f64a59d
#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>
// 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) {

View File

@@ -1,4 +1,5 @@
#include "main.hpp"
#include <iostream>
// Webserver is defined at main.hpp, you probably do not want to code here
Webserver::Webserver(asio::io_context &context)

View File

@@ -10,11 +10,7 @@
#include <asio/placeholders.hpp>
#include <asio/streambuf.hpp>
#include <exception>
#include <iostream>
#include <istream>
#include <string>
#include <system_error>
#include <unordered_map>
#define IP "127.0.0.1"

View File

@@ -8,7 +8,7 @@
</head>
<body>
<h1>Test</h1>
</body>
</html>