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()){
|
if(f.is_open()){
|
||||||
while(std::getline(f, line)){
|
while(std::getline(f, line)){
|
||||||
ret += line;
|
ret += line;
|
||||||
|
ret += "\n";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ret = "Failed to open proc filesystem";
|
ret = "Failed to open proc filesystem";
|
||||||
|
@ -9,13 +9,19 @@
|
|||||||
void setRoutes(crow::SimpleApp& app){
|
void setRoutes(crow::SimpleApp& app){
|
||||||
CROW_ROUTE(app, "/proc/meminfo")([](const crow::request& req){
|
CROW_ROUTE(app, "/proc/meminfo")([](const crow::request& req){
|
||||||
crow::json::wvalue json;
|
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))
|
if(accept == "text/plain"){
|
||||||
return crow::response(200, json.dump());
|
accept.clear();
|
||||||
else
|
status = memory::getRawProcMem(accept);
|
||||||
return crow::response(503, json.dump());
|
return crow::response(status ? 200 : 503, accept);
|
||||||
|
} else {
|
||||||
|
status = memory::getProcMem(json);
|
||||||
|
return crow::response(status ? 200 : 503, json.dump());
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
CROW_ROUTE(app, "/mem")([]{
|
CROW_ROUTE(app, "/mem")([]{
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include <crow.h>
|
#include <crow.h>
|
||||||
#include "components/memory.hpp"
|
#include "components/memory.hpp"
|
||||||
|
Loading…
Reference in New Issue
Block a user