383 lines
18 KiB
C++
383 lines
18 KiB
C++
#pragma once
|
|
#include "adminive/http.hpp"
|
|
#include "httplib.h"
|
|
#include <algorithm>
|
|
#include <charconv>
|
|
#include <concepts>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
#include <vector>
|
|
namespace adminive {
|
|
template <Json_Type Json>
|
|
void write_http_json(httplib::Response& response, const Json& value) {
|
|
response.set_content(dump_json(value, 2), "application/json; charset=utf-8");
|
|
}
|
|
template <Json_Type Json>
|
|
void write_http_response(httplib::Response& response, Http_Response<Json> value) {
|
|
response.status = value.status;
|
|
write_http_json(response, value.body);
|
|
}
|
|
template <Json_Type Json>
|
|
void write_http_error(httplib::Response& response, int status, const Update_Result& update) {
|
|
write_http_response(response, make_http_error<Json>(status, update));
|
|
}
|
|
template <class T, Json_Type Json>
|
|
requires Writable_Adapted_Object<T> && std::copy_constructible<Object_Model_Type<T>>
|
|
class Http_Resource {
|
|
public:
|
|
using Service = Resource_Service<T, Json>;
|
|
using Transaction = typename Service::Transaction;
|
|
Http_Resource(T& value, std::string path, Transaction transaction = {}, std::mutex* shared_mutex = nullptr) : service_(std::make_shared<Service>(value, std::move(path), std::move(transaction), shared_mutex)) {}
|
|
Json amis_schema() const {
|
|
return service_->amis_schema();
|
|
}
|
|
void bind(httplib::Server& server) const {
|
|
const auto service = service_;
|
|
server.Get(service->path() + "/descriptor", [service](const httplib::Request&, httplib::Response& response) {
|
|
write_http_response(response, service->descriptor_response());
|
|
});
|
|
server.Get(service->path() + "/data", [service](const httplib::Request&, httplib::Response& response) {
|
|
write_http_response(response, service->data_response());
|
|
});
|
|
server.Get(service->path() + "/amis", [service](const httplib::Request&, httplib::Response& response) {
|
|
write_http_response(response, service->amis_response());
|
|
});
|
|
server.Post(service->path() + "/data", [service](const httplib::Request& request, httplib::Response& response) {
|
|
write_http_response(response, service->update_response(request.body));
|
|
});
|
|
}
|
|
private:
|
|
std::shared_ptr<Service> service_;
|
|
};
|
|
template <Described_Type T, Json_Type Json>
|
|
requires std::default_initializable<T> && std::copy_constructible<T> && std::assignable_from<T&, T>
|
|
class Http_Collection_Resource {
|
|
public:
|
|
explicit Http_Collection_Resource(std::string path, std::vector<T> initial = {}) : path_(std::move(path)) {
|
|
for(auto& value : initial) {
|
|
entries_.push_back(Entry{next_id_++, std::move(value)});
|
|
}
|
|
}
|
|
template <Described_Type Status>
|
|
void register_overview_status(std::string api, std::uint64_t interval = 2000) {
|
|
overview_status_api_ = std::move(api);
|
|
overview_status_descriptor_ = to_status_descriptor_json<Json, Status>();
|
|
overview_status_interval_ = interval;
|
|
}
|
|
template <auto Function>
|
|
void register_status(std::uint64_t interval = 2000) {
|
|
using Function_Type = decltype(Function);
|
|
static_assert(std::is_member_function_pointer_v<Function_Type>);
|
|
static_assert(std::is_invocable_v<Function_Type, T&>);
|
|
using Status = std::remove_cvref_t<std::invoke_result_t<Function_Type, T&>>;
|
|
static_assert(Described_Type<Status>);
|
|
status_descriptor_ = to_status_descriptor_json<Json, Status>();
|
|
status_interval_ = interval;
|
|
status_reader_ = [](T& value) {
|
|
return to_status_json<Json>(std::invoke(Function, value));
|
|
};
|
|
}
|
|
std::size_t size() const {
|
|
std::scoped_lock lock(mutex_);
|
|
return entries_.size();
|
|
}
|
|
Json items_json() const {
|
|
std::scoped_lock lock(mutex_);
|
|
Json items = json_array<Json>();
|
|
for(const auto& entry : entries_) {
|
|
json_append(items, encode_entry(entry));
|
|
}
|
|
return items;
|
|
}
|
|
Json amis_schema() const {
|
|
const Json* descriptor = status_reader_ ? &status_descriptor_ : nullptr;
|
|
if(overview_status_api_.empty()) {
|
|
return to_amis_crud_schema<Json, T>(path_, descriptor, status_interval_);
|
|
}
|
|
return to_amis_crud_status_schema<Json, T>(path_, overview_status_api_, overview_status_descriptor_, overview_status_interval_, descriptor, status_interval_);
|
|
}
|
|
void bind(httplib::Server& server) {
|
|
server.Get(path_ + "/descriptor", [](const httplib::Request&, httplib::Response& response) {
|
|
write_http_json(response, make_http_result<Json>(0, "", to_descriptor_json<Json, T>()));
|
|
});
|
|
server.Get(path_ + "/amis", [this](const httplib::Request&, httplib::Response& response) {
|
|
write_http_json(response, make_http_result<Json>(0, "", amis_schema()));
|
|
});
|
|
if(status_reader_) {
|
|
server.Get(path_ + "/status/descriptor", [this](const httplib::Request&, httplib::Response& response) {
|
|
write_http_json(response, make_http_result<Json>(0, "", status_descriptor_));
|
|
});
|
|
server.Get(status_pattern(), [this](const httplib::Request& request, httplib::Response& response) {
|
|
const auto id = read_route_id(request);
|
|
std::scoped_lock lock(mutex_);
|
|
const auto iterator = find_entry(id);
|
|
if(iterator == entries_.end()) {
|
|
write_not_found(response);
|
|
return;
|
|
}
|
|
write_http_json(response, make_http_result<Json>(0, "", status_reader_(iterator->value)));
|
|
});
|
|
}
|
|
server.Get(path_, [this](const httplib::Request& request, httplib::Response& response) {
|
|
const std::size_t page = read_size_parameter(request, "page", 1);
|
|
const std::size_t per_page = read_size_parameter(request, "perPage", 20);
|
|
std::scoped_lock lock(mutex_);
|
|
std::vector<const Entry*> ordered_entries;
|
|
ordered_entries.reserve(entries_.size());
|
|
for(const auto& entry : entries_) {
|
|
ordered_entries.push_back(&entry);
|
|
}
|
|
apply_request_sort(request, ordered_entries);
|
|
const std::size_t offset = std::min((page - 1) * per_page, ordered_entries.size());
|
|
const std::size_t end = std::min(offset + per_page, ordered_entries.size());
|
|
Json items = json_array<Json>();
|
|
for(std::size_t index = offset; index < end; ++index) {
|
|
json_append(items, encode_entry(*ordered_entries[index]));
|
|
}
|
|
Json data = json_object<Json>();
|
|
json_set(data, "items", std::move(items));
|
|
json_set(data, "total", ordered_entries.size());
|
|
write_http_json(response, make_http_result<Json>(0, "", std::move(data)));
|
|
});
|
|
server.Get(item_pattern(), [this](const httplib::Request& request, httplib::Response& response) {
|
|
const auto id = read_route_id(request);
|
|
std::scoped_lock lock(mutex_);
|
|
const auto iterator = find_entry(id);
|
|
if(iterator == entries_.end()) {
|
|
write_not_found(response);
|
|
return;
|
|
}
|
|
write_http_json(response, make_http_result<Json>(0, "", encode_entry(*iterator)));
|
|
});
|
|
server.Post(path_, [this](const httplib::Request& request, httplib::Response& response) {
|
|
try {
|
|
Json input = parse_json<Json>(request.body);
|
|
Json_Adapter<Json>::erase(input, "id");
|
|
T value{};
|
|
const auto result = apply_frontend_create<Json>(value, input);
|
|
if(!result.success) {
|
|
write_http_error<Json>(response, 422, result);
|
|
return;
|
|
}
|
|
std::scoped_lock lock(mutex_);
|
|
entries_.push_back(Entry{next_id_++, std::move(value)});
|
|
write_http_json(response, make_http_result<Json>(0, "created", encode_entry(entries_.back())));
|
|
} catch(const std::exception& error) {
|
|
response.status = 400;
|
|
write_http_json(response, make_http_result<Json>(400, error.what(), json_object<Json>()));
|
|
}
|
|
});
|
|
if(describe<T>().list_options().user_reorderable_) {
|
|
server.Post(path_ + "/order", [this](const httplib::Request& request, httplib::Response& response) {
|
|
try {
|
|
const Json input = parse_json<Json>(request.body);
|
|
const auto ids = read_order_ids(Json_Adapter<Json>::at(input, "ids"));
|
|
std::scoped_lock lock(mutex_);
|
|
reorder(ids);
|
|
write_http_json(response, make_http_result<Json>(0, "reordered", json_object<Json>()));
|
|
} catch(const std::exception& error) {
|
|
response.status = 400;
|
|
write_http_json(response, make_http_result<Json>(400, error.what(), json_object<Json>()));
|
|
}
|
|
});
|
|
}
|
|
server.Put(item_pattern(), [this](const httplib::Request& request, httplib::Response& response) {
|
|
update(request, response);
|
|
});
|
|
server.Patch(item_pattern(), [this](const httplib::Request& request, httplib::Response& response) {
|
|
update(request, response);
|
|
});
|
|
server.Delete(item_pattern(), [this](const httplib::Request& request, httplib::Response& response) {
|
|
const auto id = read_route_id(request);
|
|
std::scoped_lock lock(mutex_);
|
|
const auto iterator = find_entry(id);
|
|
if(iterator == entries_.end()) {
|
|
write_not_found(response);
|
|
return;
|
|
}
|
|
entries_.erase(iterator);
|
|
write_http_json(response, make_http_result<Json>(0, "deleted", json_object<Json>()));
|
|
});
|
|
}
|
|
private:
|
|
struct Entry {
|
|
std::uint64_t id{};
|
|
T value;
|
|
};
|
|
using Iterator = typename std::vector<Entry>::iterator;
|
|
static std::size_t read_size_parameter(const httplib::Request& request, const char* name, std::size_t fallback) {
|
|
if(!request.has_param(name)) {
|
|
return fallback;
|
|
}
|
|
const auto text = request.get_param_value(name);
|
|
std::size_t value{};
|
|
const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), value);
|
|
if(error != std::errc{} || end != text.data() + text.size() || value == 0) {
|
|
return fallback;
|
|
}
|
|
return value;
|
|
}
|
|
static int compare_json_values(const Json& left, const Json& right) {
|
|
if(Json_Adapter<Json>::is_number(left) && Json_Adapter<Json>::is_number(right)) {
|
|
const long double left_value = Json_Adapter<Json>::number(left);
|
|
const long double right_value = Json_Adapter<Json>::number(right);
|
|
return left_value < right_value ? -1 : left_value > right_value ? 1 : 0;
|
|
}
|
|
if(Json_Adapter<Json>::is_string(left) && Json_Adapter<Json>::is_string(right)) {
|
|
const auto left_value = json_get<Json, std::string>(left);
|
|
const auto right_value = json_get<Json, std::string>(right);
|
|
return left_value < right_value ? -1 : left_value > right_value ? 1 : 0;
|
|
}
|
|
if(Json_Adapter<Json>::is_boolean(left) && Json_Adapter<Json>::is_boolean(right)) {
|
|
const bool left_value = json_get<Json, bool>(left);
|
|
const bool right_value = json_get<Json, bool>(right);
|
|
return left_value == right_value ? 0 : left_value ? 1 : -1;
|
|
}
|
|
const std::string left_value = dump_json(left);
|
|
const std::string right_value = dump_json(right);
|
|
return left_value < right_value ? -1 : left_value > right_value ? 1 : 0;
|
|
}
|
|
static bool is_sortable_field(const std::string& name) {
|
|
if(name == "id") {
|
|
return true;
|
|
}
|
|
bool sortable{};
|
|
std::apply([&](const auto&... field) {
|
|
((field.name() == name ? sortable = field.is_sortable() : false), ...);
|
|
}, describe<T>().fields());
|
|
return sortable;
|
|
}
|
|
static std::uint64_t read_route_id(const httplib::Request& request) {
|
|
const std::string text = request.matches[1].str();
|
|
std::uint64_t id{};
|
|
const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), id);
|
|
if(error != std::errc{} || end != text.data() + text.size()) {
|
|
return 0;
|
|
}
|
|
return id;
|
|
}
|
|
static std::vector<std::uint64_t> read_order_ids(const Json& value) {
|
|
std::vector<std::uint64_t> result;
|
|
if(Json_Adapter<Json>::is_array(value)) {
|
|
for(std::size_t index = 0; index < Json_Adapter<Json>::size(value); ++index) {
|
|
const auto& item = Json_Adapter<Json>::at(value, index);
|
|
result.push_back(Json_Adapter<Json>::is_string(item) ? std::stoull(json_get<Json, std::string>(item)) : json_get<Json, std::uint64_t>(item));
|
|
}
|
|
return result;
|
|
}
|
|
const std::string text = json_get<Json, std::string>(value);
|
|
std::size_t begin{};
|
|
while(begin < text.size()) {
|
|
const auto end = text.find(',', begin);
|
|
result.push_back(std::stoull(text.substr(begin, end == std::string::npos ? text.size() - begin : end - begin)));
|
|
if(end == std::string::npos) {
|
|
break;
|
|
}
|
|
begin = end + 1;
|
|
}
|
|
return result;
|
|
}
|
|
void apply_request_sort(const httplib::Request& request, std::vector<const Entry*>& entries) const {
|
|
const auto descriptor = describe<T>();
|
|
const auto& options = descriptor.list_options();
|
|
std::string order_by = options.default_order_by;
|
|
std::string order_dir = options.default_order_dir;
|
|
if(request.has_param("orderBy")) {
|
|
order_by = request.get_param_value("orderBy");
|
|
order_dir = request.has_param("orderDir") ? request.get_param_value("orderDir") : "asc";
|
|
}
|
|
if(order_by.empty() || !is_sortable_field(order_by)) {
|
|
return;
|
|
}
|
|
const bool descending = order_dir == "desc";
|
|
std::stable_sort(entries.begin(), entries.end(), [&](const Entry* left, const Entry* right) {
|
|
int comparison{};
|
|
if(order_by == "id") {
|
|
comparison = left->id < right->id ? -1 : left->id > right->id ? 1 : 0;
|
|
} else {
|
|
const auto left_json = to_frontend_json<Json>(left->value);
|
|
const auto right_json = to_frontend_json<Json>(right->value);
|
|
comparison = compare_json_values(Json_Adapter<Json>::at(left_json, order_by), Json_Adapter<Json>::at(right_json, order_by));
|
|
}
|
|
return descending ? comparison > 0 : comparison < 0;
|
|
});
|
|
}
|
|
std::string item_pattern() const {
|
|
return path_ + R"(/(\d+))";
|
|
}
|
|
std::string status_pattern() const {
|
|
return path_ + R"(/(\d+)/status)";
|
|
}
|
|
Iterator find_entry(std::uint64_t id) {
|
|
return std::find_if(entries_.begin(), entries_.end(), [id](const Entry& entry) {
|
|
return entry.id == id;
|
|
});
|
|
}
|
|
static Json encode_entry(const Entry& entry) {
|
|
Json result = to_frontend_json<Json>(entry.value);
|
|
json_set(result, "id", entry.id);
|
|
return result;
|
|
}
|
|
static void write_not_found(httplib::Response& response) {
|
|
response.status = 404;
|
|
write_http_json(response, make_http_result<Json>(404, "record not found", json_object<Json>()));
|
|
}
|
|
void reorder(const std::vector<std::uint64_t>& ids) {
|
|
std::vector<Entry> remaining = std::move(entries_);
|
|
std::vector<Entry> reordered;
|
|
reordered.reserve(remaining.size());
|
|
for(const auto id : ids) {
|
|
const auto iterator = std::find_if(remaining.begin(), remaining.end(), [id](const Entry& entry) {
|
|
return entry.id == id;
|
|
});
|
|
if(iterator != remaining.end()) {
|
|
reordered.push_back(std::move(*iterator));
|
|
remaining.erase(iterator);
|
|
}
|
|
}
|
|
for(auto& entry : remaining) {
|
|
reordered.push_back(std::move(entry));
|
|
}
|
|
entries_ = std::move(reordered);
|
|
}
|
|
void update(const httplib::Request& request, httplib::Response& response) {
|
|
try {
|
|
Json patch = parse_json<Json>(request.body);
|
|
Json_Adapter<Json>::erase(patch, "id");
|
|
const auto id = read_route_id(request);
|
|
std::scoped_lock lock(mutex_);
|
|
const auto iterator = find_entry(id);
|
|
if(iterator == entries_.end()) {
|
|
write_not_found(response);
|
|
return;
|
|
}
|
|
const auto result = apply_frontend_patch<Json>(iterator->value, patch);
|
|
if(!result.success) {
|
|
write_http_error<Json>(response, 422, result);
|
|
return;
|
|
}
|
|
write_http_json(response, make_http_result<Json>(0, result.message, encode_entry(*iterator)));
|
|
} catch(const std::exception& error) {
|
|
response.status = 400;
|
|
write_http_json(response, make_http_result<Json>(400, error.what(), json_object<Json>()));
|
|
}
|
|
}
|
|
std::string path_;
|
|
std::string overview_status_api_;
|
|
Json overview_status_descriptor_{};
|
|
std::uint64_t overview_status_interval_{2000};
|
|
Json status_descriptor_{};
|
|
std::function<Json(T&)> status_reader_;
|
|
std::uint64_t status_interval_{2000};
|
|
std::vector<Entry> entries_;
|
|
std::uint64_t next_id_{1};
|
|
mutable std::mutex mutex_;
|
|
};
|
|
}
|