Add JSON API.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2021-01-09 14:36:36 +01:00
parent d8212c261f
commit c9002f3640
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 141 additions and 0 deletions

View File

@ -11,3 +11,9 @@ target_link_libraries(${PROJECT_NAME}-gen_rss
PRIVATE fediblock pugixml)
install(TARGETS ${PROJECT_NAME}-gen_rss
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
add_executable(${PROJECT_NAME}-json_api "json_api.cpp")
target_link_libraries(${PROJECT_NAME}-json_api
PRIVATE fediblock)
install(TARGETS ${PROJECT_NAME}-json_api
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")

View File

@ -0,0 +1,99 @@
/* This file is part of FediBlock-backend.
* Copyright © 2021 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "json_api.hpp"
#include "files.hpp"
#include "git.hpp"
#include "types.hpp"
#include <nlohmann/json.hpp>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <ostream>
#include <stdexcept>
#include <vector>
namespace FediBlock::json_api
{
using std::cerr;
using std::cout;
using std::exception;
using std::ifstream;
using std::ostream;
using std::runtime_error;
using std::stringstream;
using std::vector;
void write_json(ostream &out, const vector<entry_type> &entries)
{
nlohmann::json json;
for (const auto &entry : entries)
{
size_t startpos{0};
size_t endpos{0};
do
{
endpos = entry.instance.find(',', startpos);
string instance{entry.instance.substr(startpos, endpos - startpos)};
json.push_back({{"instance", instance}, {"url", entry.json_url}});
startpos = endpos + 1;
} while (endpos != string::npos);
}
out << json.dump() << '\n';
}
} // namespace FediBlock::json_api
int main(int /*argc*/, char * /*argv*/[])
{
using namespace FediBlock;
using namespace FediBlock::json_api;
git::init(true);
bool remove_lockfile{false};
try
{
try
{
git::update_cached_repo();
remove_lockfile = true;
}
catch (const runtime_error &e)
{
cerr << "Warning: " << e.what() << '\n';
// Ignore, use old version of repo.
}
const auto entries{files::read_json_files(true)};
write_json(cout, entries);
}
catch (const exception &e)
{
cerr << "Error: " << e.what() << '\n';
}
git::cleanup(remove_lockfile);
return 0;
}

View File

@ -0,0 +1,36 @@
/* This file is part of FediBlock-backend.
* Copyright © 2021 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FEDIBLOCK_BACKEND_JSON_API_HPP
#define FEDIBLOCK_BACKEND_JSON_API_HPP
#include "types.hpp"
#include <ostream>
#include <vector>
namespace FediBlock::json_api
{
using std::ostream;
using std::vector;
// Output JSON to an ostream.
void write_json(ostream &out, const vector<entry_type> &entries);
} // namespace FediBlock::json_api
#endif // FEDIBLOCK_BACKEND_JSON_API_HPP