Gotcha
Gotcha is an enhanced web framework based on Axum, providing additional features and conveniences for building web applications in Rust.
Features
- Built on top of Axum for high performance and reliability
- OpenAPI documentation generation (optional)
- Prometheus metrics integration (optional)
- CORS support (optional)
- Static file serving (optional)
- Task scheduling
- Configuration management
- Message system
- State management
Simple Example (New Builder API)
use gotcha::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Gotcha::new()
.get("/", || async { "Hello World" })
.get("/hello/:name", |Path(name): Path<String>| async move {
format!("Hello, {}!", name)
})
.listen("127.0.0.1:3000")
.await?;
Ok(())
}
Advanced Example (Traditional Trait API)
use gotcha::{async_trait, ConfigWrapper, GotchaApp, GotchaContext, GotchaRouter, Responder, State};
use serde::{Deserialize, Serialize};
pub async fn hello_world(_state: State<ConfigWrapper<Config>>) -> impl Responder {
"hello world"
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct Config {
pub name: String,
}
pub struct App {}
#[async_trait]
impl GotchaApp for App {
type State = ();
type Config = Config;
fn routes(&self, router: GotchaRouter<GotchaContext<Self::State, Self::Config>>) -> GotchaRouter<GotchaContext<Self::State, Self::Config>> {
router.get("/", hello_world)
}
async fn state(&self, _config: &ConfigWrapper<Self::Config>) -> Result<Self::State, Box<dyn std::error::Error>> {
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
App {}.run().await?;
Ok(())
}
Optional Features
openapi- Enables OpenAPI documentation generationprometheus- Enables Prometheus metricscors- Enables CORS supportstatic_files- Enables static file serving capabilities