App

Struct App 

Source
pub struct App { /* private fields */ }
Expand description

Main application builder

Implementations§

Source§

impl App

Source

pub fn new() -> Self

Create a new App instance

Examples found in repository?
examples/rest-api.rs (line 126)
121async fn main() {
122    // Create shared database
123    let db: Database = Arc::new(Mutex::new(HashMap::new()));
124
125    // Build and run the app
126    App::new()
127        .auto_configure()
128        .mount(user_routes().with_state(db))
129        .run()
130        .await
131        .unwrap();
132}
More examples
Hide additional examples
examples/auth-api.rs (line 164)
96async fn main() {
97    // Make sure AUTH_JWT_SECRET is set in environment
98    // For development, you can use the default, but set it in production!
99    let mut auth_config = AuthConfig::from_env();
100    auth_config.jwt_secret =
101        "rapid-rs-dev-secret-change-me-in-production-make-it-at-least-32-chars".to_string();
102
103    let user_store = InMemoryUserStore::new();
104
105    let app_state = AuthAppState {
106        config: auth_config.clone(),
107        user_store: user_store.clone(),
108    };
109
110    let admin_middleware_routes = Router::new()
111        .route("/middleware/admin", get(middleware_admin))
112        .layer(RequireRoles::any(vec!["admin"]));
113
114    let user_middleware_routes = Router::new()
115        .route("/middleware/user", get(middleware_user))
116        .layer(RequireRoles::any(vec!["user"]));
117
118    // Build routes
119    let protected_routes = Router::new()
120        .route("/protected", get(protected_route))
121        .route("/admin", get(admin_route))
122        .route("/public", get(public_route));
123
124    // Admin Registration Route
125    let auth_extras = Router::new()
126        .route("/auth/register-admin", post(register_admin))
127        .with_state(app_state);
128
129    println!("🔐 Auth API Example");
130    println!("==================");
131    println!();
132    println!("📝 Register a user:");
133    println!("   curl -X POST http://localhost:3000/auth/register -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"John Doe\\\"}}\"");
134    println!();
135    println!("🔑 Login:");
136    println!("   curl -X POST http://localhost:3000/auth/login -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\"}}\"");
137    println!();
138    println!("🔒 Access protected route:");
139    println!("   curl -X GET http://localhost:3000/protected -H \"Authorization: Bearer <access_token>\"");
140    println!();
141    println!("Scenario 1: Regular User");
142    println!("> Register a regular User:");
143    println!("  curl -X POST http://localhost:3000/auth/register -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"Basic Joe\\\"}}\"");
144    println!();
145    println!("> Test Admin Route (Should fail with code 403 Forbidden):");
146    println!("  curl -X GET http://localhost:3000/middleware/admin -H \"Authorization: Bearer <access_token>\"");
147    println!();
148    println!("Scenario 2: Admin User");
149    println!("> Register an ADMIN User:");
150    println!("  curl -X POST http://localhost:3000/auth/register-admin -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"admin@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"Admin Joe\\\"}}\"");
151    println!();
152    println!("> Test Admin Route (Should succed):");
153    println!("  curl -X GET http://localhost:3000/middleware/admin -H \"Authorization: Bearer <access_token>\"");
154
155    println!();
156
157    let api_routes = Router::new()
158        .merge(protected_routes)
159        .merge(admin_middleware_routes)
160        .merge(user_middleware_routes)
161        .merge(auth_extras)
162        .layer(from_fn_with_state(auth_config.clone(), inject_auth_config));
163
164    App::new()
165        .auto_configure()
166        .mount(auth_routes_with_store(auth_config, user_store))
167        .mount(api_routes)
168        .run()
169        .await
170        .unwrap();
171}
Source

pub fn auto_configure(self) -> Self

Auto-configure the application with sensible defaults:

  • Loads configuration from files and environment
  • Sets up structured logging with tracing
  • Configures CORS with permissive defaults
  • Adds health check endpoint
  • Enables Swagger UI at /docs
Examples found in repository?
examples/rest-api.rs (line 127)
121async fn main() {
122    // Create shared database
123    let db: Database = Arc::new(Mutex::new(HashMap::new()));
124
125    // Build and run the app
126    App::new()
127        .auto_configure()
128        .mount(user_routes().with_state(db))
129        .run()
130        .await
131        .unwrap();
132}
More examples
Hide additional examples
examples/auth-api.rs (line 165)
96async fn main() {
97    // Make sure AUTH_JWT_SECRET is set in environment
98    // For development, you can use the default, but set it in production!
99    let mut auth_config = AuthConfig::from_env();
100    auth_config.jwt_secret =
101        "rapid-rs-dev-secret-change-me-in-production-make-it-at-least-32-chars".to_string();
102
103    let user_store = InMemoryUserStore::new();
104
105    let app_state = AuthAppState {
106        config: auth_config.clone(),
107        user_store: user_store.clone(),
108    };
109
110    let admin_middleware_routes = Router::new()
111        .route("/middleware/admin", get(middleware_admin))
112        .layer(RequireRoles::any(vec!["admin"]));
113
114    let user_middleware_routes = Router::new()
115        .route("/middleware/user", get(middleware_user))
116        .layer(RequireRoles::any(vec!["user"]));
117
118    // Build routes
119    let protected_routes = Router::new()
120        .route("/protected", get(protected_route))
121        .route("/admin", get(admin_route))
122        .route("/public", get(public_route));
123
124    // Admin Registration Route
125    let auth_extras = Router::new()
126        .route("/auth/register-admin", post(register_admin))
127        .with_state(app_state);
128
129    println!("🔐 Auth API Example");
130    println!("==================");
131    println!();
132    println!("📝 Register a user:");
133    println!("   curl -X POST http://localhost:3000/auth/register -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"John Doe\\\"}}\"");
134    println!();
135    println!("🔑 Login:");
136    println!("   curl -X POST http://localhost:3000/auth/login -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\"}}\"");
137    println!();
138    println!("🔒 Access protected route:");
139    println!("   curl -X GET http://localhost:3000/protected -H \"Authorization: Bearer <access_token>\"");
140    println!();
141    println!("Scenario 1: Regular User");
142    println!("> Register a regular User:");
143    println!("  curl -X POST http://localhost:3000/auth/register -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"Basic Joe\\\"}}\"");
144    println!();
145    println!("> Test Admin Route (Should fail with code 403 Forbidden):");
146    println!("  curl -X GET http://localhost:3000/middleware/admin -H \"Authorization: Bearer <access_token>\"");
147    println!();
148    println!("Scenario 2: Admin User");
149    println!("> Register an ADMIN User:");
150    println!("  curl -X POST http://localhost:3000/auth/register-admin -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"admin@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"Admin Joe\\\"}}\"");
151    println!();
152    println!("> Test Admin Route (Should succed):");
153    println!("  curl -X GET http://localhost:3000/middleware/admin -H \"Authorization: Bearer <access_token>\"");
154
155    println!();
156
157    let api_routes = Router::new()
158        .merge(protected_routes)
159        .merge(admin_middleware_routes)
160        .merge(user_middleware_routes)
161        .merge(auth_extras)
162        .layer(from_fn_with_state(auth_config.clone(), inject_auth_config));
163
164    App::new()
165        .auto_configure()
166        .mount(auth_routes_with_store(auth_config, user_store))
167        .mount(api_routes)
168        .run()
169        .await
170        .unwrap();
171}
Source

pub fn mount(self, router: Router) -> Self

Mount additional routes

Examples found in repository?
examples/rest-api.rs (line 128)
121async fn main() {
122    // Create shared database
123    let db: Database = Arc::new(Mutex::new(HashMap::new()));
124
125    // Build and run the app
126    App::new()
127        .auto_configure()
128        .mount(user_routes().with_state(db))
129        .run()
130        .await
131        .unwrap();
132}
More examples
Hide additional examples
examples/auth-api.rs (line 166)
96async fn main() {
97    // Make sure AUTH_JWT_SECRET is set in environment
98    // For development, you can use the default, but set it in production!
99    let mut auth_config = AuthConfig::from_env();
100    auth_config.jwt_secret =
101        "rapid-rs-dev-secret-change-me-in-production-make-it-at-least-32-chars".to_string();
102
103    let user_store = InMemoryUserStore::new();
104
105    let app_state = AuthAppState {
106        config: auth_config.clone(),
107        user_store: user_store.clone(),
108    };
109
110    let admin_middleware_routes = Router::new()
111        .route("/middleware/admin", get(middleware_admin))
112        .layer(RequireRoles::any(vec!["admin"]));
113
114    let user_middleware_routes = Router::new()
115        .route("/middleware/user", get(middleware_user))
116        .layer(RequireRoles::any(vec!["user"]));
117
118    // Build routes
119    let protected_routes = Router::new()
120        .route("/protected", get(protected_route))
121        .route("/admin", get(admin_route))
122        .route("/public", get(public_route));
123
124    // Admin Registration Route
125    let auth_extras = Router::new()
126        .route("/auth/register-admin", post(register_admin))
127        .with_state(app_state);
128
129    println!("🔐 Auth API Example");
130    println!("==================");
131    println!();
132    println!("📝 Register a user:");
133    println!("   curl -X POST http://localhost:3000/auth/register -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"John Doe\\\"}}\"");
134    println!();
135    println!("🔑 Login:");
136    println!("   curl -X POST http://localhost:3000/auth/login -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\"}}\"");
137    println!();
138    println!("🔒 Access protected route:");
139    println!("   curl -X GET http://localhost:3000/protected -H \"Authorization: Bearer <access_token>\"");
140    println!();
141    println!("Scenario 1: Regular User");
142    println!("> Register a regular User:");
143    println!("  curl -X POST http://localhost:3000/auth/register -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"Basic Joe\\\"}}\"");
144    println!();
145    println!("> Test Admin Route (Should fail with code 403 Forbidden):");
146    println!("  curl -X GET http://localhost:3000/middleware/admin -H \"Authorization: Bearer <access_token>\"");
147    println!();
148    println!("Scenario 2: Admin User");
149    println!("> Register an ADMIN User:");
150    println!("  curl -X POST http://localhost:3000/auth/register-admin -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"admin@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"Admin Joe\\\"}}\"");
151    println!();
152    println!("> Test Admin Route (Should succed):");
153    println!("  curl -X GET http://localhost:3000/middleware/admin -H \"Authorization: Bearer <access_token>\"");
154
155    println!();
156
157    let api_routes = Router::new()
158        .merge(protected_routes)
159        .merge(admin_middleware_routes)
160        .merge(user_middleware_routes)
161        .merge(auth_extras)
162        .layer(from_fn_with_state(auth_config.clone(), inject_auth_config));
163
164    App::new()
165        .auto_configure()
166        .mount(auth_routes_with_store(auth_config, user_store))
167        .mount(api_routes)
168        .run()
169        .await
170        .unwrap();
171}
Source

pub fn route(self, path: &str, method_router: MethodRouter) -> Self

Add a route manually

Source

pub async fn run(self) -> Result<(), Box<dyn Error>>

Run the application

Examples found in repository?
examples/rest-api.rs (line 129)
121async fn main() {
122    // Create shared database
123    let db: Database = Arc::new(Mutex::new(HashMap::new()));
124
125    // Build and run the app
126    App::new()
127        .auto_configure()
128        .mount(user_routes().with_state(db))
129        .run()
130        .await
131        .unwrap();
132}
More examples
Hide additional examples
examples/auth-api.rs (line 168)
96async fn main() {
97    // Make sure AUTH_JWT_SECRET is set in environment
98    // For development, you can use the default, but set it in production!
99    let mut auth_config = AuthConfig::from_env();
100    auth_config.jwt_secret =
101        "rapid-rs-dev-secret-change-me-in-production-make-it-at-least-32-chars".to_string();
102
103    let user_store = InMemoryUserStore::new();
104
105    let app_state = AuthAppState {
106        config: auth_config.clone(),
107        user_store: user_store.clone(),
108    };
109
110    let admin_middleware_routes = Router::new()
111        .route("/middleware/admin", get(middleware_admin))
112        .layer(RequireRoles::any(vec!["admin"]));
113
114    let user_middleware_routes = Router::new()
115        .route("/middleware/user", get(middleware_user))
116        .layer(RequireRoles::any(vec!["user"]));
117
118    // Build routes
119    let protected_routes = Router::new()
120        .route("/protected", get(protected_route))
121        .route("/admin", get(admin_route))
122        .route("/public", get(public_route));
123
124    // Admin Registration Route
125    let auth_extras = Router::new()
126        .route("/auth/register-admin", post(register_admin))
127        .with_state(app_state);
128
129    println!("🔐 Auth API Example");
130    println!("==================");
131    println!();
132    println!("📝 Register a user:");
133    println!("   curl -X POST http://localhost:3000/auth/register -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"John Doe\\\"}}\"");
134    println!();
135    println!("🔑 Login:");
136    println!("   curl -X POST http://localhost:3000/auth/login -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\"}}\"");
137    println!();
138    println!("🔒 Access protected route:");
139    println!("   curl -X GET http://localhost:3000/protected -H \"Authorization: Bearer <access_token>\"");
140    println!();
141    println!("Scenario 1: Regular User");
142    println!("> Register a regular User:");
143    println!("  curl -X POST http://localhost:3000/auth/register -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"user@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"Basic Joe\\\"}}\"");
144    println!();
145    println!("> Test Admin Route (Should fail with code 403 Forbidden):");
146    println!("  curl -X GET http://localhost:3000/middleware/admin -H \"Authorization: Bearer <access_token>\"");
147    println!();
148    println!("Scenario 2: Admin User");
149    println!("> Register an ADMIN User:");
150    println!("  curl -X POST http://localhost:3000/auth/register-admin -H \"Content-Type: application/json\" -d \"{{\\\"email\\\":\\\"admin@example.com\\\",\\\"password\\\":\\\"SecurePass123\\\",\\\"name\\\":\\\"Admin Joe\\\"}}\"");
151    println!();
152    println!("> Test Admin Route (Should succed):");
153    println!("  curl -X GET http://localhost:3000/middleware/admin -H \"Authorization: Bearer <access_token>\"");
154
155    println!();
156
157    let api_routes = Router::new()
158        .merge(protected_routes)
159        .merge(admin_middleware_routes)
160        .merge(user_middleware_routes)
161        .merge(auth_extras)
162        .layer(from_fn_with_state(auth_config.clone(), inject_auth_config));
163
164    App::new()
165        .auto_configure()
166        .mount(auth_routes_with_store(auth_config, user_store))
167        .mount(api_routes)
168        .run()
169        .await
170        .unwrap();
171}

Trait Implementations§

Source§

impl Default for App

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for App

§

impl !RefUnwindSafe for App

§

impl Send for App

§

impl Sync for App

§

impl Unpin for App

§

impl !UnwindSafe for App

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,