lunch-rest/main.cpp

139 lines
4.9 KiB
C++
Raw Normal View History

#include "restaurant.hpp"
2020-09-14 22:55:03 +00:00
#include "menu.hpp"
#include "meal.hpp"
#include "restaurants/restaurants.hpp"
2020-09-14 22:55:03 +00:00
#include <iostream>
#include <memory>
2020-09-14 23:30:34 +00:00
#include <restbed>
#include <sstream>
2020-09-14 22:55:03 +00:00
std::vector<std::string> days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"};
std::vector<std::unique_ptr<LunchRest::Restaurant>> restaurants;
2020-09-14 22:55:03 +00:00
2020-09-15 21:04:16 +00:00
void refresh( const std::shared_ptr< restbed::Session > session ) {
for(auto &x : restaurants)
x->parse();
std::string response = "Refreshed menus!";
session->close(restbed::OK, response, { { "Content-Length", std::to_string(response.length()) }, { "Access-Control-Allow-Origin", "*" } });
}
2020-09-14 23:30:34 +00:00
void get_all( const std::shared_ptr< restbed::Session > session ) {
std::stringstream ss{};
2020-09-15 14:05:16 +00:00
bool atleastonerest = false;
2020-09-14 23:30:34 +00:00
ss << "[";
for(auto &x : restaurants) {
2020-09-15 14:05:16 +00:00
atleastonerest = true;
ss << x->jsonify() << ",";
2020-09-14 23:30:34 +00:00
}
2020-09-15 14:05:16 +00:00
if(atleastonerest)
ss.seekp(-1, ss.cur);
2020-09-14 23:30:34 +00:00
ss << "]";
auto res = ss.str();
2020-09-15 19:28:41 +00:00
session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) }, { "Access-Control-Allow-Origin", "*" } });
2020-09-14 23:30:34 +00:00
}
2020-09-15 14:05:16 +00:00
void get( const std::shared_ptr< restbed::Session > session ) {
// TODO better
int day = -1;
int restaurant = -1;
auto request = session->get_request();
for(const auto &query_param : request->get_query_parameters()) {
2020-09-15 14:05:16 +00:00
if(query_param.first == "day") {
if(query_param.second == "monday") {
day = 0;
} else if(query_param.second == "tuesday") {
day = 1;
} else if(query_param.second == "wednesday") {
day = 2;
} else if(query_param.second == "thursday") {
day = 3;
} else {
day = 4;
}
}
if(query_param.first == "restaurant") {
if(query_param.second == "udrevaka") {
restaurant = 0;
} else if(query_param.second == "padagali") {
restaurant = 1;
} else if(query_param.second == "lightofindia") {
restaurant = 2;
} else if(query_param.second == "ukarla") {
restaurant = 3;
} else {
restaurant = 4;
}
}
}
if(day == -1 && restaurant == -1) {
std::string error = "DIDN'T SPECIFY DAY OR RESTAURANT";
2020-09-15 19:28:41 +00:00
session->close(restbed::OK, error, { { "Content-Length", std::to_string(error.length()) }, { "Access-Control-Allow-Origin", "*" } });
2020-09-15 14:05:16 +00:00
return;
}
std::stringstream ss{};
if(day != -1 && restaurant != -1) {
ss << "[" << restaurants[restaurant]->jsonify({day}) << "]";
2020-09-15 14:05:16 +00:00
} else if(day != -1) {
ss << "[";
bool atleastone = false;
for(auto &x : restaurants) {
2020-09-15 14:05:16 +00:00
atleastone = true;
2020-09-15 15:28:46 +00:00
ss << x->jsonify({day}) << ",";
2020-09-15 14:05:16 +00:00
}
if(atleastone)
ss.seekp(-1, ss.cur);
ss << "]";
} else if(restaurant != -1) {
ss << "[" << restaurants[restaurant]->jsonify() << "]";
2020-09-15 14:05:16 +00:00
}
auto res = ss.str();
2020-09-15 19:28:41 +00:00
session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) }, { "Access-Control-Allow-Origin", "*" } });
2020-09-15 14:05:16 +00:00
}
2020-09-14 23:30:34 +00:00
int main() {
restaurants.emplace_back(new LunchRest::UDrevakaRestaurant());
restaurants.emplace_back(new LunchRest::PadagaliRestaurant());
restaurants.emplace_back(new LunchRest::LightOfIndiaRestaurant());
restaurants.emplace_back(new LunchRest::UKarlaRestaurant());
restaurants.emplace_back(new LunchRest::AlCaponeRestaurant());
2020-09-14 23:30:34 +00:00
std::cout << "Initial parsing" << std::endl;
for(auto &x : restaurants)
2020-09-14 23:30:34 +00:00
x->parse();
std::cout << "Finished parsing" << std::endl;
2020-09-15 14:05:16 +00:00
restbed::Service service;
auto getallserv = std::make_shared< restbed::Resource >();
getallserv->set_path("/get_all");
getallserv->set_method_handler( "GET", get_all );
service.publish(getallserv);
auto getserv = std::make_shared< restbed::Resource >();
getserv->set_path("/get");
getserv->set_method_handler( "GET", get );
service.publish(getserv);
2020-09-15 21:04:16 +00:00
auto refreshserv = std::make_shared< restbed::Resource >();
refreshserv->set_path("/refresh");
refreshserv->set_method_handler( "GET", refresh );
service.publish(refreshserv);
2020-09-15 14:05:16 +00:00
auto ssl_settings = std::make_shared<restbed::SSLSettings>();
ssl_settings->set_http_disabled(true);
ssl_settings->set_private_key(restbed::Uri("file:///home/zvon/data/programming/lunch-rest/example.key"));
ssl_settings->set_certificate(restbed::Uri("file:///home/zvon/data/programming/lunch-rest/example.crt"));
ssl_settings->set_temporary_diffie_hellman(restbed::Uri("file:///home/zvon/data/programming/lunch-rest/dhparams.pem"));
ssl_settings->set_port(1985);
2020-09-14 23:30:34 +00:00
auto settings = std::make_shared< restbed::Settings >();
settings->set_port(1984);
settings->set_default_header( "Connection", "close" );
2020-09-15 14:05:16 +00:00
settings->set_ssl_settings(ssl_settings);
2020-09-14 23:30:34 +00:00
service.start(settings);
return 0;
2020-09-14 22:55:03 +00:00
}