-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
103 lines (70 loc) · 2.04 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include "include/httplib.h"
std::string host;
int port;
std::string configPath = "config.conf";
std::string readFile(std::string path) {
std::fstream file(path.c_str());
if(!file.is_open()) return "";
std::string line;
std::string text;
while(getline(file, line)) {
text.append(line);
text.append("\n");
}
file.close();
return text;
}
std::map<std::string, std::string> parseConf(std::string text) {
std::map<std::string, std::string> lines;
int pointer = 0;
bool searchingOption = true;
while(text[pointer] != '\0') { // Repeating until we're at the end of the line
std::string option = "";
std::string value = "";
while(text[pointer] != '\n') { // Each of the config items are seperated by \n
const char* character = &text[pointer];
if(searchingOption) {
if(*character == ':') {
searchingOption = false;
continue;
}
option.append(character);
} else {
value.append(character);
}
pointer++;
}
lines.insert({option, value});
searchingOption = true;
}
return lines;
}
int main() {
// Getting config data
std::string configText = readFile(configPath);
std::map<std::string, std::string> configData = parseConf(configText);
// Moving config data to variables
host = configData.at("host");
port = std::stoi(configData.at("port"));
// Server setup
std::cout << "Starting server on host: " << host << ":" << port << "\n";
httplib::Server server;
// Get
std::cout << "Setting up get requests...\n";
server.Get("/.*", [](const httplib::Request &req, httplib::Response &res) {
const std::string* remote_addr = &req.remote_addr;
const int* remote_port = &req.remote_port;
const std::string* path = &req.path;
std::cout << "Recieved a request on " << *remote_addr << ":" << *remote_port << " to " << *path << "\n";
std::string output = *path;
res.set_content(output, "text/plain");
});
// Listen
std::cout << "Listening on port " << port << "\n";
server.listen(host, port);
return 0;
}