magpie
Loading...
Searching...
No Matches
App.hpp
1#pragma once
2
3#include "magpie/AppDecl.hpp"
4#include "magpie/application/Methods.hpp"
5#include "magpie/config/AppConfig.hpp"
6#include "magpie/data/CommonData.hpp"
7#include "magpie/middlewares/Middleware.hpp"
8#include "magpie/routing/Compile.hpp"
9#include "magpie/routing/Router.hpp"
10#include "magpie/transport/TCPServer.hpp"
11#include "raven/config/SSLConfig.hpp"
12#include <memory>
13#include <type_traits>
14
15namespace magpie {
16
17template <data::IsCommonData ContextType = data::CommonData>
18class App : public ContextApp<ContextType> {
19private:
21
22 std::shared_ptr<ContextType> dataStore;
23 std::shared_ptr<routing::BaseRouter> router;
24
25 std::shared_ptr<Middlewares<ContextType>> middlewares;
26
27public:
28
29 App(
30 std::shared_ptr<ContextType> dataStore,
31 AppConfig&& conf = {},
32 std::optional<raven::SSLConfig>&& sslConfig = std::nullopt
33 ): ContextApp<ContextType>(std::move(conf)),
34 serv(
35 this,
36 this->config.port,
37 this->config.concurrency,
38 this->config.bindAddr,
39 std::move(sslConfig)
40 ),
41 dataStore(dataStore),
42 router(std::make_shared<routing::Router<ContextType>>()),
43 middlewares(std::make_shared<Middlewares<ContextType>>())
44 {
45 dataStore->app = (BaseApp*) this;
46 }
47
48 // Not sure why clangd and/or clang-tidy whines about this being incorrect use; adding ::type breaks the build in
49 // all the combinations of `typename = [typename] std::enable_if[_v]<>[::type]` I could think of
50 template <typename = std::enable_if<std::is_trivially_default_constructible_v<ContextType>>>
51 App(
52 AppConfig&& conf = {},
53 std::optional<raven::SSLConfig>&& sslConfig = std::nullopt
54 ): App(std::make_shared<ContextType>(), std::move(conf), std::move(sslConfig)) {
55
56 }
57 ~App() = default;
58
72 template <
74 Method::HttpMethod method
75 >
77 const routing::RouteCallback<path, ContextType>& callback
78 ) {
79 return std::static_pointer_cast<routing::Router<ContextType>>(router)
80 ->template registerRoute<path>(
81 callback,
82 method
83 );
84 }
85
86 void run() override {
87 serv.start();
88 }
89
90 void shutdown() override {
91 serv.stop();
92 }
93
94 uint16_t getPort() {
95 return this->serv.getPort();
96 }
97
98 void sync() {
99 this->serv.sync();
100 }
101
102 const routing::BaseRouter& getRouter() override {
103 return *this->router;
104 }
105
106 data::CommonData* getContext() const override {
107 return this->dataStore.get();
108 }
109
110 void registerGlobalMiddlewares(
111 const std::vector<std::shared_ptr<Middleware<ContextType>>>& middlewares
112 ) {
113 this->middlewares->middlewares = middlewares;
114 }
115
116 Middlewares<ContextType>* getMiddlewaresAsPtr() override {
117 return this->middlewares.get();
118 }
119
120};
121
122}
routing::BaseRoute< ContextType > * route(const routing::RouteCallback< path, ContextType > &callback)
Definition App.hpp:76
Definition AppDecl.hpp:14
Definition BaseRoute.hpp:16
Definition Router.hpp:22
Definition TCPServer.hpp:14
Definition AppConfig.hpp:11
Definition Middleware.hpp:145
Definition Compile.hpp:19