Last active
May 27, 2022 17:20
-
-
Save jfjensen/37f2b62da71ba64f4cb0bd6fdcc298ca to your computer and use it in GitHub Desktop.
The source code for the project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "./include/cinatra.hpp" | |
#include "./include/ginger.h" | |
#include <iostream> | |
#include <sstream> | |
#include <vector> | |
#include <fstream> | |
#include <map> | |
#include <string> | |
using namespace cinatra; | |
int main() { | |
http_server server(std::thread::hardware_concurrency()); | |
std::vector<std::map<std::string, ginger::object>> xs; | |
xs.push_back({ | |
{ "num", 1 }, | |
{ "img", "images/1.png" }, | |
{ "name", "strawberries" }, | |
{ "price", "$2.00" }, | |
{ "reviews", 125}, | |
{ "one", true}, { "two", true}, { "three", false}, { "four", false}, { "five", false}, | |
{ "description", "Lorem ipsum dolor sit amet, consectetur adipiscing elit."}, | |
}); | |
xs.push_back({ | |
{ "num", 2 }, | |
{ "img", "images/2.png" }, | |
{ "name", "onions" }, | |
{ "price", "$2.80" }, | |
{ "reviews", 125}, | |
{ "one", true}, { "two", true}, { "three", true}, { "four", false}, { "five", false}, | |
{ "description", "Lorem ipsum dolor sit amet, consectetur adipiscing elit."}, | |
}); | |
xs.push_back({ | |
{ "num", 3 }, | |
{ "img", "images/3.png" }, | |
{ "name", "tomatoes" }, | |
{ "price", "$3.10" }, | |
{ "reviews", 125}, | |
{ "one", true}, { "two", true}, { "three", true}, { "four", false}, { "five", false}, | |
{ "description", "Lorem ipsum dolor sit amet, consectetur adipiscing elit."}, | |
}); | |
xs.push_back({ | |
{ "num", 4 }, | |
{ "img", "images/4.png" }, | |
{ "name", "courgette" }, | |
{ "price", "$1.20" }, | |
{ "reviews", 125}, | |
{ "one", true}, { "two", false}, { "three", false}, { "four", false}, { "five", false}, | |
{ "description", "Lorem ipsum dolor sit amet, consectetur adipiscing elit."}, | |
}); | |
xs.push_back({ | |
{ "num", 5 }, | |
{ "img", "images/5.png" }, | |
{ "name", "broccoli" }, | |
{ "price", "$3.80" }, | |
{ "reviews", 125}, | |
{ "one", true}, { "two", true}, { "three", true}, { "four", true}, { "five", true}, | |
{ "description", "Lorem ipsum dolor sit amet, consectetur adipiscing elit."}, | |
}); | |
xs.push_back({ | |
{ "num", 6 }, | |
{ "img", "images/6.png" }, | |
{ "name", "potatoes" }, | |
{ "price", "$3.00" }, | |
{ "reviews", 125}, | |
{ "one", true}, { "two", true}, { "three", true}, { "four", false}, { "five", false}, | |
{ "description", "Lorem ipsum dolor sit amet, consectetur adipiscing elit."}, | |
}); | |
server.listen("0.0.0.0", "8080"); | |
server.set_http_handler<GET, POST>("/index", [xs](request& req, response& res) { | |
std::map<std::string, ginger::object> t; | |
t["title"] = "Organic Products For Sale"; | |
t["xs"] = xs; | |
std::ifstream tmpl("./templates/product.html"); | |
std::stringstream ss; | |
ss << tmpl.rdbuf(); | |
std::stringstream output; | |
try { | |
ginger::parse(ss.str(), t, ginger::from_ios(output)); | |
} catch (ginger::parse_error& error) { | |
std::cerr << error.long_error() << std::endl; | |
} | |
res.set_status_and_content(status_type::ok, output.str()); | |
}); | |
server.set_static_dir("./static/"); | |
server.run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment