-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb-server-commander.cpp
More file actions
46 lines (40 loc) · 1.58 KB
/
web-server-commander.cpp
File metadata and controls
46 lines (40 loc) · 1.58 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
#include "web-server-commander.hpp"
void WebServerCommander::run(std::string_view cmdline) {
if(cmdline == "help") {
help();
return;
}
if(auto iter=mCommandMap.find(cmdline.data()); iter != std::end(mCommandMap)) {
auto&[func, _] = iter->second;
func();
} else {
std::cerr << "Invalid Command => \'" << cmdline << '\'' << std::endl;
}
}
void WebServerCommander::help() {
for(auto& iter : mCommandMap) {
auto& cmd = iter.first;
auto&[_, desc] = iter.second;
std::cout << "- " << cmd << " => " << desc << std::endl;
}
}
void WebServerCommander::init() {
if(was == nullptr) {
std::runtime_error("Was is nullptr");
}
addCommand("run", [&](){was->run();}, "Start HTTP Server");
addCommand("stop", [&](){was->stop();},"Stop HTTP Server");
addCommand("state", [&](){std::cout << was->getState() << std::endl;},"Show Service Info");
addCommand("restart", [&](){was->restart();}, "Restart HTTP Server");
addCommand("reload", [&](){was->reload();}, "Auto reload html pages");
addCommand("quit", [&](){exit(0);}, "Quit Application");
}
void WebServerCommander::addCommand(std::string_view cmd, std::function<void()> func, std::string desc) {
if(auto iter=mCommandMap.find(cmd.data()); iter == std::end(mCommandMap)) {
mCommandMap.insert({cmd.data(), pack(func, desc)});
} else {
std::stringstream ss;
ss << "Add Command Failed: " << cmd << " is already exists" << std::endl;
throw std::runtime_error(ss.str());
}
}