|
| 1 | +/** |
| 2 | + * This program is free software: you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU General Public License as published by |
| 4 | + * the Free Software Foundation, either version 3 of the License, or |
| 5 | + * (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + **/ |
| 15 | + |
| 16 | +#include "httplib.h" |
| 17 | +#include <chrono> |
| 18 | +#include <ctime> |
| 19 | +#include <iostream> |
| 20 | +#include <libpq-fe.h> |
| 21 | + |
| 22 | +void tx(const httplib::Request &req, httplib::Response &res); |
| 23 | +void logger(const httplib::Request &req, const httplib::Response &res); |
| 24 | +std::string get_configuration(const std::string &env_name, |
| 25 | + const std::string &default_value = ""); |
| 26 | + |
| 27 | +/** |
| 28 | + * The database URL to be used as a libpq connection string |
| 29 | + */ |
| 30 | +const std::string database_url = get_configuration("DATABASE_URL"); |
| 31 | +const std::string sql_query = get_configuration("SQL_QUERY", "SELECT 1"); |
| 32 | + |
| 33 | +/** |
| 34 | + * Everything starts from here |
| 35 | + */ |
| 36 | +int main() { |
| 37 | + // Initial logging |
| 38 | + std::cout << "Database URL: " << std::quoted(database_url) << std::endl; |
| 39 | + std::cout << "SQL query: " << std::quoted(sql_query) << std::endl; |
| 40 | + std::cout.flush(); |
| 41 | + |
| 42 | + // Starting HTTP server |
| 43 | + httplib::Server svr; |
| 44 | + svr.set_logger(logger); |
| 45 | + svr.Get("/tx", tx); |
| 46 | + svr.listen("0.0.0.0", 8080); |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Given a request/response pair, put a small log message in stdout |
| 53 | + */ |
| 54 | +void logger(const httplib::Request &req, const httplib::Response &res) { |
| 55 | + using namespace std; |
| 56 | + |
| 57 | + const auto now = std::chrono::system_clock::now(); |
| 58 | + const auto nowAsTimeT = std::chrono::system_clock::to_time_t(now); |
| 59 | + const auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 60 | + now.time_since_epoch()) % |
| 61 | + 1000; |
| 62 | + |
| 63 | + cout << std::put_time(std::localtime(&nowAsTimeT), "%a %b %d %Y %T") << '.' |
| 64 | + << std::setfill('0') << std::setw(3) << nowMs.count(); |
| 65 | + cout << " " << req.remote_addr << " " << req.path << " - " << res.status |
| 66 | + << endl; |
| 67 | + cout.flush(); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * The test transaction on PostgreSQL |
| 72 | + */ |
| 73 | +void tx(const httplib::Request &req, httplib::Response &res) { |
| 74 | + PGconn *conn = PQconnectdb(database_url.c_str()); |
| 75 | + |
| 76 | + if (PQstatus(conn) != CONNECTION_OK) { |
| 77 | + res.set_content("Connection error", "text/plain"); |
| 78 | + res.status = 500; |
| 79 | + std::cout << PQerrorMessage(conn) << std::endl; |
| 80 | + std::cout.flush(); |
| 81 | + } else { |
| 82 | + PGresult *pgres = PQexec(conn, sql_query.c_str()); |
| 83 | + if (PQresultStatus(pgres) != PGRES_COMMAND_OK && |
| 84 | + PQresultStatus(pgres) != PGRES_TUPLES_OK) { |
| 85 | + res.set_content("Query error", "text/plain"); |
| 86 | + res.status = 500; |
| 87 | + std::cout << PQresultErrorMessage(pgres) << std::endl; |
| 88 | + std::cout.flush(); |
| 89 | + } else { |
| 90 | + res.set_content("Ok!", "text/plain"); |
| 91 | + } |
| 92 | + PQclear(pgres); |
| 93 | + } |
| 94 | + |
| 95 | + PQfinish(conn); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Get a configuration parameter or returns the default value |
| 100 | + */ |
| 101 | +std::string get_configuration(const std::string &env_name, |
| 102 | + const std::string &default_value) { |
| 103 | + auto value = std::getenv(env_name.c_str()); |
| 104 | + if (value == NULL) { |
| 105 | + return default_value; |
| 106 | + } else { |
| 107 | + return value; |
| 108 | + } |
| 109 | +} |
0 commit comments