use axum::{
Router,
routing::{get, post},
Extension,
Json,
};
use wacht::{
init_from_env,
middleware::{AuthLayer, AuthContext},
};
use serde_json::{json, Value};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize SDK
init_from_env().await?;
// Public routes (no auth required)
let public_routes = Router::new()
.route("/health", get(health_check))
.route("/login", post(login));
// Protected routes (auth required)
let protected_routes = Router::new()
.route("/profile", get(get_profile))
.route("/organizations", get(list_organizations))
.layer(AuthLayer::new());
// Combine routers
let app = Router::new()
.merge(public_routes)
.merge(protected_routes);
// Start server
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
println!("Server running on http://0.0.0.0:3000");
axum::serve(listener, app).await?;
Ok(())
}
// Public endpoint
async fn health_check() -> &'static str {
"OK"
}
// Public endpoint
async fn login() -> Json<Value> {
// Your login logic here
Json(json!({
"token": "jwt-token-here"
}))
}
// Protected endpoint
async fn get_profile(
Extension(auth): Extension<AuthContext>
) -> Json<Value> {
Json(json!({
"user_id": auth.user_id,
"session_id": auth.session_id
}))
}
// Protected endpoint
async fn list_organizations(
Extension(auth): Extension<AuthContext>
) -> Json<Value> {
let orgs = auth.organization_permissions
.unwrap_or_default();
Json(json!({
"user_id": auth.user_id,
"organizations": orgs
}))
}