37 lines
837 B
C++
37 lines
837 B
C++
|
#include "routes.h"
|
||
|
|
||
|
|
||
|
std::string RouteHandler::get_response_by_request(const std::string& request) {
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
if (request.find("GET /current") != std::string::npos) {
|
||
|
return get_current_temperature();
|
||
|
}
|
||
|
|
||
|
return format_http_response("404 NOT FOUND", "404 Not Found");
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
std::string RouteHandler::get_current_temperature() {
|
||
|
std::string temperature_info = (
|
||
|
"{\"temperature\": \"22°C\", \"weather\": \"good\"}"
|
||
|
);
|
||
|
return format_http_response("200 OK", temperature_info);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
std::string RouteHandler::format_http_response(std::string status, std::string text) {
|
||
|
return (
|
||
|
"HTTP/1.1 " + status + "\r\n"
|
||
|
"Content-Type: application/json\r\n"
|
||
|
"Content-Length: " + std::to_string(text.length()) + "\r\n"
|
||
|
"\r\n"
|
||
|
+ text
|
||
|
);
|
||
|
}
|