| 1 | /************************************************************************************ |
| 2 | * |
| 3 | * D++, A Lightweight C++ library for Discord |
| 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * Copyright 2021 Craig Edwards and D++ contributors |
| 7 | * (https://github.com/brainboxdotcc/DPP/graphs/contributors) |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | ************************************************************************************/ |
| 22 | |
| 23 | #include <dpp/dpp.h> |
| 24 | #include <iostream> |
| 25 | |
| 26 | /** |
| 27 | * @brief Generate a simple html response |
| 28 | * @param request request to respond to |
| 29 | */ |
| 30 | void respond(dpp::http_server_request& request) { |
| 31 | request.set_status(200).set_response_header("Content-Type", "text/html").set_response_body("<h1>It lives!</h1>"); |
| 32 | } |
| 33 | |
| 34 | int main() { |
| 35 | using namespace std::chrono_literals; |
| 36 | char* t = getenv("DPP_UNIT_TEST_TOKEN"); |
| 37 | if (t) { |
| 38 | dpp::cluster bot(t, 0, dpp::NO_SHARDS, 1, 1, false, dpp::cache_policy::cpol_none); |
| 39 | dpp::http_server *server1{nullptr}, *server2{nullptr}; |
| 40 | |
| 41 | bot.on_log([&](const dpp::log_t& log) { |
| 42 | std::cout << "[" << dpp::utility::current_date_time() << "] " << dpp::utility::loglevel(log.severity) << ": " << log.message << std::endl; |
| 43 | }); |
| 44 | bot.on_ready([&](const dpp::ready_t& ready) { |
| 45 | |
| 46 | /* A plaintext HTTP server on port 3011 */ |
| 47 | server1 = new dpp::http_server(&bot, "0.0.0.0", 3011, [&bot](dpp::http_server_request* request) { |
| 48 | respond(*request); |
| 49 | }); |
| 50 | |
| 51 | /* An SSL enabled HTTPS server on port 3042 */ |
| 52 | server2 = new dpp::http_server(&bot, "0.0.0.0", 3042, [&bot](dpp::http_server_request* request) { |
| 53 | respond(*request); |
| 54 | }, "../../testdata/localhost.key", "../../testdata/localhost.pem"); |
| 55 | |
| 56 | /* A discord interactions endpoint on port 3010 */ |
| 57 | bot.enable_webhook_server(getenv("DPP_PUBLIC_KEY"), "0.0.0.0", 3010); |
| 58 | }); |
| 59 | bot.on_slashcommand([&](const dpp::slashcommand_t& event) { |
| 60 | event.reply("hello"); |
| 61 | }); |
| 62 | |
| 63 | bot.start(dpp::st_wait); |
| 64 | } |
| 65 | } |