diff --git a/src/components/state.cpp b/src/components/state.cpp index f96930b..a3c4b51 100644 --- a/src/components/state.cpp +++ b/src/components/state.cpp @@ -23,6 +23,8 @@ bool state::getUptime(crow::json::wvalue& ret){ ret["uptime"] = std::stof(line.substr(0, space)); ret["idle"] = std::stof(line.substr(space+1)); + f.close(); + } else { ret["message"] = "Failed to open proc filesystem"; return false; @@ -32,5 +34,15 @@ bool state::getUptime(crow::json::wvalue& 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; } diff --git a/src/routes.cpp b/src/routes.cpp index 4758069..f73b92a 100644 --- a/src/routes.cpp +++ b/src/routes.cpp @@ -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; bool status; + std::string accept = req.get_header_value("Accept"); - status = state::getUptime(json); - return crow::response(status ? 200: 503, json.dump()); + std::transform(accept.begin(), accept.end(), accept.begin(), ::tolower); + 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")([]{