• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polyphony-chat / sonata / 16240259554

12 Jul 2025 05:13PM UTC coverage: 80.18% (-1.6%) from 81.791%
16240259554

push

github

bitfl0wer
chore: sqlx-cli prepare

8 of 18 branches covered (44.44%)

Branch coverage included in aggregate %.

259 of 315 relevant lines covered (82.22%)

589.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/api/mod.rs
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4

5
use log::info;
6
use poem::http::{Method, StatusCode};
7
use poem::listener::TcpListener;
8
use poem::middleware::{Cors, NormalizePath};
9
use poem::web::Json;
10
use poem::{EndpointExt, IntoResponse, Response, Route, Server, handler};
11
use serde_json::json;
12

13
use crate::config::ApiConfig;
14
use crate::database::Database;
15
use crate::database::tokens::TokenStore;
16

17
/// Admin-only functionality.
18
pub(super) mod admin;
19
/// Custom middlewares, such as authentication and active-user.
20
pub(crate) mod middlewares;
21

22
#[allow(clippy::expect_used)]
23
#[cfg_attr(coverage_nightly, coverage(off))]
24
/// Build the API [Route]s and start a `tokio::task`, which is a poem [Server] processing incoming
25
/// HTTP API requests.
26
pub(super) fn start_api(
27
    api_config: ApiConfig,
28
    db: Database,
29
    token_store: TokenStore,
30
) -> tokio::task::JoinHandle<()> {
31
    let routes = Route::new()
32
        .at("/healthz", healthz)
33
        .nest("/.p2/core/", setup_p2_core_routes())
34
        .with(NormalizePath::new(poem::middleware::TrailingSlash::Trim))
35
        .with(Cors::new().allow_methods(&[
36
            Method::CONNECT,
37
            Method::GET,
38
            Method::POST,
39
            Method::PUT,
40
            Method::DELETE,
41
            Method::PATCH,
42
            Method::OPTIONS,
43
        ]))
44
        .catch_all_error(custom_error)
45
        .data(db)
46
        .data(token_store);
47

48
    let handle = tokio::task::spawn(async move {
49
        Server::new(TcpListener::bind((api_config.host.as_str().trim(), api_config.port)))
50
            .run(routes)
51
            .await
52
            .expect("Failed to start HTTP server");
53
        log::info!("HTTP Server stopped");
54
    });
55
    info!("Started HTTP API server");
56
    handle
57
}
58

59
/// Catch-all fallback error.
60
async fn custom_error(err: poem::Error) -> impl IntoResponse {
×
61
    Json(json! ({
×
62
        "success": false,
×
63
        "message": err.to_string(),
×
64
    }))
×
65
    .with_status(err.status())
×
66
}
×
67

68
#[handler]
69
fn healthz() -> impl IntoResponse {
×
70
    Response::builder().status(StatusCode::OK).finish()
×
71
}
×
72

73
/// All routes under `/.p2/core/`.
74
fn setup_p2_core_routes() -> Route {
×
75
    Route::new()
×
76
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc