lunch-rest/main.cpp

154 lines
6.0 KiB
C++
Raw Permalink Normal View History

2020-09-20 14:27:49 +00:00
#include "environment.hpp"
2020-09-14 22:55:03 +00:00
#include "meal.hpp"
2022-02-24 15:25:21 +00:00
#include "menu.hpp"
#include "restaurant.hpp"
2022-03-03 07:49:58 +00:00
#include "restaurants/menicka.hpp"
#include "restaurants/restaurants.hpp"
2020-09-14 22:55:03 +00:00
2021-05-03 11:41:20 +00:00
#include <fstream>
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>
2021-05-03 11:41:20 +00:00
#include <time.h>
2020-09-14 22:55:03 +00:00
2020-09-16 07:48:28 +00:00
std::map<std::string, std::unique_ptr<LunchRest::Restaurant>> restaurants;
2022-02-24 15:25:21 +00:00
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 } });
2020-09-16 07:48:28 +00:00
}
2020-09-14 22:55:03 +00:00
2022-02-24 15:25:21 +00:00
void refresh(const std::shared_ptr<restbed::Session> session) {
for (auto &restaurant : restaurants)
2020-09-16 07:48:28 +00:00
restaurant.second->parse();
2020-09-15 21:04:16 +00:00
std::string response = "Refreshed menus!";
2022-02-24 15:25:21 +00:00
sendResponse("Refreshed menus!", restbed::OK, session);
2020-09-15 21:04:16 +00:00
}
2022-02-24 15:25:21 +00:00
std::string get_all_json(const std::vector<int> &days = { 0, 1, 2, 3, 4, 5,
6 }) {
2020-09-14 23:30:34 +00:00
std::stringstream ss{};
2020-09-15 14:05:16 +00:00
bool atleastonerest = false;
2020-09-14 23:30:34 +00:00
ss << "[";
2022-02-24 15:25:21 +00:00
for (auto &restaurant : restaurants) {
2020-09-15 14:05:16 +00:00
atleastonerest = true;
2022-02-24 15:25:21 +00:00
ss << restaurant.second->jsonify(days) << ",";
2020-09-14 23:30:34 +00:00
}
2022-02-24 15:25:21 +00:00
if (atleastonerest)
2020-09-15 14:05:16 +00:00
ss.seekp(-1, ss.cur);
2020-09-14 23:30:34 +00:00
ss << "]";
2021-05-03 11:41:20 +00:00
return ss.str();
}
2022-02-24 15:25:21 +00:00
void get_all(const std::shared_ptr<restbed::Session> session) {
sendResponse(get_all_json(), restbed::OK, session, "application/json");
2020-09-14 23:30:34 +00:00
}
2022-02-24 15:25:21 +00:00
void get(const std::shared_ptr<restbed::Session> session) {
2020-09-15 14:05:16 +00:00
// 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();
2022-02-24 15:25:21 +00:00
for (const auto &query_param : request->get_query_parameters()) {
if (query_param.first == "day") {
if (query_param.second == "monday") {
2020-09-15 14:05:16 +00:00
day = 0;
2022-02-24 15:25:21 +00:00
} else if (query_param.second == "tuesday") {
2020-09-15 14:05:16 +00:00
day = 1;
2022-02-24 15:25:21 +00:00
} else if (query_param.second == "wednesday") {
2020-09-15 14:05:16 +00:00
day = 2;
2022-02-24 15:25:21 +00:00
} else if (query_param.second == "thursday") {
2020-09-15 14:05:16 +00:00
day = 3;
2022-02-24 15:25:21 +00:00
} else if (query_param.second == "friday") {
2020-09-15 14:05:16 +00:00
day = 4;
2022-02-24 15:25:21 +00:00
} else if (query_param.second == "saturday") {
2020-09-16 07:48:28 +00:00
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
}
}
2022-02-24 15:25:21 +00:00
if (query_param.first == "restaurant")
2020-09-16 07:48:28 +00:00
restaurant = query_param.second;
}
2022-02-24 15:25:21 +00:00
if (day == -1 && restaurant == "") {
sendResponse("DIDN'T SPECIFY DAY OR RESTAURANT", restbed::BAD_REQUEST,
session);
2020-09-16 07:48:28 +00:00
return;
2020-09-15 14:05:16 +00:00
}
2020-09-16 07:48:28 +00:00
2022-02-24 15:25:21 +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;
}
2022-02-24 15:25:21 +00:00
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() + "]";
2020-09-15 14:05:16 +00:00
}
2022-02-24 15:25:21 +00:00
sendResponse(response, restbed::OK, session, "application/json");
2020-09-15 14:05:16 +00:00
}
2022-02-24 15:25:21 +00:00
int main(int /*UNUSED*/, char ** /*UNUSED*/, char **env) {
2020-09-20 14:27:49 +00:00
setupEnv(env);
2022-03-03 07:49:58 +00:00
restaurants["udrevaka"] = std::make_unique<LunchRest::MenickaRestaurant>("https://www.menicka.cz/2752-u-drevaka-beergrill.html", "U Dřeváka");
restaurants["padagali"] = std::make_unique<LunchRest::MenickaRestaurant>("https://www.menicka.cz/4116-padagali.html", "Padagali");
restaurants["lightofindia"] = std::make_unique<LunchRest::MenickaRestaurant>("https://www.menicka.cz/5448-light-of-india.html", "Light of India");
2020-09-16 07:48:28 +00:00
restaurants["ukarla"] = std::make_unique<LunchRest::UKarlaRestaurant>();
2022-03-03 07:49:58 +00:00
restaurants["alcapone"] = std::make_unique<LunchRest::MenickaRestaurant>("https://www.menicka.cz/2609-pizzeria-al-capone.html", "Al Capone");
restaurants["suzies"] = std::make_unique<LunchRest::MenickaRestaurant>("https://www.menicka.cz/3830-suzies-steak-pub.html", "Suzies");
2022-03-02 07:35:36 +00:00
restaurants["tao"] = std::make_unique<LunchRest::TaoRestaurant>();
2022-03-03 06:13:44 +00:00
restaurants["mahostina"] = std::make_unique<LunchRest::MahostinaRestaurant>();
2022-09-13 20:34:29 +00:00
restaurants["divabara"] = std::make_unique<LunchRest::MenickaRestaurant>("https://www.menicka.cz/6468-diva-bara.html", "Divá Bára");
2020-09-14 23:30:34 +00:00
std::cout << "Initial parsing" << std::endl;
2022-02-24 15:25:21 +00:00
for (auto &restaurant : restaurants)
2020-09-16 07:48:28 +00:00
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;
2022-02-24 15:25:21 +00:00
auto getallserv = std::make_shared<restbed::Resource>();
2020-09-15 14:05:16 +00:00
getallserv->set_path("/get_all");
2022-02-24 15:25:21 +00:00
getallserv->set_method_handler("GET", get_all);
2020-09-15 14:05:16 +00:00
service.publish(getallserv);
2022-02-24 15:25:21 +00:00
auto getserv = std::make_shared<restbed::Resource>();
2020-09-15 14:05:16 +00:00
getserv->set_path("/get");
2022-02-24 15:25:21 +00:00
getserv->set_method_handler("GET", get);
2020-09-15 14:05:16 +00:00
service.publish(getserv);
2022-02-24 15:25:21 +00:00
auto refreshserv = std::make_shared<restbed::Resource>();
2020-09-15 21:04:16 +00:00
refreshserv->set_path("/refresh");
2022-02-24 15:25:21 +00:00
refreshserv->set_method_handler("GET", refresh);
2020-09-15 21:04:16 +00:00
service.publish(refreshserv);
2022-03-02 07:35:36 +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);*/
2022-02-24 15:25:21 +00:00
auto settings = std::make_shared<restbed::Settings>();
2020-09-14 23:30:34 +00:00
settings->set_port(1984);
2022-02-24 15:25:21 +00:00
settings->set_default_header("Connection", "close");
2022-03-02 07:35:36 +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
}