Actix web framework reference guide

What is Actix Web?

๐Ÿฆ€ Powerful, pragmatic, and extremely fast web framework for Rust

Actix Web is a modern Rust web framework that delivers blazing fast performance with type safety and a rich feature set. Built on Actix actor framework, it provides everything you need for production-ready web applications.

  • Type Safe - Forget stringly typed objects, from request to response everything has types
  • Blazingly Fast - One of the fastest web frameworks according to TechEmpower benchmarks
  • Feature Rich - HTTP/2, logging, middleware, sessions, WebSockets, and more out of the box
  • Powerful Extractors - Extract data from HTTP requests with automatic deserialization and validation
  • Flexible Responders - Return various types that implement the Responder trait for consistent APIs
  • Easy Form Handling - Handle JSON and URL-encoded forms with automatic parsing

๐Ÿ”ง AstroX Backend Features

Our Actix backend implementation includes production-ready features for authentication, API endpoints, and session management.

๐Ÿ” Authentication & Sessions

๐ŸŒ API Endpoints

โš™๏ธ Configuration & Middleware

๐Ÿงช Testing & Quality

๐Ÿ“ Code Examples

Protected API Endpoint

Example from src/backend/src/api/protected/get.rs - demonstrates session validation:

#[get("/api/protected")] pub async fn protected_endpoint(session: Session) -> impl Responder {   match validate_session(&session) {     Ok(_) => HttpResponse::Ok().json(...),     Err(_) => HttpResponse::Unauthorized().json(...)   } }

Custom Authentication Middleware

From src/backend/src/auth/auth_middleware.rs - protects routes using glob patterns:

pub struct Authentication {   pub routes: Vec<String>, } // Usage in main.rs: let auth_routes = vec!["/auth/*".to_string()]; App::new().wrap(Authentication { routes: auth_routes })

External API Proxy

From src/backend/src/api/space_x/get.rs - proxies external API requests:

#[get("/api/space-x")] pub async fn json_get_space_x() -> Result<HttpResponse, ActixError> {   let response = get("https://api.spacexdata.com/v3/rockets").await;   // Handle response and return JSON }

CORS Configuration

From src/backend/src/cors/get_cors_options.rs - environment-based CORS:

pub fn get_cors_options(env: String, allowed_origin: String) -> Cors {   if env == "prod" {     Cors::default().allowed_origin(&allowed_origin)   } else {     Cors::default().allow_any_origin()   } }

Session Validation

From src/backend/src/session/validate_session.rs - checks user authentication:

pub fn validate_session(session: &Session) -> anyhow::Result<()> {   let user_id: Option<i64> = session.get("user_id")?;   match user_id {     Some(_) => { session.renew(); Ok(()) }     None => Err(anyhow::anyhow!("Not authenticated"))   } }

๐Ÿš€ Getting Started

The Actix server starts automatically when you run the AstroX CLI. It serves both API endpoints and the built frontend.