#include "environment.hpp" #include "meal.hpp" #include "menu.hpp" #include "restaurant.hpp" #include "restaurants/menicka.hpp" #include "restaurants/restaurants.hpp" #include #include #include #include #include #include #include std::map> restaurants; void sendResponse(const std::string &response, int status_code, const std::shared_ptr 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 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 &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 session) { sendResponse(get_all_json(), restbed::OK, session, "application/json"); } void get(const std::shared_ptr 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("https://www.menicka.cz/2752-u-drevaka-beergrill.html", "U Dřeváka"); restaurants["padagali"] = std::make_unique("https://www.menicka.cz/4116-padagali.html", "Padagali"); restaurants["lightofindia"] = std::make_unique("https://www.menicka.cz/5448-light-of-india.html", "Light of India"); restaurants["ukarla"] = std::make_unique(); restaurants["alcapone"] = std::make_unique("https://www.menicka.cz/2609-pizzeria-al-capone.html", "Al Capone"); restaurants["suzies"] = std::make_unique("https://www.menicka.cz/3830-suzies-steak-pub.html", "Suzies"); restaurants["tao"] = std::make_unique(); restaurants["mahostina"] = std::make_unique(); restaurants["divabara"] = std::make_unique("https://www.menicka.cz/6468-diva-bara.html", "Divá Bára"); 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(); getallserv->set_path("/get_all"); getallserv->set_method_handler("GET", get_all); service.publish(getallserv); auto getserv = std::make_shared(); getserv->set_path("/get"); getserv->set_method_handler("GET", get); service.publish(getserv); auto refreshserv = std::make_shared(); refreshserv->set_path("/refresh"); refreshserv->set_method_handler("GET", refresh); service.publish(refreshserv); /* auto ssl_settings = std::make_shared(); 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(); settings->set_port(1984); settings->set_default_header("Connection", "close"); // settings->set_ssl_settings(ssl_settings); service.start(settings); return 0; }