/* This file is part of FediBlock-backend. * Copyright © 2021 tastytea * * 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 . */ #include "json_api.hpp" #include "files.hpp" #include "git.hpp" #include "types.hpp" #include #include #include #include #include #include #include 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 &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; cout << "Content-Type: application/json\r\n\r\n"; 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; }