NetworkedApplications: HTTP application layer with auth and admin views
This commit is contained in:
1 parent
0ef4e3b9ac
commit
11fea2566a
4 files changed
+707
-1
No files matched your search
@@ -0,0 +1,84 @@
|
||||
#pragma once
|
||||
|
||||
#include "networked_kv/domain.hpp"
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
struct HttpRequest {
|
||||
std::string method;
|
||||
std::string target;
|
||||
std::map<std::string, std::string, std::less<>> headers;
|
||||
std::string body;
|
||||
|
||||
[[nodiscard]] std::optional<std::string_view>
|
||||
header(std::string_view name) const;
|
||||
};
|
||||
|
||||
struct HttpResponse {
|
||||
int status{200};
|
||||
std::map<std::string, std::string, std::less<>> headers;
|
||||
std::string body;
|
||||
};
|
||||
|
||||
struct ApiConfig {
|
||||
std::string bearer_token;
|
||||
std::size_t max_body_bytes{1024ULL * 1024ULL};
|
||||
std::size_t max_scan_limit{1000};
|
||||
};
|
||||
|
||||
struct ApiMetrics {
|
||||
std::uint64_t requests{0};
|
||||
std::uint64_t unauthorized{0};
|
||||
std::uint64_t client_errors{0};
|
||||
std::uint64_t server_errors{0};
|
||||
std::uint64_t bytes_in{0};
|
||||
std::uint64_t bytes_out{0};
|
||||
};
|
||||
|
||||
/// Transport-agnostic HTTP application layer for the key-value service.
|
||||
///
|
||||
/// Net owns parsing and framing; this maps an already-parsed request to the
|
||||
/// KV domain, enforces bearer auth on the data plane, and exposes bounded
|
||||
/// admin views that never dump credentials or mutate data.
|
||||
class KvApi
|
||||
{
|
||||
public:
|
||||
|
||||
KvApi(KvStore& store, ApiConfig config);
|
||||
|
||||
[[nodiscard]] HttpResponse handle(const HttpRequest& request);
|
||||
|
||||
[[nodiscard]] HttpResponse admin(const HttpRequest& request) const;
|
||||
|
||||
void set_ready(bool ready) noexcept { m_ready = ready; }
|
||||
|
||||
[[nodiscard]] ApiMetrics metrics() const noexcept { return m_metrics; }
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] bool authorized(const HttpRequest& request) const;
|
||||
[[nodiscard]] HttpResponse
|
||||
kv_get(std::string_view key, const HttpRequest& request);
|
||||
[[nodiscard]] HttpResponse
|
||||
kv_put(std::string_view key, const HttpRequest& request);
|
||||
[[nodiscard]] HttpResponse
|
||||
kv_delete(std::string_view key, const HttpRequest& request);
|
||||
[[nodiscard]] HttpResponse
|
||||
kv_scan(const HttpRequest& request);
|
||||
void note(const HttpResponse& response);
|
||||
|
||||
KvStore& m_store;
|
||||
ApiConfig m_config;
|
||||
bool m_ready{true};
|
||||
ApiMetrics m_metrics;
|
||||
};
|
||||
} // namespace NetworkedKv
|
||||
Reference in new issue
Block a user