155 lines
5.6 KiB
C++
155 lines
5.6 KiB
C++
#include "environment.hpp"
|
|
#include "meal.hpp"
|
|
#include "menu.hpp"
|
|
#include "restaurant.hpp"
|
|
#include "restaurants/restaurants.hpp"
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <restbed>
|
|
#include <sstream>
|
|
#include <time.h>
|
|
|
|
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,
|
|
const std::string &content_type = "text/plain") {
|
|
session->close(status_code, response,
|
|
{ { "Content-Length", std::to_string(response.length()) },
|
|
{ "Access-Control-Allow-Origin", "*" },
|
|
{ "Content-Type", content_type } });
|
|
}
|
|
|
|
void refresh(const std::shared_ptr<restbed::Session> session) {
|
|
for (auto &restaurant : restaurants)
|
|
restaurant.second->parse();
|
|
std::string response = "Refreshed menus!";
|
|
sendResponse("Refreshed menus!", restbed::OK, session);
|
|
}
|
|
|
|
std::string get_all_json(const std::vector<int> &days = { 0, 1, 2, 3, 4, 5,
|
|
6 }) {
|
|
std::stringstream ss{};
|
|
bool atleastonerest = false;
|
|
ss << "[";
|
|
for (auto &restaurant : restaurants) {
|
|
atleastonerest = true;
|
|
ss << restaurant.second->jsonify(days) << ",";
|
|
}
|
|
if (atleastonerest)
|
|
ss.seekp(-1, ss.cur);
|
|
ss << "]";
|
|
return ss.str();
|
|
}
|
|
|
|
void get_all(const std::shared_ptr<restbed::Session> session) {
|
|
sendResponse(get_all_json(), restbed::OK, session, "application/json");
|
|
}
|
|
|
|
void get(const std::shared_ptr<restbed::Session> session) {
|
|
// TODO better
|
|
int day = -1;
|
|
std::string restaurant = "";
|
|
auto request = session->get_request();
|
|
for (const auto &query_param : request->get_query_parameters()) {
|
|
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 if (query_param.second == "friday") {
|
|
day = 4;
|
|
} else if (query_param.second == "saturday") {
|
|
day = 5;
|
|
} else {
|
|
day = 6;
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
|
|
if (restaurant != "" && restaurants.find(restaurant) == restaurants.end()) {
|
|
sendResponse("YOU DIDN'T SPECIFY A VALID RESTAURANT!",
|
|
restbed::BAD_REQUEST, session);
|
|
return;
|
|
}
|
|
|
|
std::string response{};
|
|
if (day != -1 && restaurant != "") {
|
|
response = "[" + restaurants[restaurant]->jsonify({ day }) + "]";
|
|
} else if (day != -1) {
|
|
response = get_all_json({ day });
|
|
} else if (restaurant != "") {
|
|
response = "[" + restaurants[restaurant]->jsonify() + "]";
|
|
}
|
|
sendResponse(response, restbed::OK, session, "application/json");
|
|
}
|
|
|
|
int main(int /*UNUSED*/, char ** /*UNUSED*/, char **env) {
|
|
setupEnv(env);
|
|
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>();
|
|
restaurants["zo"] = std::make_unique<LunchRest::ZoRestaurant>();
|
|
restaurants["suzies"] = std::make_unique<LunchRest::SuziesRestaurant>();
|
|
restaurants["tao"] = std::make_unique<LunchRest::TaoRestaurant>();
|
|
restaurants["mahostina"] = std::make_unique<LunchRest::MahostinaRestaurant>();
|
|
std::cout << "Initial parsing" << std::endl;
|
|
for (auto &restaurant : restaurants)
|
|
restaurant.second->parse();
|
|
std::cout << "Finished parsing" << std::endl;
|
|
|
|
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);
|
|
|
|
auto refreshserv = std::make_shared<restbed::Resource>();
|
|
refreshserv->set_path("/refresh");
|
|
refreshserv->set_method_handler("GET", refresh);
|
|
service.publish(refreshserv);
|
|
|
|
/* 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);*/
|
|
|
|
auto settings = std::make_shared<restbed::Settings>();
|
|
settings->set_port(1984);
|
|
settings->set_default_header("Connection", "close");
|
|
// settings->set_ssl_settings(ssl_settings);
|
|
|
|
service.start(settings);
|
|
|
|
return 0;
|
|
}
|