diff options
| author | Rutger Broekhoff | 2026-08-28 22:17:10 +0200 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-08-28 22:17:10 +0200 |
| commit | 70643a9e4821bd11c3f363f1708fe420965467d1 (patch) | |
| tree | 65572b58e247248846628940a2aaca197a15d784 /server/src/srv.cppm | |
| parent | 173cd34bdd0717d3f4beeac76b3296ecd46ada0d (diff) | |
| download | routemon-70643a9e4821bd11c3f363f1708fe420965467d1.tar.gz routemon-70643a9e4821bd11c3f363f1708fe420965467d1.zip | |
Format again
Diffstat (limited to 'server/src/srv.cppm')
| -rw-r--r-- | server/src/srv.cppm | 94 |
1 files changed, 46 insertions, 48 deletions
diff --git a/server/src/srv.cppm b/server/src/srv.cppm index d9ff880..5763bc5 100644 --- a/server/src/srv.cppm +++ b/server/src/srv.cppm | |||
| @@ -63,17 +63,17 @@ public: | |||
| 63 | auto unwrap() -> gpx::file&& | 63 | auto unwrap() -> gpx::file&& |
| 64 | { | 64 | { |
| 65 | return std::visit( | 65 | return std::visit( |
| 66 | util::overloaded{ | 66 | util::overloaded{ |
| 67 | [](std::exception_ptr ex) -> gpx::file&& | 67 | [](std::exception_ptr ex) -> gpx::file&& |
| 68 | { | 68 | { |
| 69 | if (ex) | 69 | if (ex) |
| 70 | std::rethrow_exception(ex); | 70 | std::rethrow_exception(ex); |
| 71 | else | 71 | else |
| 72 | throw std::runtime_error{"no GPX file parse result available"}; | 72 | throw std::runtime_error{"no GPX file parse result available"}; |
| 73 | }, | ||
| 74 | [](gpx::file&& f) -> gpx::file&& { return std::move(f); }, | ||
| 75 | }, | 73 | }, |
| 76 | std::move(res_)); | 74 | [](gpx::file&& f) -> gpx::file&& { return std::move(f); }, |
| 75 | }, | ||
| 76 | std::move(res_)); | ||
| 77 | } | 77 | } |
| 78 | }; | 78 | }; |
| 79 | 79 | ||
| @@ -100,7 +100,7 @@ struct readable_gpx_body | |||
| 100 | 100 | ||
| 101 | auto | 101 | auto |
| 102 | init(boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept | 102 | init(boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept |
| 103 | -> void | 103 | -> void |
| 104 | { | 104 | { |
| 105 | try | 105 | try |
| 106 | { | 106 | { |
| @@ -114,9 +114,9 @@ struct readable_gpx_body | |||
| 114 | } | 114 | } |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | auto | 117 | auto put( |
| 118 | put(beast::concepts::const_buffer_sequence auto b, | 118 | beast::concepts::const_buffer_sequence auto b, |
| 119 | beast::error_code& ec) noexcept -> std::size_t | 119 | beast::error_code& ec) noexcept -> std::size_t |
| 120 | { | 120 | { |
| 121 | auto total = 0uz; | 121 | auto total = 0uz; |
| 122 | try | 122 | try |
| @@ -125,9 +125,7 @@ struct readable_gpx_body | |||
| 125 | it != net::buffer_sequence_end(b); it++) | 125 | it != net::buffer_sequence_end(b); it++) |
| 126 | { | 126 | { |
| 127 | r_.put( | 127 | r_.put( |
| 128 | std::string_view{ | 128 | std::string_view{static_cast<char const*>(it->data()), it->size()}); |
| 129 | static_cast<char const*>(it->data()), it->size() | ||
| 130 | }); | ||
| 131 | total += it->size(); | 129 | total += it->size(); |
| 132 | } | 130 | } |
| 133 | ec = {}; | 131 | ec = {}; |
| @@ -168,25 +166,25 @@ public: | |||
| 168 | 166 | ||
| 169 | private: | 167 | private: |
| 170 | auto handle_process_gpx(l0_ctx ctx, http::readable_request r) | 168 | auto handle_process_gpx(l0_ctx ctx, http::readable_request r) |
| 171 | -> net::awaitable<http::presponse> | 169 | -> net::awaitable<http::presponse> |
| 172 | { | 170 | { |
| 173 | auto gpx_file = gpx::file{}; | 171 | auto gpx_file = gpx::file{}; |
| 174 | try | 172 | try |
| 175 | { | 173 | { |
| 176 | auto req = | 174 | auto req = |
| 177 | co_await http::read_request<readable_gpx_body>(ctx, std::move(r)); | 175 | co_await http::read_request<readable_gpx_body>(ctx, std::move(r)); |
| 178 | gpx_file = std::move(req->body().unwrap()); | 176 | gpx_file = std::move(req->body().unwrap()); |
| 179 | } | 177 | } |
| 180 | catch (std::exception& ex) | 178 | catch (std::exception& ex) |
| 181 | { | 179 | { |
| 182 | // TODO: more detailed problem reporting | 180 | // TODO: more detailed problem reporting |
| 183 | auto tpl = problem::tpl{ | 181 | auto tpl = problem::tpl{ |
| 184 | .status = bhttp::status::bad_request, | 182 | .status = bhttp::status::bad_request, |
| 185 | .title = translate("Failed to parse GPX file"), | 183 | .title = translate("Failed to parse GPX file"), |
| 186 | .type_uri = "https://routemon.fautchen.eu/problems/gpx-parse-failed", | 184 | .type_uri = "https://routemon.fautchen.eu/problems/gpx-parse-failed", |
| 187 | }; | 185 | }; |
| 188 | co_return http::problem_rsp( | 186 | co_return http::problem_rsp( |
| 189 | ctx, tpl.instantiate(), http::keep_alive{false}); | 187 | ctx, tpl.instantiate(), http::keep_alive{false}); |
| 190 | } | 188 | } |
| 191 | 189 | ||
| 192 | // TODO: catch handler exceptions and return 500 when raised? | 190 | // TODO: catch handler exceptions and return 500 when raised? |
| @@ -195,17 +193,17 @@ private: | |||
| 195 | if (!mres) | 193 | if (!mres) |
| 196 | { | 194 | { |
| 197 | auto tpl = problem::tpl{ | 195 | auto tpl = problem::tpl{ |
| 198 | .status = bhttp::status::internal_server_error, | 196 | .status = bhttp::status::internal_server_error, |
| 199 | .title = translate("Internal server error"), | 197 | .title = translate("Internal server error"), |
| 200 | .type_uri = "https://routemon.fautchen.eu/problems/" | 198 | .type_uri = "https://routemon.fautchen.eu/problems/" |
| 201 | "internal-server-error", | 199 | "internal-server-error", |
| 202 | }; | 200 | }; |
| 203 | co_return http::problem_rsp( | 201 | co_return http::problem_rsp( |
| 204 | ctx, tpl.instantiate(), http::keep_alive{true}); | 202 | ctx, tpl.instantiate(), http::keep_alive{true}); |
| 205 | } | 203 | } |
| 206 | 204 | ||
| 207 | auto rsp = http::make_rsp<bhttp::string_body>( | 205 | auto rsp = http::make_rsp<bhttp::string_body>( |
| 208 | bhttp::status::ok, http::keep_alive{true}); | 206 | bhttp::status::ok, http::keep_alive{true}); |
| 209 | rsp.set(bhttp::field::content_type, "application/json"); | 207 | rsp.set(bhttp::field::content_type, "application/json"); |
| 210 | rsp.body() = json::serialize(json::value_from(*mres)); | 208 | rsp.body() = json::serialize(json::value_from(*mres)); |
| 211 | rsp.prepare_payload(); | 209 | rsp.prepare_payload(); |
| @@ -213,14 +211,14 @@ private: | |||
| 213 | } | 211 | } |
| 214 | 212 | ||
| 215 | auto handle_sysinfo(l0_ctx ctx, http::readable_request r) | 213 | auto handle_sysinfo(l0_ctx ctx, http::readable_request r) |
| 216 | -> net::awaitable<http::presponse> | 214 | -> net::awaitable<http::presponse> |
| 217 | { | 215 | { |
| 218 | auto req = | 216 | auto req = |
| 219 | co_await http::read_request<bhttp::empty_body>(ctx, std::move(r)); | 217 | co_await http::read_request<bhttp::empty_body>(ctx, std::move(r)); |
| 220 | auto info = inner_.sysinfo(); | 218 | auto info = inner_.sysinfo(); |
| 221 | 219 | ||
| 222 | auto rsp = http::make_rsp<bhttp::string_body>( | 220 | auto rsp = http::make_rsp<bhttp::string_body>( |
| 223 | bhttp::status::ok, http::keep_alive{true}); | 221 | bhttp::status::ok, http::keep_alive{true}); |
| 224 | rsp.set(bhttp::field::content_type, "application/json"); | 222 | rsp.set(bhttp::field::content_type, "application/json"); |
| 225 | rsp.body() = json::serialize(json::value_from(info)); | 223 | rsp.body() = json::serialize(json::value_from(info)); |
| 226 | rsp.prepare_payload(); | 224 | rsp.prepare_payload(); |
| @@ -236,17 +234,17 @@ public: | |||
| 236 | { return std::bind_front(member, this); }; | 234 | { return std::bind_front(member, this); }; |
| 237 | 235 | ||
| 238 | return http::dtree<http::routed_ctx<outer_ctx>>{}.named_subtrees({ | 236 | return http::dtree<http::routed_ctx<outer_ctx>>{}.named_subtrees({ |
| 239 | {"gpx", | 237 | {"gpx", |
| 240 | http::dtree<l0_ctx>{ | 238 | http::dtree<l0_ctx>{ |
| 241 | { | 239 | { |
| 242 | .post = handler(&handler::handle_process_gpx), | 240 | .post = handler(&handler::handle_process_gpx), |
| 243 | } | 241 | } |
| 244 | }.no_subtrees()}, | 242 | }.no_subtrees()}, |
| 245 | {"sysinfo", http::dtree<l0_ctx>{ | 243 | {"sysinfo", http::dtree<l0_ctx>{ |
| 246 | { | 244 | { |
| 247 | .get = handler(&handler::handle_sysinfo), | 245 | .get = handler(&handler::handle_sysinfo), |
| 248 | } | 246 | } |
| 249 | }.no_subtrees()}, | 247 | }.no_subtrees()}, |
| 250 | }); | 248 | }); |
| 251 | } | 249 | } |
| 252 | }; | 250 | }; |
| @@ -257,13 +255,13 @@ export class server | |||
| 257 | http::server<handler::outer_ctx> srv_; | 255 | http::server<handler::outer_ctx> srv_; |
| 258 | 256 | ||
| 259 | static auto make_global_middleware() | 257 | static auto make_global_middleware() |
| 260 | -> http::middleware_t<http::base_ctx, handler::outer_ctx> | 258 | -> http::middleware_t<http::base_ctx, handler::outer_ctx> |
| 261 | { | 259 | { |
| 262 | return http::middleware_compose< | 260 | return http::middleware_compose< |
| 263 | http::base_ctx, http::trace_id_ctx<http::base_ctx>, | 261 | http::base_ctx, http::trace_id_ctx<http::base_ctx>, |
| 264 | http::trace_id_ctx<http::base_ctx>>( | 262 | http::trace_id_ctx<http::base_ctx>>( |
| 265 | http::trace_id_middleware<http::base_ctx>, | 263 | http::trace_id_middleware<http::base_ctx>, |
| 266 | http::lax_cors_middleware<http::trace_id_ctx<http::base_ctx>>); | 264 | http::lax_cors_middleware<http::trace_id_ctx<http::base_ctx>>); |
| 267 | } | 265 | } |
| 268 | 266 | ||
| 269 | public: | 267 | public: |