Add changed response type based on Accept header
This commit is contained in:
parent
e8d3b5f2d8
commit
39b09d7495
@ -36,6 +36,7 @@ bool memory::getRawProcMem(std::string& ret){
|
||||
if(f.is_open()){
|
||||
while(std::getline(f, line)){
|
||||
ret += line;
|
||||
ret += "\n";
|
||||
}
|
||||
} else {
|
||||
ret = "Failed to open proc filesystem";
|
||||
|
@ -9,13 +9,19 @@
|
||||
void setRoutes(crow::SimpleApp& app){
|
||||
CROW_ROUTE(app, "/proc/meminfo")([](const crow::request& req){
|
||||
crow::json::wvalue json;
|
||||
bool status;
|
||||
std::string accept = req.get_header_value("Accept");
|
||||
|
||||
std::cerr << "Accept: " << req.get_header_value("Accept") << "\n";
|
||||
std::transform(accept.begin(), accept.end(), accept.begin(), ::tolower);
|
||||
|
||||
if(memory::getProcMem(json))
|
||||
return crow::response(200, json.dump());
|
||||
else
|
||||
return crow::response(503, json.dump());
|
||||
if(accept == "text/plain"){
|
||||
accept.clear();
|
||||
status = memory::getRawProcMem(accept);
|
||||
return crow::response(status ? 200 : 503, accept);
|
||||
} else {
|
||||
status = memory::getProcMem(json);
|
||||
return crow::response(status ? 200 : 503, json.dump());
|
||||
}
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/mem")([]{
|
||||
|
@ -6,6 +6,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
#include <crow.h>
|
||||
#include "components/memory.hpp"
|
||||
|
Loading…
Reference in New Issue
Block a user