@@ -35,13 +35,8 @@ manapi::net::http::server & manapi::net::http::server::operator=(const server &n
3535struct 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
4742manapi::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+
68209manapi::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 );
0 commit comments