-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.cpp
65 lines (47 loc) · 2.03 KB
/
server.cpp
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
#include <chrono>
#include <map>
#include <string>
#include <thread>
#include <sstream>
#include "server_http.hpp"
#include <stdio.h>
#include <strings.h>
#include <memory>
#include "node_exporter_aix.hpp"
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
int start_server(int port, int flags) {
HttpServer server;
server.config.port = port;
server.default_resource["GET"] = [flags](std::shared_ptr<HttpServer::Response> response, std::shared_ptr<HttpServer::Request> request) {
std::ostringstream output;
auto static_labels = generate_static_labels();
if(flags & PART_COMPAT) gather_cpu_compat(output, static_labels);
if(flags & PART_COMPAT) gather_cpus_compat(output, static_labels);
if(flags & PART_CPU) gather_cpus(output, static_labels);
if(flags & PART_DISKADAPTER) gather_diskadapters(output, static_labels);
if(flags & PART_DISKPATH) gather_diskpaths(output, static_labels);
if(flags & PART_MEM_PAGES) gather_memory_pages(output, static_labels);
if(flags & PART_MEM) gather_memory(output, static_labels);
if(flags & PART_DISK) gather_disks(output, static_labels);
if(flags & PART_NETINTERFACE) gather_netinterfaces(output, static_labels);
if(flags & PART_NETADAPTER) gather_netadapters(output, static_labels);
if(flags & PART_NETBUFFER) gather_netbuffers(output, static_labels);
if(flags & PART_PARTITION) gather_partition(output, static_labels);
if(flags & PART_FILESYSTEMS) gather_filesystems(output, static_labels);
if(flags & PART_VMSTAT_V) gather_vmstat_v(output, static_labels);
if(flags & PART_FCSTAT_E) gather_fcstats(output, static_labels);
auto outstr = output.str();
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << outstr.length() << "\r\n\r\n"
<< outstr;
};
std::thread server_thread([&server]() {
// Start server
try {
server.start();
} catch (const SimpleWeb::system_error &e) {
std::cerr << "Error starting HTTP server: " << e.what() << std::endl;
}
});
server_thread.join();
return 0;
}