108 lines
2.5 KiB
C++
108 lines
2.5 KiB
C++
/* 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;
|
|
|
|
cout << "Content-Type: application/json\r\n\r\n";
|
|
|
|
char *env{getenv("REQUEST_METHOD")};
|
|
if (env != nullptr && string(env) != "GET")
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
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;
|
|
}
|