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