add generics example to actix-web

This commit is contained in:
Pavel Bakun 2023-07-04 19:33:12 +03:00
parent 0e8ed50e3a
commit f2e430505d
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
use actix_web::{web, App, Error, HttpResponse, HttpServer, Result};
async fn index<T: ApiProvider>(api: web::Data<T>) -> Result<HttpResponse, Error> {
let hi = api.dont_worry();
println!("{}", hi);
Ok(HttpResponse::Ok().body(hi))
}
pub trait ApiProvider {
fn dont_worry(&self) -> String;
}
pub struct Api {}
impl ApiProvider for Api {
fn dont_worry(&self) -> String {
"be happy".to_string()
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let api = web::Data::new(Api {});
HttpServer::new(move || {
App::new()
.app_data(api.clone())
.service(web::resource("/").route(web::get().to(index::<Api>)))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}