Skip to content

Commit b0cff36

Browse files
committed
FIX | ctoken
1 parent ff88ebe commit b0cff36

10 files changed

Lines changed: 206 additions & 200 deletions

include/ManapiHttp.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ namespace manapi::net::http {
112112
* @return the future
113113
*/
114114
manapi::future<manapi::status> stop () override;
115+
116+
manapi::future<manapi::status> config(std::string path) override;
117+
118+
manapi::future<manapi::status> config_object(json config) override;
115119
private:
116120
std::shared_ptr<site> copy() override;
117121

include/http/ManapiSite.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ namespace manapi::net::http {
211211
* @param path file path
212212
* @return a future
213213
*/
214-
manapi::future<manapi::status> config (std::string path);
214+
virtual manapi::future<manapi::status> config (std::string path) = 0;
215215

216216
/**
217217
* set a config JSON object
218218
* @param config config JSON object
219219
* @return a future
220220
*/
221-
manapi::future<manapi::status> config_object (json config);
221+
virtual manapi::future<manapi::status> config_object (json config) = 0;
222222

223223
// const manapi::json &config ();
224224

main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ int main() {
126126
db->add_slave(std::move(slave)).unwrap();
127127

128128
manapi::unwrap(co_await route.config(manapi::fs::path::join(".", "config.json")));
129+
130+
manapi::unwrap(co_await route.start());
129131
});
130132

131133
cb();

src/ManapiEventLoop.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -681,25 +681,27 @@ static void m_event_loop_free_on_finish_cb_( std::map <size_t, std::move_only_fu
681681
}
682682

683683
static manapi::future<> m_event_loop_call_on_finish_cb(std::map <size_t, std::pair<int, std::move_only_function<manapi::future<>()>>> &m) {
684-
using item_t = std::pair<int, std::move_only_function<manapi::future<void>()>>;
685-
std::vector<item_t> values;
686-
values.reserve(m.size());
687684
while (!m.empty()) {
688-
const auto it = m.extract(m.begin());
689-
values.push_back(std::move(it.mapped()));
690-
}
685+
using item_t = std::pair<int, std::move_only_function<manapi::future<void>()>>;
686+
std::vector<item_t> values;
687+
values.reserve(m.size());
688+
while (!m.empty()) {
689+
const auto it = m.extract(m.begin());
690+
values.push_back(std::move(it.mapped()));
691+
}
691692

692-
std::sort(values.begin(), values.end(), +[](const item_t& a, const item_t& b)
693-
-> bool { return a.first > b.first; });
693+
std::sort(values.begin(), values.end(), +[](const item_t& a, const item_t& b)
694+
-> bool { return a.first > b.first; });
694695

695-
for (auto &it : values) {
696-
if (it.second) {
697-
try {
698-
auto callback = std::move(it.second);
699-
co_await manapi::async::invoke(std::move(callback));
700-
}
701-
catch (std::exception const &e) {
702-
manapi_log_error("eventloop:finish callback failed due to %s", e.what());
696+
for (auto &it : values) {
697+
if (it.second) {
698+
try {
699+
auto callback = std::move(it.second);
700+
co_await manapi::async::invoke(std::move(callback));
701+
}
702+
catch (std::exception const &e) {
703+
manapi_log_error("eventloop:finish callback failed due to %s", e.what());
704+
}
703705
}
704706
}
705707
}
@@ -880,6 +882,9 @@ manapi::future<> manapi::event_loop::stop() {
880882

881883
::m_event_loop_free_on_finish_cb_(this->m_map_clean_up_cb);
882884

885+
this->m_map_finish_cb.clear();
886+
this->m_map_clean_up_cb.clear();
887+
883888
if (this->m_flags & EVENT_LOOP_FLAG_ACTIVE) {
884889
::uv_stop(this->m_loop.get());
885890
}

src/ManapiHttp.cpp

Lines changed: 146 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,8 @@ manapi::net::http::server & manapi::net::http::server::operator=(const server &n
3535
struct manapi::net::http::server::data2_t : manapi::net::http::site::data_t {
3636
pools_t pools;
3737
std::size_t next_pool_id;
38-
async::promise_sync<void>::resolve_t resolve_stop;
3938
std::shared_ptr<ev::async> init_watcher;
4039
uint8_t flags;
41-
42-
~data2_t () {
43-
std::cout << "OH NO\n";
44-
}
4540
};
4641

4742
manapi::net::http::server::server() : site() {
@@ -65,6 +60,152 @@ manapi::status_or<manapi::net::http::server> manapi::net::http::server::create(s
6560
}
6661
}
6762

63+
manapi::future<manapi::status> manapi::net::http::server::config(std::string path) {
64+
try {
65+
if (this->data->event_id)
66+
co_return status_already_exists("http:config already exists");
67+
68+
this->data->event_id = async::current()->eventloop()->subscribe_finish(-1, [p = *this] () mutable
69+
-> future<> {
70+
auto res = co_await p.stop();
71+
manapi_log_trace(manapi::debug::LOG_TRACE_HIGH, "http:Stop status=%.*s", res.msg().size(), res.msg().data());
72+
});
73+
74+
this->data->clean_up_id = async::current()->eventloop()->subscribe_clean_up([p = *this] () mutable
75+
-> void {
76+
p.clean_up();
77+
});
78+
79+
this->data->server_config = co_await this->data->sctx.storage().subscribe([p = *this] (auto &&f1)
80+
-> void { on_config_update(p.data, std::forward<decltype(f1)>(f1)); });
81+
82+
auto res = co_await this->data->sctx.storage().edit_async (this->data->server_config,
83+
[this, path = std::move(path)] (manapi::json &config) mutable -> manapi::future<bool> {
84+
if (!config.is_object())
85+
config = manapi::json::object();
86+
87+
if (!config.contains("site") || !config["site"].is_object()) {
88+
89+
try {
90+
auto exists = co_await manapi::fs::async_exists(path);
91+
if (!exists.ok() || !exists.unwrap())
92+
{
93+
std::string data = manapi::json::object().dump(4);
94+
auto res = co_await manapi::fs::async_write(path, std::move(data), ev::IRWXU, ev::FS_O_CREAT|ev::FS_O_TRUNC|ev::FS_O_WRONLY);
95+
res.unwrap();
96+
}
97+
98+
auto res = co_await manapi::fs::async_read (path);
99+
auto obj = manapi::json(res.unwrap(), true);
100+
101+
if (!obj.is_object())
102+
obj = manapi::json::object();
103+
104+
config["site"] = std::move(obj);
105+
}
106+
catch (std::exception const &e) {
107+
MANAPIHTTP_LOG("server router: config read failed due to {}", e.what());
108+
}
109+
110+
config["site_time"] = 0;
111+
config["site_path"] = std::move(path);
112+
config["cache_time"] = 0;
113+
114+
co_await this->setup_config(config);
115+
*this->data->config_ = config;
116+
co_return true;
117+
}
118+
*this->data->config_ = config;
119+
co_return false;
120+
});
121+
res.unwrap();
122+
co_return status_ok();
123+
}
124+
catch (std::exception const &e) {
125+
manapi_log_error("%s due to %s", "http:config() failed", e.what());
126+
}
127+
128+
auto res = manapi::status_ok();
129+
try {
130+
res = co_await this->stop();
131+
this->clean_up();
132+
}
133+
catch (std::exception const &e) {
134+
res = manapi::status_unknown(std::string{e.what()});
135+
}
136+
137+
if (!res.ok())
138+
manapi_log_ferror("http:failed to stop due to %s", res.msg().data());
139+
140+
co_return status_internal("http:config() failed");
141+
}
142+
143+
manapi::future<manapi::status> manapi::net::http::server::config_object(json config) {
144+
try {
145+
if (this->data->event_id)
146+
co_return status_already_exists("http:config already exists");
147+
148+
this->data->event_id = async::current()->eventloop()->subscribe_finish(-1, [p = *this] () mutable
149+
-> future<> {
150+
auto res = co_await p.stop();
151+
manapi_log_trace(manapi::debug::LOG_TRACE_HIGH, "http:Stop status=%.*s", res.msg().size(), res.msg().data());
152+
});
153+
154+
this->data->clean_up_id = async::current()->eventloop()->subscribe_clean_up([p = *this] () mutable
155+
-> void {
156+
p.clean_up();
157+
});
158+
159+
this->data->server_config = co_await this->data->sctx.storage().subscribe([data = this->data] (auto &&f1)
160+
-> void { on_config_update(data, std::forward<decltype(f1)>(f1)); });
161+
162+
auto res = co_await this->data->sctx.storage().edit_async (this->data->server_config,
163+
[this, nconfig = std::move(config)] (manapi::json &config) mutable -> manapi::future<bool> {
164+
if (!config.is_object())
165+
config = manapi::json::object();
166+
167+
if (!config.contains("site") || !config["site"].is_object()) {
168+
169+
try {
170+
if (!nconfig.is_object())
171+
nconfig = manapi::json::object();
172+
173+
config["site"] = std::move(nconfig);
174+
}
175+
catch (std::exception const &e) {
176+
MANAPIHTTP_LOG("server router: config read failed due to {}", e.what());
177+
}
178+
}
179+
180+
config["site_time"] = 0;
181+
config["site_path"] = "";
182+
config["cache_time"] = 0;
183+
184+
co_await this->setup_config(config);
185+
co_return true;
186+
});
187+
res.unwrap();
188+
co_return status_ok();
189+
}
190+
catch (std::exception const &e) {
191+
manapi_log_error("%s due to %s", "http:config() failed", e.what());
192+
}
193+
194+
auto res = manapi::status_ok();
195+
try {
196+
res = co_await this->stop();
197+
this->clean_up();
198+
}
199+
catch (std::exception const &e) {
200+
res = manapi::status_unknown(std::string{e.what()});
201+
}
202+
203+
if (!res.ok())
204+
manapi_log_ferror("http:failed to stop due to %s", res.msg().data());
205+
206+
co_return status_internal("http:config() failed");
207+
}
208+
68209
manapi::future<manapi::status> manapi::net::http::server::start() {
69210
auto res = manapi::status_ok();
70211
auto data2 = std::static_pointer_cast<data2_t>(this->data);

src/fs/ManapiFilesystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ manapi::future<manapi::ev::status> manapi::fs::async_mkdir(std::string path, int
215215

216216
ctoken cancellation2 = ctoken::unit (cancellation);
217217

218-
auto err = co_await async_fs_operation<manapi::ev::status>([path = path.substr(0, size), mode, cancellation2, cancellation](std::shared_ptr<ev::fs> w) mutable
218+
auto err = co_await async_fs_operation<manapi::ev::status>([path = path.substr(0, size), mode](std::shared_ptr<ev::fs> w) mutable
219219
-> bool {
220220
return !w->mkdir(path.data(), mode);
221221
},

0 commit comments

Comments
 (0)