30 static constexpr std::string_view kName =
"easy-dependencies";
32 ~DependenciesBase()
override;
44template <
class Dependencies>
49 static constexpr std::string_view
kName =
"easy-dependencies";
51 DependenciesComponent(
const components::ComponentConfig& config,
const components::ComponentContext& context)
52 : DependenciesBase(config, context),
53 dependencies_(context)
56 Dependencies GetDependencies()
const {
return dependencies_; }
59 Dependencies dependencies_;
66 using Callback = std::function<std::string(
const server::
http::HttpRequest&,
const impl::DependenciesBase&)>;
72 void Route(std::string_view path, Callback&& func, std::initializer_list<server::
http::
HttpMethod> methods);
75 template <
class Component>
81 component_list_.Append<Component>(name);
82 AddComponentConfig(name, config);
86 template <
class Component>
87 bool TryAddComponent(std::string_view name) {
92 component_list_.Append<Component>(name);
97 void DbSchema(std::string_view schema);
103 void Port(std::uint16_t port);
110 friend class HttpWith;
112 void AddComponentConfig(std::string_view name, std::string_view config);
114 HttpBase(
int argc,
const char*
const argv[]);
120 const char*
const* argv_;
121 std::string static_config_;
124 std::uint16_t port_ = 8080;
131template <
class... Dependency>
132class Dependencies
final :
public Dependency... {
134 explicit Dependencies(
const components::ComponentContext& context) : Dependency{context}... {}
136 static void RegisterOn(HttpBase& app) { (Dependency::RegisterOn(app), ...); }
143template <
class Dependency = Dependencies<>>
144class HttpWith
final {
156 class Callback
final {
158 template <
class Function>
159 Callback(Function func);
161 HttpBase::Callback Extract() &&
noexcept {
return std::move(func_); }
164 static Dependency GetDependencies(
const impl::DependenciesBase& deps) {
165 return static_cast<
const DependenciesComponent&>(deps).GetDependencies();
167 HttpBase::Callback func_;
170 HttpWith(
int argc,
const char*
const argv[])
173 impl_.TryAddComponent<DependenciesComponent>(DependenciesComponent::kName);
175 ~HttpWith() { Dependency::RegisterOn(impl_); }
184 std::string_view path,
195 impl_
.Route(path
, std::move(func).Extract()
, methods
);
200 HttpWith&
Get(std::string_view path, Callback&& func) {
206 HttpWith&
Post(std::string_view path, Callback&& func) {
212 HttpWith&
Del(std::string_view path, Callback&& func) {
218 HttpWith&
Put(std::string_view path, Callback&& func) {
224 HttpWith&
Patch(std::string_view path, Callback&& func) {
236 HttpWith&
Port(std::uint16_t port) {
253template <
class Function>
254HttpWith<Dependency>::Callback::Callback(Function func) {
255 using server::
http::HttpRequest;
257 constexpr unsigned kMatches =
260 (std::is_invocable_r_v<
formats::
json::
Value, Function,
const HttpRequest&,
const Dependency&> << 2) |
261 (std::is_invocable_r_v<std::string, Function,
const HttpRequest&,
const Dependency&> << 3) |
262 (std::is_invocable_r_v<
formats::
json::
Value, Function,
const HttpRequest&> << 4) |
263 (std::is_invocable_r_v<std::string, Function,
const HttpRequest&> << 5);
266 "Failed to find a matching signature. See the easy::HttpWith::Callback docs for info on "
267 "supported signatures"
269 constexpr bool has_single_match = ((kMatches & (kMatches - 1)) == 0);
272 "Found more than one matching signature, probably due to `auto` usage in parameters. See "
273 "the easy::HttpWith::Callback docs for info on supported signatures"
276 if constexpr (kMatches & 1) {
277 func_ = [f = std::move(func)](
const HttpRequest& req,
const impl::DependenciesBase& deps) {
281 }
else if constexpr (kMatches & 2) {
282 func_ = [f = std::move(func)](
const HttpRequest& req,
const impl::DependenciesBase&) {
286 }
else if constexpr (kMatches & 4) {
287 func_ = [f = std::move(func)](
const HttpRequest& req,
const impl::DependenciesBase& deps) {
291 }
else if constexpr (kMatches & 8) {
292 func_ = [f = std::move(func)](
const HttpRequest& req,
const impl::DependenciesBase& deps) {
293 return f(req, GetDependencies(deps));
295 }
else if constexpr (kMatches & 16) {
296 func_ = [f = std::move(func)](
const HttpRequest& req,
const impl::DependenciesBase&) {
301 static_assert(kMatches & 32);
302 func_ = [f = std::move(func)](
const HttpRequest& req,
const impl::DependenciesBase&) {
return f(req); };
309 explicit PgDep(
const components::ComponentContext& context);
310 storages::
postgres::
Cluster& pg()
const noexcept {
return *pg_cluster_; }
311 static void RegisterOn(HttpBase& app);
314 storages::
postgres::ClusterPtr pg_cluster_;
320 explicit HttpDep(
const components::ComponentContext& context);
321 clients::http::
Client& http() {
return http_; }
322 static void RegisterOn(HttpBase& app);
325 clients::http::
Client& http_;