pub struct App { /* private fields */ }Expand description
Main application builder
Implementations§
Source§impl App
impl App
Sourcepub fn new() -> Self
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
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}Sourcepub fn auto_configure(self) -> Self
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
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}Sourcepub fn mount(self, router: Router) -> Self
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
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}Sourcepub fn route(self, path: &str, method_router: MethodRouter) -> Self
pub fn route(self, path: &str, method_router: MethodRouter) -> Self
Add a route manually
Sourcepub async fn run(self) -> Result<(), Box<dyn Error>>
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
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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