use serde::{Deserialize, Serialize};
use server_less::http;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
pub title: String,
pub score: f64,
}
#[derive(Clone)]
pub struct SearchService;
#[http]
impl SearchService {
pub fn search(&self, query: String, limit: Option<u32>) -> Vec<SearchResult> {
let limit = limit.unwrap_or(20);
vec![SearchResult {
title: format!("Result for '{}' (limit={})", query, limit),
score: 0.95,
}]
}
pub fn get_item(&self, item_id: u32) -> Option<String> {
if item_id == 42 {
Some("The answer".into())
} else {
None
}
}
pub fn create_item(&self, name: String, description: Option<String>) -> String {
format!("Created '{}': {}", name, description.unwrap_or_default())
}
}
#[tokio::main]
async fn main() {
let service = SearchService;
println!("OpenAPI spec:");
println!(
"{}",
serde_json::to_string_pretty(&SearchService::openapi_spec()).unwrap()
);
println!("\nStarting server on http://localhost:3000");
let app = service.http_router();
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}