pub struct JetServer { /* private fields */ }Expand description
AT-Jet HTTP Server
A high-performance HTTP server optimized for Protobuf APIs.
§Example
use at_jet::prelude::*;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let server = JetServer::new()
.route("/api/health", get(health_check))
.with_cors()
.with_compression()
.with_tracing();
server.serve("0.0.0.0:8080").await?;
Ok(())
}
async fn health_check() -> &'static str {
"OK"
}Implementations§
Source§impl JetServer
impl JetServer
Sourcepub fn route(self, path: &str, method_router: MethodRouter) -> Self
pub fn route(self, path: &str, method_router: MethodRouter) -> Self
Sourcepub fn with_cors(self) -> Self
pub fn with_cors(self) -> Self
Enable CORS with permissive defaults (suitable for development)
This allows any origin, method, and header. For production,
use with_cors_config() for fine-grained control.
§Example
ⓘ
let server = JetServer::new()
.route("/api/users", get(list_users))
.with_cors(); // Allow all origins (development only)Sourcepub fn with_cors_config(self, cors: CorsLayer) -> Self
pub fn with_cors_config(self, cors: CorsLayer) -> Self
Enable CORS with custom configuration (recommended for production)
§Example
ⓘ
use tower_http::cors::{CorsLayer, AllowOrigin};
use http::{HeaderValue, Method};
let cors = CorsLayer::new()
.allow_origin([
"https://app.example.com".parse::<HeaderValue>().unwrap(),
"https://admin.example.com".parse::<HeaderValue>().unwrap(),
])
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE])
.allow_headers([http::header::CONTENT_TYPE, http::header::AUTHORIZATION])
.allow_credentials(true)
.max_age(Duration::from_secs(3600));
let server = JetServer::new()
.route("/api/users", get(list_users))
.with_cors_config(cors);Sourcepub fn with_compression(self) -> Self
pub fn with_compression(self) -> Self
Enable response compression (gzip)
Sourcepub fn with_tracing(self) -> Self
pub fn with_tracing(self) -> Self
Enable request/response tracing
This adds detailed logging for HTTP requests including:
- Request method, URI, and version
- Response status code
- Request duration
Requires a tracing subscriber to be initialized.
§Example
ⓘ
use tracing_subscriber::EnvFilter;
// Initialize tracing subscriber
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
let server = JetServer::new()
.route("/api/users", get(list_users))
.with_tracing();Sourcepub fn layer<L>(self, layer: L) -> Self
pub fn layer<L>(self, layer: L) -> Self
Add a custom middleware layer to the server
This method allows you to add any tower-compatible middleware layer.
§Example - Request Timeout
ⓘ
use std::time::Duration;
use tower_http::timeout::TimeoutLayer;
let server = JetServer::new()
.route("/api/users", get(list_users))
.layer(TimeoutLayer::new(Duration::from_secs(30)));§Example - Custom Authentication Middleware
ⓘ
use axum::{middleware, http::Request, response::Response};
async fn auth_middleware<B>(
request: Request<B>,
next: axum::middleware::Next<B>,
) -> Response {
// Check authentication here
next.run(request).await
}
let server = JetServer::new()
.route("/api/users", get(list_users))
.layer(middleware::from_fn(auth_middleware));Sourcepub fn into_router(self) -> Router
pub fn into_router(self) -> Router
Get the router for advanced customization
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for JetServer
impl !RefUnwindSafe for JetServer
impl Send for JetServer
impl Sync for JetServer
impl Unpin for JetServer
impl !UnwindSafe for JetServer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more