diff --git a/src/http/CMakeLists.txt b/src/http/CMakeLists.txt index d379c1603..800a7be83 100644 --- a/src/http/CMakeLists.txt +++ b/src/http/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(http SHARED) target_sources(http PRIVATE AsyncReq.cc BinaryEncoding.cc + ChttpMetaModule.cc ChttpModule.cc Client.cc ClientConnPool.cc diff --git a/src/http/ChttpMetaModule.cc b/src/http/ChttpMetaModule.cc new file mode 100644 index 000000000..45aff9858 --- /dev/null +++ b/src/http/ChttpMetaModule.cc @@ -0,0 +1,71 @@ +/* + * LSST Data Management System + * + * This product includes software developed by the + * LSST Project (http://www.lsst.org/). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the LSST License Statement and + * the GNU General Public License along with this program. If not, + * see . + */ + +// Class header +#include "http/ChttpMetaModule.h" + +// System headers +#include + +using namespace std; +using json = nlohmann::json; + +namespace { +// Authorization context is not required by this module +lsst::qserv::http::AuthType const authType = lsst::qserv::http::AuthType::NONE; +string const authKey; +string const adminAuthKey; +} // namespace + +namespace lsst::qserv::http { + +unsigned int const ChttpMetaModule::version = 35; + +void ChttpMetaModule::process(string const& context, nlohmann::json const& info, httplib::Request const& req, + httplib::Response& resp, string const& subModuleName) { + ChttpMetaModule module(context, info, req, resp); + module.execute(subModuleName, ::authType); +} + +ChttpMetaModule::ChttpMetaModule(string const& context, nlohmann::json const& info, + httplib::Request const& req, httplib::Response& resp) + : http::ChttpModule(::authKey, ::adminAuthKey, req, resp), _context(context), _info(info) { + if (!_info.is_object()) { + throw invalid_argument("ChttpMetaModule::" + string(__func__) + " parameter info must be an object."); + } +} + +json ChttpMetaModule::executeImpl(string const& subModuleName) { + if (subModuleName == "VERSION") return _version(); + throw invalid_argument(context() + "::" + string(__func__) + " unsupported sub-module: '" + + subModuleName + "'"); +} + +string ChttpMetaModule::context() const { return _context; } + +json ChttpMetaModule::_version() { + debug(__func__); + json result = _info; + result["version"] = ChttpMetaModule::version; + return result; +} + +} // namespace lsst::qserv::http diff --git a/src/http/ChttpMetaModule.h b/src/http/ChttpMetaModule.h new file mode 100644 index 000000000..1e500b8d5 --- /dev/null +++ b/src/http/ChttpMetaModule.h @@ -0,0 +1,80 @@ +/* + * LSST Data Management System + * + * This product includes software developed by the + * LSST Project (http://www.lsst.org/). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the LSST License Statement and + * the GNU General Public License along with this program. If not, + * see . + */ +#ifndef LSST_QSERV_HTTP_CHTTPMETAMODULE_H +#define LSST_QSERV_HTTP_CHTTPMETAMODULE_H + +// System headers +#include +#include + +// Third party headers +#include "nlohmann/json.hpp" + +// Qserv headers +#include "http/ChttpModule.h" + +// This header declarations +namespace lsst::qserv::http { + +/** + * Class ChttpMetaModule implements a handler for the metadata queries on the REST API itself. + * The service responds with an information object provided at the creation time of the module. + */ +class ChttpMetaModule : public http::ChttpModule { +public: + typedef std::shared_ptr Ptr; + + /// The current version of the REST API + static unsigned int const version; + + /** + * @note supported values for parameter 'subModuleName' are: + * 'VERSION' - return a version of the REST API + * + * @param info The information object to be returned to clients of the service. + * @throws std::invalid_argument for unknown values of parameter 'subModuleName' + */ + static void process(std::string const& context, nlohmann::json const& info, httplib::Request const& req, + httplib::Response& resp, std::string const& subModuleName); + + ChttpMetaModule() = delete; + ChttpMetaModule(ChttpMetaModule const&) = delete; + ChttpMetaModule& operator=(ChttpMetaModule const&) = delete; + + ~ChttpMetaModule() final = default; + +protected: + virtual nlohmann::json executeImpl(std::string const& subModuleName) final; + virtual std::string context() const final; + +private: + ChttpMetaModule(std::string const& context, nlohmann::json const& info, httplib::Request const& req, + httplib::Response& resp); + + nlohmann::json _version(); + + std::string const _context; + nlohmann::json const _info; +}; + +} // namespace lsst::qserv::http + +#endif // LSST_QSERV_HTTP_CHTTPMETAMODULE_H