Add text/plain option for /proc/uptime

This commit is contained in:
Tyler Perkins 2022-04-29 12:12:02 -04:00
parent f49c557f82
commit 22d9f6bd88
2 changed files with 23 additions and 3 deletions

View File

@ -23,6 +23,8 @@ bool state::getUptime(crow::json::wvalue& ret){
ret["uptime"] = std::stof(line.substr(0, space)); ret["uptime"] = std::stof(line.substr(0, space));
ret["idle"] = std::stof(line.substr(space+1)); ret["idle"] = std::stof(line.substr(space+1));
f.close();
} else { } else {
ret["message"] = "Failed to open proc filesystem"; ret["message"] = "Failed to open proc filesystem";
return false; return false;
@ -32,5 +34,15 @@ bool state::getUptime(crow::json::wvalue& ret){
} }
bool state::getRawUptime(std::string& ret){ bool state::getRawUptime(std::string& ret){
std::ifstream f ("/proc/uptime");
if(f.is_open()){
std::getline(f, ret);
ret += "\n";
f.close();
} else {
ret = "Failed to open proc filesystem";
return false;
}
return true; return true;
} }

View File

@ -24,13 +24,21 @@ void setRoutes(crow::SimpleApp& app){
} }
}); });
CROW_ROUTE(app, "/proc/uptime")([](){ CROW_ROUTE(app, "/proc/uptime")([](const crow::request& req){
crow::json::wvalue json; crow::json::wvalue json;
bool status; bool status;
std::string accept = req.get_header_value("Accept");
status = state::getUptime(json); std::transform(accept.begin(), accept.end(), accept.begin(), ::tolower);
return crow::response(status ? 200: 503, json.dump());
if(accept == "text/plain"){
accept.clear();
status = state::getRawUptime(accept);
return crow::response(status ? 200 : 503, accept);
} else {
status = state::getUptime(json);
return crow::response(status ? 200: 503, json.dump());
}
}); });
CROW_ROUTE(app, "/mem")([]{ CROW_ROUTE(app, "/mem")([]{