#include #include #include #include #include #include #include #include #include #include namespace Echoion::Plugins::ShortUrl { namespace { constexpr std::string_view kPluginId{"url-shortener-external"}; } class ExternalShortenerPlugin final : public Api::IExtensionPlugin, public Void::ShortUrl::IShortener { public: [[nodiscard]] std::string_view name() const noexcept override { return kPluginId; } [[nodiscard]] std::string_view provider_id() const noexcept override { return kPluginId; } void applyConfig(const char* config) { configPayload_ = config != nullptr ? config : ""; } void on_load() override { shortener_.reset(); activationError_.clear(); dormant_ = !hasExternalConfiguration(configPayload_); if (dormant_) { activationError_ = "plugin is not configured"; return; } auto config = parseExternalConfig(configPayload_); if (!config) { activationError_ = std::move(config).error(); return; } auto shortener = ExternalShortener::create(std::move(*config)); if (!shortener) { activationError_ = std::move(shortener).error(); return; } shortener_ = std::move(*shortener); } void on_unload() override { shortener_.reset(); } [[nodiscard]] bool ready() const noexcept { return dormant_ || shortener_ != nullptr; } [[nodiscard]] Void::ShortUrl::Health health() const override { if (shortener_ != nullptr) { return shortener_->health(); } return { .status = Void::ShortUrl::HealthStatus::unhealthy, .message = activationError_.empty() ? "plugin is not configured" : activationError_, }; } [[nodiscard]] Void::ShortUrl::ShortenResult shorten(const Void::ShortUrl::ShortenRequest& request) override { if (shortener_ == nullptr) { return { .short_url = {}, .key = {}, .expires_at_unix_seconds = 0, .error = { .code = Void::ShortUrl::ErrorCode::unavailable, .message = "external shortener is unavailable", }, }; } return shortener_->shorten(request); } private: std::string configPayload_; std::string activationError_; std::unique_ptr shortener_; bool dormant_ = false; }; std::unique_ptr createModule() { return std::make_unique(); } const Void::Plugin::ModuleDescriptor& moduleDescriptor() { static const Void::Plugin::ModuleDescriptor descriptor{ .module_id = "url-shortener-external", .component_version = {.major = 2, .minor = 0, .patch = 0}, .abi_hash = "sha256:251e3fa0ee8228cb2eb79b399deee58524b64fbb4764c8a2644357b3502354fe", .abi_contract = Void::Json::object({ {"schemaVersion", 1}, {"contractId", "echoion.plugin.url-shortener-external"}, {"descriptor", "Void::Plugin::ModuleDescriptor"}, }), .dependencies = { { .module_id = "EchoionApi", .version_range = "^2.0.0", .abi_hash = "sha256:336492a52097dd12f222bb6d9acab19e8eb6a502eab45bb78127bce6d733fbd3", }, }, .activation_policy = Void::Plugin::ActivationPolicy::Reloadable, .create = createModule, }; return descriptor; } } // namespace Echoion::Plugins::ShortUrl extern "C" { const Void::Plugin::ModuleDescriptor* echoion_module_query() { return &Echoion::Plugins::ShortUrl::moduleDescriptor(); } void* void_plugin_create() { return std::make_unique< Echoion::Plugins::ShortUrl::ExternalShortenerPlugin>() .release(); } void void_plugin_destroy(void* instance) { std::unique_ptr{ static_cast( instance)}; } Void::Plugin::PluginInfo void_plugin_info() { return { .abi_version = Void::Plugin::ABI_VERSION, .name = "url-shortener-external", .plugin_version = ECHOION_COMPONENT_VERSION, .description = "Configurable external URL-shortening adapter", }; } void void_plugin_configure(void* instance, const char* config) { static_cast(instance) ->applyConfig(config); } int void_plugin_ready(void* instance) { return static_cast( instance) ->ready() ? 0 : 1; } Void::Service::IService* void_plugin_service(void* instance) { auto* plugin = static_cast( instance); return static_cast( static_cast(plugin)); } } // extern "C"