diff options
Diffstat (limited to 'server/src/srv.cpp')
| -rw-r--r-- | server/src/srv.cpp | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/server/src/srv.cpp b/server/src/srv.cpp index b70f05c..f79da33 100644 --- a/server/src/srv.cpp +++ b/server/src/srv.cpp | |||
| @@ -19,6 +19,7 @@ import :gpx; | |||
| 19 | import :srv; | 19 | import :srv; |
| 20 | 20 | ||
| 21 | namespace beast = boost::beast; | 21 | namespace beast = boost::beast; |
| 22 | namespace chrono = std::chrono; | ||
| 22 | namespace json = boost::json; | 23 | namespace json = boost::json; |
| 23 | namespace net = boost::asio; | 24 | namespace net = boost::asio; |
| 24 | using tcp = boost::asio::ip::tcp; | 25 | using tcp = boost::asio::ip::tcp; |
| @@ -157,12 +158,22 @@ static_assert(bhttp::concepts::body_reader<readable_gpx_body>); | |||
| 157 | auto handler::handle_process_gpx(l0_ctx ctx, http::readable_request r) | 158 | auto handler::handle_process_gpx(l0_ctx ctx, http::readable_request r) |
| 158 | -> net::awaitable<http::presponse> | 159 | -> net::awaitable<http::presponse> |
| 159 | { | 160 | { |
| 161 | auto l = | ||
| 162 | l_.sub("handle_process_gpx").with("trace_id", ctx.trace_id.as_string()); | ||
| 163 | |||
| 160 | auto gpx_file = gpx::file{}; | 164 | auto gpx_file = gpx::file{}; |
| 161 | try | 165 | try |
| 162 | { | 166 | { |
| 167 | l.debug("Reading GPX request body"); | ||
| 168 | auto const before_read_gpx = chrono::steady_clock::now(); | ||
| 163 | auto req = | 169 | auto req = |
| 164 | co_await http::read_request<readable_gpx_body>(ctx, std::move(r)); | 170 | co_await http::read_request<readable_gpx_body>(ctx, std::move(r)); |
| 165 | gpx_file = std::move(req->body().unwrap()); | 171 | gpx_file = std::move(req->body().unwrap()); |
| 172 | l.debug( | ||
| 173 | "Read GPX request body in {}", | ||
| 174 | chrono::duration<double, std::milli>{ | ||
| 175 | chrono::steady_clock::now() - before_read_gpx | ||
| 176 | }); | ||
| 166 | } | 177 | } |
| 167 | catch (std::exception& ex) | 178 | catch (std::exception& ex) |
| 168 | { | 179 | { |
| @@ -178,7 +189,7 @@ auto handler::handle_process_gpx(l0_ctx ctx, http::readable_request r) | |||
| 178 | 189 | ||
| 179 | // TODO: catch handler exceptions and return 500 when raised? | 190 | // TODO: catch handler exceptions and return 500 when raised? |
| 180 | // (keep-alive depends on whether whole request was read) | 191 | // (keep-alive depends on whether whole request was read) |
| 181 | auto mres = inner_.process_gpx(std::move(gpx_file)); | 192 | auto mres = inner_.process_gpx(ctx.trace_id, std::move(gpx_file)); |
| 182 | if (!mres) | 193 | if (!mres) |
| 183 | { | 194 | { |
| 184 | auto tpl = problem::tpl{ | 195 | auto tpl = problem::tpl{ |
| @@ -212,7 +223,10 @@ auto handler::handle_sysinfo(l0_ctx ctx, http::readable_request r) | |||
| 212 | co_return rsp; | 223 | co_return rsp; |
| 213 | } | 224 | } |
| 214 | 225 | ||
| 215 | handler::handler(api::handler&& inner) : inner_{std::move(inner)} {} | 226 | handler::handler(log::logger const& l, api::handler&& inner) |
| 227 | : l_{l.sub("handler")}, inner_{std::move(inner)} | ||
| 228 | { | ||
| 229 | } | ||
| 216 | 230 | ||
| 217 | auto handler::make_routes() -> http::route_tree<http::routed_ctx<outer_ctx>> | 231 | auto handler::make_routes() -> http::route_tree<http::routed_ctx<outer_ctx>> |
| 218 | { | 232 | { |
| @@ -235,22 +249,21 @@ auto handler::make_routes() -> http::route_tree<http::routed_ctx<outer_ctx>> | |||
| 235 | auto server::make_global_middleware(config::http_server const& cfg) | 249 | auto server::make_global_middleware(config::http_server const& cfg) |
| 236 | -> http::middleware_t<http::base_ctx, handler::outer_ctx> | 250 | -> http::middleware_t<http::base_ctx, handler::outer_ctx> |
| 237 | { | 251 | { |
| 238 | return http::middleware_compose< | 252 | return http:: |
| 239 | http::base_ctx, http::trace_id_ctx<http::base_ctx>, | 253 | middleware_compose<http::base_ctx, http::base_ctx, http::base_ctx>( |
| 240 | http::trace_id_ctx<http::base_ctx> | 254 | http::trace_id_middleware<http::base_ctx>, |
| 241 | >(http::trace_id_middleware<http::base_ctx>, | 255 | http::cors_middleware<http::base_ctx>(cfg.allow_origins)); |
| 242 | http::cors_middleware<http::trace_id_ctx<http::base_ctx>>( | ||
| 243 | cfg.allow_origins)); | ||
| 244 | } | 256 | } |
| 245 | 257 | ||
| 246 | server::server( | 258 | server::server( |
| 247 | log::logger const& l, config::http_server const& cfg, | 259 | log::logger const& l, config::http_server const& cfg, |
| 248 | locale::selector&& lsel, api::handler&& inner) | 260 | locale::selector&& lsel, api::handler&& inner) |
| 249 | : handler_{std::move(inner)}, | 261 | : handler_{l, std::move(inner)}, |
| 250 | srv_{ | 262 | srv_{ |
| 251 | l, http::router{ | 263 | l, |
| 252 | std::move(lsel), make_global_middleware(cfg), handler_.make_routes() | 264 | http::router{ |
| 253 | } | 265 | l, std::move(lsel), make_global_middleware(cfg), handler_.make_routes() |
| 266 | } | ||
| 254 | } | 267 | } |
| 255 | { | 268 | { |
| 256 | } | 269 | } |