| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #include <httplib.h>
- #include <iostream>
- #include "sqlitedb.h"
- namespace App {
- constexpr auto DefaultPort = 8888;
- constexpr auto DefaultHost = "0.0.0.0";
- constexpr auto ContentType = "application/json";
- static httplib::Server *srv = nullptr;
- } // namespace App
- void signalHandler(int signum)
- {
- std::cout << "\n\nInterrupt signal (" << signum << ") received.\n" << std::endl;
- if (App::srv) {
- App::srv->stop();
- delete App::srv;
- }
- exit(signum);
- }
- int main(int argc, char *argv[])
- {
- std::signal(SIGINT, signalHandler);
- App::srv = new httplib::Server();
- SqliteDb db("beer.db");
- auto read_table = [&db](const httplib::Request& req, httplib::Response& res) {
- auto table = req.path_params.at("table");
- std::vector< SqliteDb::Param> params;
- for (const auto& [param, value] : req.params)
- {
- params.push_back({ param, value });
- }
- res.set_content("Table: " + db.readTable( table, params ), "text/plain");
- };
- App::srv->Get("/table/:table", read_table);
- auto host = argc > 1 ? argv[1] : App::DefaultHost;
- auto port = argc > 2 ? atoi(argv[2]) : App::DefaultPort;
- std::cout << "Listen on " << host << ":" << port << std::endl;
- App::srv->listen(host, port);
- return 0;
- }
|