Skip to content

Commit

Permalink
Added CPP-HTTPLIB-based version of the meta module
Browse files Browse the repository at this point in the history
  • Loading branch information
iagaponenko committed Jul 5, 2024
1 parent d5d6f66 commit 5c13233
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/http/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(http SHARED)
target_sources(http PRIVATE
AsyncReq.cc
BinaryEncoding.cc
ChttpMetaModule.cc
ChttpModule.cc
Client.cc
ClientConnPool.cc
Expand Down
71 changes: 71 additions & 0 deletions src/http/ChttpMetaModule.cc
Original file line number Diff line number Diff line change
@@ -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 <http://www.lsstcorp.org/LegalNotices/>.
*/

// Class header
#include "http/ChttpMetaModule.h"

// System headers
#include <stdexcept>

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
80 changes: 80 additions & 0 deletions src/http/ChttpMetaModule.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.lsstcorp.org/LegalNotices/>.
*/
#ifndef LSST_QSERV_HTTP_CHTTPMETAMODULE_H
#define LSST_QSERV_HTTP_CHTTPMETAMODULE_H

// System headers
#include <memory>
#include <string>

// 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<ChttpMetaModule> 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

0 comments on commit 5c13233

Please sign in to comment.