rust_saas_boilerplate/lib.rs
1//! Rust SaaS Boilerplate
2//!
3//! A production-grade Rust SaaS boilerplate - A complete starter template for building
4//! SaaS applications. Can be used as a library dependency or run as a standalone server.
5//!
6//! This boilerplate provides:
7//! - Complete authentication system
8//! - User management
9//! - Modular architecture
10//! - Error handling
11//! - Environment-based configuration
12//! - And much more!
13//!
14//! # Usage as Library
15//!
16//! Add to your `Cargo.toml`:
17//!
18//! ```toml
19//! [dependencies]
20//! rust-saas-boilerplate = { path = "../rust-saas-boilerplate" }
21//! # Or from git:
22//! # rust-saas-boilerplate = { git = "https://github.com/HardikKSavaliya/rust-saas-backend.git" }
23//! ```
24//!
25//! Then use it in your code:
26//!
27//! ```rust,no_run
28//! use rust_saas_boilerplate::{create_app, AppConfig};
29//!
30//! let app = create_app();
31//! // Use in your Axum router
32//! ```
33//!
34//! # Usage as Binary
35//!
36//! ```bash
37//! cargo run
38//! ```
39
40pub mod app;
41pub mod config;
42pub mod error;
43pub mod modules;
44
45// Re-export commonly used types
46pub use app::rust_saas;
47pub use config::AppConfig;
48pub use error::{AppError, AppResult};
49
50/// Initialize logging based on environment
51/// - Production: Only ERROR level logs
52/// - Development: All logs (INFO, DEBUG, etc.)
53/// - Can be overridden by RUST_LOG environment variable
54///
55/// # Example
56///
57/// ```rust,no_run
58/// use rust_saas_boilerplate::{init_logging, AppConfig};
59///
60/// let config = AppConfig::from_env();
61/// init_logging(&config);
62/// ```
63pub fn init_logging(config: &AppConfig) {
64 use tracing_subscriber::{fmt, EnvFilter};
65
66 let filter = if config.is_production() {
67 // Production: Only show ERROR level and above
68 EnvFilter::new("error")
69 } else {
70 // Development: Show all logs (can be overridden by RUST_LOG env var)
71 EnvFilter::try_from_default_env()
72 .unwrap_or_else(|_| EnvFilter::new("info"))
73 };
74
75 fmt()
76 .with_target(false)
77 .with_env_filter(filter)
78 .init();
79}
80
81/// Create the application router
82/// This is the main entry point for using this library
83///
84/// # Example
85///
86/// ```rust,no_run
87/// use rust_saas_boilerplate::create_app;
88/// use axum::Router;
89///
90/// let app: Router = create_app();
91/// // Merge with your own routes or use as-is
92/// ```
93pub fn create_app() -> axum::Router {
94 app::rust_saas()
95}