lunch-rest/main.cpp

140 lines
5.0 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>
2020-09-16 07:48:28 +00:00
#include <map>
2020-09-14 22:55:03 +00:00
#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"};
2020-09-16 07:48:28 +00:00
std::map<std::string, std::unique_ptr<LunchRest::Restaurant>> restaurants;
void sendResponse(const std::string &response, int status_code, const std::shared_ptr< restbed::Session > session) {
session->close(status_code, response, { { "Content-Length", std::to_string(response.length()) }, { "Access-Control-Allow-Origin", "*" } });
}
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 ) {
2020-09-16 07:48:28 +00:00
for(auto &restaurant : restaurants)
restaurant.second->parse();
2020-09-15 21:04:16 +00:00
std::string response = "Refreshed menus!";
2020-09-16 07:48:28 +00:00
sendResponse( "Refreshed menus!", restbed::OK, session );
2020-09-15 21:04:16 +00:00
}
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 << "[";
2020-09-16 07:48:28 +00:00
for(auto &restaurant : restaurants) {
2020-09-15 14:05:16 +00:00
atleastonerest = true;
2020-09-16 07:48:28 +00:00
ss << restaurant.second->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 << "]";
2020-09-16 07:48:28 +00:00
sendResponse(ss.str(), restbed::OK, session);
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;
2020-09-16 07:48:28 +00:00
std::string restaurant = "";
2020-09-15 14:05:16 +00:00
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;
2020-09-16 07:48:28 +00:00
} else if(query_param.second == "friday") {
2020-09-15 14:05:16 +00:00
day = 4;
2020-09-16 07:48:28 +00:00
} else if(query_param.second == "saturday") {
day = 5;
2020-09-15 14:05:16 +00:00
} else {
2020-09-16 07:48:28 +00:00
day = 6;
2020-09-15 14:05:16 +00:00
}
}
2020-09-16 07:48:28 +00:00
if(query_param.first == "restaurant")
restaurant = query_param.second;
}
if(day == -1 && restaurant == "") {
sendResponse("DIDN'T SPECIFY DAY OR RESTAURANT", restbed::BAD_REQUEST, session);
return;
2020-09-15 14:05:16 +00:00
}
2020-09-16 07:48:28 +00:00
if(restaurant != "" && restaurants.find(restaurant) == restaurants.end()) {
sendResponse("YOU DIDN'T SPECIFY A VALID RESTAURANT!", restbed::BAD_REQUEST, session);
2020-09-15 14:05:16 +00:00
return;
}
std::stringstream ss{};
2020-09-16 07:48:28 +00:00
if(day != -1 && restaurant != "") {
ss << "[" << restaurants[restaurant]->jsonify({day}) << "]";
2020-09-15 14:05:16 +00:00
} else if(day != -1) {
ss << "[";
bool atleastone = false;
2020-09-16 07:48:28 +00:00
for(auto &restaurant : restaurants) {
2020-09-15 14:05:16 +00:00
atleastone = true;
2020-09-16 07:48:28 +00:00
ss << restaurant.second->jsonify({day}) << ",";
2020-09-15 14:05:16 +00:00
}
if(atleastone)
ss.seekp(-1, ss.cur);
ss << "]";
2020-09-16 07:48:28 +00:00
} else if(restaurant != "") {
ss << "[" << restaurants[restaurant]->jsonify() << "]";
2020-09-15 14:05:16 +00:00
}
2020-09-16 07:48:28 +00:00
sendResponse(ss.str(), restbed::OK, session);
2020-09-15 14:05:16 +00:00
}
2020-09-14 23:30:34 +00:00
int main() {
2020-09-16 07:48:28 +00:00
restaurants["udrevaka"] = std::make_unique<LunchRest::UDrevakaRestaurant>();
restaurants["padagali"] = std::make_unique<LunchRest::PadagaliRestaurant>();
restaurants["lightofindia"] = std::make_unique<LunchRest::LightOfIndiaRestaurant>();
restaurants["ukarla"] = std::make_unique<LunchRest::UKarlaRestaurant>();
restaurants["alcapone"] = std::make_unique<LunchRest::AlCaponeRestaurant>();
restaurants["plac"] = std::make_unique<LunchRest::PlacRestaurant>();
2020-09-14 23:30:34 +00:00
std::cout << "Initial parsing" << std::endl;
2020-09-16 07:48:28 +00:00
for(auto &restaurant : restaurants)
restaurant.second->parse();
2020-09-14 23:30:34 +00:00
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
}