restructure + parser + readme updates
This commit is contained in:
parent
c726e6cf28
commit
b4265fe9c3
10 changed files with 778 additions and 149 deletions
6
backends/backend.cpp
Normal file
6
backends/backend.cpp
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "../http.cpp"
|
||||
|
||||
class backend {
|
||||
public:
|
||||
virtual http_response handle_request(http_request req) {};
|
||||
};
|
21
backends/file_backend.cpp
Normal file
21
backends/file_backend.cpp
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "backend.cpp"
|
||||
|
||||
class file_backend : public backend {
|
||||
public:
|
||||
http_response handle_request(http_request req) {
|
||||
string filename = req.path() == "/" ? "index.html" : req.path();
|
||||
filename = "./www/" + filename;
|
||||
ifstream stream(filename);
|
||||
|
||||
int status = 200;
|
||||
if (!stream.is_open()) {
|
||||
status = 404;
|
||||
filename = "./error_pages/404.html";
|
||||
stream = ifstream(filename);
|
||||
}
|
||||
|
||||
stringstream buffer;
|
||||
buffer << stream.rdbuf();
|
||||
return http_response(buffer.str(), status);
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue