REST, baby!

This commit is contained in:
zvon 2020-09-15 01:30:34 +02:00
parent b3ab05c40c
commit 0349b3a300
6 changed files with 73 additions and 2 deletions

View File

@ -1,7 +1,7 @@
CXX ?= g++
CFLAGS ?= -O2 -Wall -Wextra `pkg-config libxml-2.0 --cflags` `pkg-config libxml++-3.0 --cflags`
PREFIX ?= /usr/local/bin
LDFLAGS ?= -lcurl `pkg-config libxml-2.0 --libs` `pkg-config libxml++-3.0 --libs`
LDFLAGS ?= -lcurl -lrestbed `pkg-config libxml-2.0 --libs` `pkg-config libxml++-3.0 --libs`
PARSERS = udrevaka.o padagali.o lightofindia.o ukarla.o

View File

@ -5,10 +5,13 @@
#include <iostream>
#include <memory>
#include <restbed>
#include <sstream>
std::vector<std::string> days = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"};
std::vector<std::unique_ptr<LunchRest::Parser>> parsers;
int main() {
/*int main() {
std::vector<std::unique_ptr<LunchRest::Parser>> parsers;
parsers.emplace_back(new LunchRest::UDrevakaParser());
parsers.emplace_back(new LunchRest::PadagaliParser());
@ -31,4 +34,48 @@ int main() {
}
}
}
}*/
void get_all( const std::shared_ptr< restbed::Session > session ) {
std::stringstream ss{};
ss << "[";
for(auto &x : parsers) {
ss << "{\"restaurant\": \"" << x->getRestaurant() << "\", \"menus\": [";
for(const auto &y : x->getMenus()) {
if(y.isValid()) {
ss << y.jsonify() << ",";
}
}
ss.seekp(-1, ss.cur);
ss << "]},";
}
ss.seekp(-1, ss.cur);
ss << "]";
auto res = ss.str();
session->close(restbed::OK, res, { { "Content-Length", std::to_string(res.length()) } });
}
int main() {
parsers.emplace_back(new LunchRest::UDrevakaParser());
parsers.emplace_back(new LunchRest::PadagaliParser());
parsers.emplace_back(new LunchRest::LightOfIndiaParser());
parsers.emplace_back(new LunchRest::UKarlaParser());
std::cout << "Initial parsing" << std::endl;
for(auto &x : parsers)
x->parse();
std::cout << "Finished parsing" << std::endl;
auto resource = std::make_shared< restbed::Resource >();
resource->set_path("/get_all");
resource->set_method_handler( "GET", get_all );
auto settings = std::make_shared< restbed::Settings >();
settings->set_port(1984);
settings->set_default_header( "Connection", "close" );
restbed::Service service;
service.publish(resource);
service.start(settings);
return 0;
}

View File

@ -1,4 +1,5 @@
#include "meal.hpp"
#include <sstream>
std::ostream &operator<<(std::ostream &os, const LunchRest::Meal &meal) {
if(meal.isSoup())
@ -10,3 +11,9 @@ std::ostream &operator<<(std::ostream &os, const LunchRest::Meal &meal) {
os << ", " << meal.getPrice() << " czk";
return os;
}
std::string LunchRest::Meal::jsonify() const {
std::stringstream ss;
ss << "{\"name\": \"" << _name << "\", \"description\": \"" << _desc << "\", \"isSoup\": " << (_isSoup ? "true" : "false") << ", \"price\": " << _price << "}";
return ss.str();
}

View File

@ -35,6 +35,7 @@ public:
void setDesc(const std::string &desc) {
_desc = desc;
}
std::string jsonify() const;
private:
bool _isSoup = false;
std::string _name;

View File

@ -1,4 +1,5 @@
#include "menu.hpp"
#include <sstream>
void LunchRest::Menu::addMeal(bool soup, const std::string &name, const std::string &desc, int price) {
_meals.emplace_back(soup, name, desc, price);
@ -52,3 +53,17 @@ void LunchRest::Menu::setInvalidMenu() {
bool LunchRest::Menu::isValid() const {
return _valid;
}
std::string LunchRest::Menu::jsonify() const {
std::stringstream ss{};
ss << "[";
auto soupInd = getSoupIndex();
ss << getMeals()[soupInd].jsonify() << ",";
for(unsigned long int i = 0; i < getMeals().size(); i++) {
if(i != soupInd)
ss << getMeals()[i].jsonify() << ",";
}
ss.seekp(-1, ss.cur);
ss << "]";
return ss.str();
}

View File

@ -21,6 +21,7 @@ public:
unsigned long int getSoupIndex() const;
void setInvalidMenu();
bool isValid() const;
std::string jsonify() const;
private:
std::vector<Meal> _meals;
bool _valid = true;