|
| 1 | +#include "frontend.h" |
| 2 | + |
| 3 | +#include <stdexcept> |
| 4 | +#include <string> |
| 5 | +#include <utility> |
| 6 | + |
| 7 | +namespace minitts::server { |
| 8 | + |
| 9 | +namespace { |
| 10 | + |
| 11 | +bool contract_value_matches(std::string_view lhs, std::string_view rhs) { |
| 12 | + return lhs == frontend_contracts::any || rhs == frontend_contracts::any || lhs == rhs; |
| 13 | +} |
| 14 | + |
| 15 | +bool contract_route_overlaps(std::string_view lhs, std::string_view rhs) { |
| 16 | + return contract_value_matches(lhs, rhs); |
| 17 | +} |
| 18 | + |
| 19 | +bool validate_pre_contracts( |
| 20 | + std::string_view previous_module, |
| 21 | + const FrontendPreContract & previous, |
| 22 | + std::string_view next_module, |
| 23 | + const FrontendPreContract & next) { |
| 24 | + if (!contract_route_overlaps(previous.method, next.method) || |
| 25 | + !contract_route_overlaps(previous.path, next.path)) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + if (!contract_value_matches(previous.request_out, next.request_in)) { |
| 29 | + throw std::runtime_error( |
| 30 | + "incompatible frontend pre-processing order: " + std::string(previous_module) + |
| 31 | + " outputs request state '" + std::string(previous.request_out) + |
| 32 | + "', but " + std::string(next_module) + |
| 33 | + " expects '" + std::string(next.request_in) + "'"); |
| 34 | + } |
| 35 | + return true; |
| 36 | +} |
| 37 | + |
| 38 | +bool validate_post_contracts( |
| 39 | + std::string_view previous_module, |
| 40 | + const FrontendPostContract & previous, |
| 41 | + std::string_view next_module, |
| 42 | + const FrontendPostContract & next) { |
| 43 | + if (!contract_route_overlaps(previous.method, next.method) || |
| 44 | + !contract_route_overlaps(previous.path, next.path)) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + if (!contract_value_matches(previous.response_out, next.response_in)) { |
| 48 | + throw std::runtime_error( |
| 49 | + "incompatible frontend post-processing order: " + std::string(previous_module) + |
| 50 | + " outputs response state '" + std::string(previous.response_out) + |
| 51 | + "', but " + std::string(next_module) + |
| 52 | + " expects '" + std::string(next.response_in) + "'"); |
| 53 | + } |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +} // namespace |
| 58 | + |
| 59 | +#include "server_frontend_module_declarations.inc" |
| 60 | + |
| 61 | +std::optional<FrontendPreContract> ServerFrontendModule::pre_contract() const { |
| 62 | + return std::nullopt; |
| 63 | +} |
| 64 | + |
| 65 | +std::optional<FrontendPostContract> ServerFrontendModule::post_contract() const { |
| 66 | + return std::nullopt; |
| 67 | +} |
| 68 | + |
| 69 | +void ServerFrontendModule::pre_process(ServerFrontendContext & context, ServerFrontendRequest & request) { |
| 70 | + (void) context; |
| 71 | + (void) request; |
| 72 | +} |
| 73 | + |
| 74 | +void ServerFrontendModule::post_process(ServerFrontendContext & context, ServerFrontendResponse & response) { |
| 75 | + (void) context; |
| 76 | + (void) response; |
| 77 | +} |
| 78 | + |
| 79 | +void ServerFrontendRegistry::add(ServerFrontendModuleFactory factory) { |
| 80 | + if (factory == nullptr) { |
| 81 | + throw std::runtime_error("server frontend registration requires a module factory"); |
| 82 | + } |
| 83 | + auto module = factory(); |
| 84 | + if (!module) { |
| 85 | + throw std::runtime_error("server frontend module factory returned null"); |
| 86 | + } |
| 87 | + |
| 88 | + const auto pre = module->pre_contract(); |
| 89 | + const auto post = module->post_contract(); |
| 90 | + for (auto it = modules_.rbegin(); it != modules_.rend(); ++it) { |
| 91 | + if (pre.has_value()) { |
| 92 | + if (const auto previous = (*it)->pre_contract()) { |
| 93 | + if (validate_pre_contracts((*it)->name(), *previous, module->name(), *pre)) { |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + for (auto it = modules_.rbegin(); it != modules_.rend(); ++it) { |
| 100 | + if (post.has_value()) { |
| 101 | + if (const auto previous = (*it)->post_contract()) { |
| 102 | + if (validate_post_contracts((*it)->name(), *previous, module->name(), *post)) { |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + modules_.push_back(std::move(module)); |
| 110 | +} |
| 111 | + |
| 112 | +bool ServerFrontendRegistry::empty() const { return modules_.empty(); } |
| 113 | + |
| 114 | +HttpResponse ServerFrontendRegistry::handle(ServerFrontendContext & context, const HttpRequest & request) const { |
| 115 | + ServerFrontendRequest frontend_request{request, std::nullopt}; |
| 116 | + for (const auto & module : modules_) { |
| 117 | + module->pre_process(context, frontend_request); |
| 118 | + if (frontend_request.response.has_value()) { |
| 119 | + return std::move(*frontend_request.response); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + auto core_response = context.forward_to_core(frontend_request.request); |
| 124 | + ServerFrontendResponse frontend_response{request, frontend_request.request, std::move(core_response)}; |
| 125 | + for (const auto & module : modules_) { |
| 126 | + module->post_process(context, frontend_response); |
| 127 | + } |
| 128 | + return std::move(frontend_response.response); |
| 129 | +} |
| 130 | + |
| 131 | +void register_static_server_frontends(ServerFrontendRegistry & registry) { |
| 132 | +#include "server_frontend_module_registrations.inc" |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace minitts::server |
0 commit comments