-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebserver.cpp
More file actions
73 lines (61 loc) · 1.68 KB
/
webserver.cpp
File metadata and controls
73 lines (61 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "webserver.hpp"
WebServer::WebServer(unsigned short port, std::string content_dir)
: port(port)
, content_dir(std::move(content_dir))
{
server.loglevel(crow::LogLevel::Warning); // Set log level to Info
}
WebServer::~WebServer()
{
server.stop();
}
void WebServer::run()
{
// Config endpoint
CROW_ROUTE(server, "/config.json")
([&]
{
// Build the JSON body
crow::json::wvalue json;
auto& ports_json = json["proxy_ports"];
for (const auto& [name, port] : proxy_ports)
ports_json[name] = port;
crow::response res{json.dump()};
res.set_header("Content-Type", "application/json");
return res;
});
// Root ‑ index.html
CROW_ROUTE(server, "/")
([&]
{
crow::response res;
res.set_static_file_info(content_dir + "/index.html");
return res;
});
// Everything else from static/
CROW_ROUTE(server, "/<path>")
([&](const std::string& path)
{
crow::response res;
res.set_static_file_info(content_dir + "/" + path);
return res;
});
auto server_future = server.port(port).multithreaded().run_async();
if (server.wait_for_server_start() != std::cv_status::no_timeout)
{
log_error("Failed to start HTTP server on port %u", port);
return;
}
log_info("HTTP server serving directory '%s' on port %u", content_dir.c_str(), port);
server_future.wait();
log_info("HTTP server stopped");
server_future.get();
}
void WebServer::stop()
{
server.stop();
}
void WebServer::add_proxy_port(const std::string& proxy_name, unsigned short port)
{
proxy_ports[proxy_name] = port;
}