1 /* 2 Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2.0, 6 as published by the Free Software Foundation. 7 8 This program is also distributed with certain software (including 9 but not limited to OpenSSL) that is licensed under separate terms, 10 as designated in a particular file or component or in included license 11 documentation. The authors of MySQL hereby grant you an additional 12 permission to link the program and your derivative works with the 13 separately licensed software that they have included with MySQL. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software 22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 */ 24 25 #ifndef MYSQLROUTER_REST_API_PLUGIN_INCLUDED 26 #define MYSQLROUTER_REST_API_PLUGIN_INCLUDED 27 28 #include "mysqlrouter/rest_api_component.h" 29 30 #include <list> 31 #include <regex> 32 #include <shared_mutex> 33 34 class RestApi { 35 public: 36 RestApi(const std::string &uri_prefix, const std::string &uri_prefix_regex); 37 38 RestApi(const RestApi &) = delete; 39 RestApi &operator=(const RestApi &) = delete; 40 41 RestApi(RestApi &&) = delete; 42 RestApi &operator=(RestApi &&) = delete; 43 44 /** 45 * process the spec's Json JsonDocument. 46 */ 47 void process_spec(RestApiComponent::SpecProcessor spec_processor); 48 49 using PathList = 50 std::list<std::tuple<std::regex, std::unique_ptr<BaseRestApiHandler>>>; 51 52 /** 53 * add handler for URI path. 54 */ 55 void add_path(const std::string &path, 56 std::unique_ptr<BaseRestApiHandler> handler); 57 58 /** 59 * remove handle for URI path. 60 */ 61 void remove_path(const std::string &path); 62 63 /** 64 * handle request for all register URI paths. 65 * 66 * if no handler accepts the request, a HTTP response with status 404 will be 67 * sent. 68 */ 69 void handle_paths(HttpRequest &req); 70 71 /** 72 * get the uri path prefix. 73 */ uri_prefix()74 std::string uri_prefix() const { return uri_prefix_; } 75 76 /** 77 * get the regex for the URI path prefix. 78 */ uri_prefix_regex()79 std::string uri_prefix_regex() const { return uri_prefix_regex_; } 80 81 /** 82 * get the spec as JSON. 83 */ 84 std::string spec(); 85 86 protected: 87 std::string uri_prefix_; 88 std::string uri_prefix_regex_; 89 90 std::shared_timed_mutex rest_api_handler_mutex_; 91 std::list< 92 std::tuple<std::string, std::regex, std::unique_ptr<BaseRestApiHandler>>> 93 rest_api_handlers_; 94 95 std::mutex spec_doc_mutex_; 96 RestApiComponent::JsonDocument spec_doc_; 97 }; 98 99 #endif 100