restructuring
This commit is contained in:
parent
ac669ba504
commit
6c9f7f7c49
13 changed files with 573 additions and 496 deletions
24
src/main.cpp
24
src/main.cpp
|
|
@ -11,23 +11,21 @@
|
|||
#include <unistd.h>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void log_request_and_response(http_request& req, unique_ptr<http_response>& resp);
|
||||
void log_request_and_response(http_request& req, std::unique_ptr<http_response>& resp);
|
||||
|
||||
constexpr int default_port = 80;
|
||||
constexpr int max_worker_threads = 128;
|
||||
|
||||
int active_threads = 0;
|
||||
mutex mtx;
|
||||
condition_variable cv;
|
||||
std::mutex mtx;
|
||||
std::condition_variable cv;
|
||||
|
||||
void handle_client(anthracite_socket s, file_backend& fb)
|
||||
{
|
||||
http_request req(s);
|
||||
unique_ptr<http_response> resp = fb.handle_request(req);
|
||||
std::unique_ptr<http_response> resp = fb.handle_request(req);
|
||||
log_request_and_response(req, resp);
|
||||
string header = resp->header_to_string();
|
||||
std::string header = resp->header_to_string();
|
||||
s.send_message(header);
|
||||
s.send_message(resp->content());
|
||||
resp.reset();
|
||||
|
|
@ -47,24 +45,24 @@ int main(int argc, char** argv)
|
|||
port_number = atoi(argv[1]);
|
||||
}
|
||||
|
||||
cout << "Initializing Anthracite" << endl;
|
||||
std::cout << "Initializing Anthracite" << std::endl;
|
||||
anthracite_socket s(port_number);
|
||||
file_backend fb(argc > 2 ? argv[2] : "./www");
|
||||
cout << "Initialization Complete" << endl;
|
||||
cout << "Listening for HTTP connections on port " << port_number << endl;
|
||||
std::cout << "Initialization Complete" << std::endl;
|
||||
std::cout << "Listening for HTTP connections on port " << port_number << std::endl;
|
||||
|
||||
while (true) {
|
||||
s.wait_for_conn();
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
cv.wait(lock, [] { return active_threads < max_worker_threads; });
|
||||
active_threads++;
|
||||
thread(handle_client, s, ref(fb)).detach();
|
||||
std::thread(handle_client, s, std::ref(fb)).detach();
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void log_request_and_response(http_request& req, unique_ptr<http_response>& resp)
|
||||
void log_request_and_response(http_request& req, std::unique_ptr<http_response>& resp)
|
||||
{
|
||||
cout << "[" << resp->status_code() << " " + http_status_map.find(resp->status_code())->second + "] " + req.client_ip() + " " + http_reverse_method_map.find(req.method())->second + " " + req.path() << endl;
|
||||
std::cout << "[" << resp->status_code() << " " + http_status_map.find(resp->status_code())->second + "] " + req.client_ip() + " " + http_reverse_method_map.find(req.method())->second + " " + req.path() << std::endl;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue