aws_smithy_http_server_python/
lib.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6/* Automatically managed default lints */
7#![cfg_attr(docsrs, feature(doc_auto_cfg))]
8/* End of automatically managed default lints */
9#![allow(clippy::derive_partial_eq_without_eq)]
10#![cfg_attr(docsrs, feature(doc_cfg))]
11
12//! Rust/Python bindings, runtime and utilities.
13//!
14//! This crates implements all the generic code needed to start and manage
15//! a Smithy Rust HTTP server where the business logic is implemented in Python,
16//! leveraging [PyO3].
17//!
18//! [PyO3]: https://pyo3.rs/
19
20pub mod context;
21mod error;
22pub mod lambda;
23pub mod logging;
24pub mod middleware;
25mod server;
26mod socket;
27pub mod tls;
28pub mod types;
29mod util;
30
31#[doc(inline)]
32pub use error::{PyError, PyMiddlewareException};
33#[doc(inline)]
34pub use logging::{py_tracing_event, PyTracingHandler};
35#[doc(inline)]
36pub use middleware::{PyMiddlewareHandler, PyMiddlewareLayer, PyRequest, PyResponse};
37#[doc(inline)]
38pub use server::{PyApp, PyHandler};
39#[doc(inline)]
40pub use socket::PySocket;
41#[doc(inline)]
42pub use util::error::{rich_py_err, RichPyErr};
43
44#[cfg(test)]
45mod tests {
46    use std::sync::Once;
47
48    use pyo3::{PyErr, Python};
49    use pyo3_asyncio::TaskLocals;
50
51    static INIT: Once = Once::new();
52
53    pub(crate) fn initialize() -> TaskLocals {
54        INIT.call_once(|| {
55            pyo3::prepare_freethreaded_python();
56        });
57
58        Python::with_gil(|py| {
59            let asyncio = py.import("asyncio")?;
60            let event_loop = asyncio.call_method0("new_event_loop")?;
61            asyncio.call_method1("set_event_loop", (event_loop,))?;
62            Ok::<TaskLocals, PyErr>(TaskLocals::new(event_loop))
63        })
64        .unwrap()
65    }
66}