Add uptime route

This commit is contained in:
Tyler Perkins 2022-04-29 12:05:34 -04:00
parent b8df32111d
commit f49c557f82
4 changed files with 64 additions and 0 deletions

36
src/components/state.cpp Normal file
View File

@ -0,0 +1,36 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 29-4-22
// System functions implementation
//
#include "state.hpp"
bool state::getUptime(crow::json::wvalue& ret){
std::ifstream f ("/proc/uptime");
std::string line;
if(f.is_open()){
int space = -1;
std::getline(f, line);
for(int i = 0; line[i] != '\0'; ++i){
if(line[i] == ' '){
space = i;
break;
}
}
ret["uptime"] = std::stof(line.substr(0, space));
ret["idle"] = std::stof(line.substr(space+1));
} else {
ret["message"] = "Failed to open proc filesystem";
return false;
}
return true;
}
bool state::getRawUptime(std::string& ret){
return true;
}

17
src/components/state.hpp Normal file
View File

@ -0,0 +1,17 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 29-4-22
// System functions definitions
//
#pragma once
#include <crow.h>
#include <string>
#include <fstream>
namespace state{
bool getUptime(crow::json::wvalue&);
bool getRawUptime(std::string&);
}

View File

@ -24,7 +24,17 @@ void setRoutes(crow::SimpleApp& app){
} }
}); });
CROW_ROUTE(app, "/proc/uptime")([](){
crow::json::wvalue json;
bool status;
status = state::getUptime(json);
return crow::response(status ? 200: 503, json.dump());
});
CROW_ROUTE(app, "/mem")([]{ CROW_ROUTE(app, "/mem")([]{
return "mem"; return "mem";
}); });
} }

View File

@ -11,5 +11,6 @@
#include <crow.h> #include <crow.h>
#include "components/memory.hpp" #include "components/memory.hpp"
#include "components/state.hpp"
void setRoutes(crow::SimpleApp&); void setRoutes(crow::SimpleApp&);