I'm trying to write a simple wrapper around all the parameters that are needed to create actix
Routes.
I want to hide all of the initialization logic of the library I'm currently writing inside the init
function.
In order to avoid users of my library having to include actix-web
and actix-rt
as a dependency, the init
function should take in my own version of Route
and then spin up the system and its arbiters.
I wrote this fairly simple piece of code to show how I want to use the wrappers:
Code
use actix_web::{HttpServer, Responder, FromRequest, web, App};
use std::future::Future;
use actix_web::dev::Factory;
pub enum Method {
Delete,
Get,
Patch,
Post,
Put,
}
pub struct Route<F, T, R, U>
where
F: Factory<T, R, U>,
T: FromRequest + 'static,
R: Future<Output = U> + 'static,
U: Responder + 'static,
{
method: Method,
path: &'static str,
handler: F,
}
pub async fn init<F, T, R, U> (_args: Vec<String>, routes: Vec<Route<F, T, R, U>>)
where
F: Factory<T, R, U>,
T: FromRequest + 'static,
R: Future<Output = U> + 'static,
U: Responder + 'static,
{
// do a lot of initialization here
let host = "127.0.0.1"; // this comes from configuration
let port = 3000; // this comes from configuration
// initialize server and bind all middleware, data and routes
HttpServer::new(move || {
let mut app = App::new();
// add a more states, data and middleware here
for route in routes {
match route.method {
Method::Get => app = app.route(route.path, web::get().to(route.handler)),
Method::Delete => app = app.route(route.path, web::delete().to(route.handler)),
Method::Patch => app = app.route(route.path, web::patch().to(route.handler)),
Method::Post => app = app.route(route.path, web::post().to(route.handler)),
Method::Put => app = app.route(route.path, web::put().to(route.handler)),
}
}
app
})
.bind(format!("{}:{}", host, port))?
.run()
.await
.map_err(|_| {
std::io::Error::new(std::io::ErrorKind::Other, "Failed to wrap up server")
})?;
// wrap up
}
Here is my
Cargo.toml
[package]
name = "route_wrapper"
version = "0.1.0"
edition = "2018"
[dependencies]
actix-web = "2.0.0"
When compiling I see:
error[E0392]: parameter `T` is never used
--> src/lib.rs:13:21
|
13 | pub struct Route<F, T, R, U>
| ^ unused parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `std::marker::PhantomData`
error[E0392]: parameter `R` is never used
--> src/lib.rs:13:24
|
13 | pub struct Route<F, T, R, U>
| ^ unused parameter
|
= help: consider removing `R`, referring to it in a field, or using a marker such as `std::marker::PhantomData`
error[E0392]: parameter `U` is never used
--> src/lib.rs:13:27
|
13 | pub struct Route<F, T, R, U>
| ^ unused parameter
|
= help: consider removing `U`, referring to it in a field, or using a marker such as `std::marker::PhantomData`
error: aborting due to 3 previous errors
but I don't understand why.
I'm using T
, R
, and U
to define F
, so why is this compiler error shown?
In the end I would like to be able to just use the library as a dependency and then do something like:
Example
async fn get() -> HttpResponse {
HttpResponse::Ok().body("hello world from get")
}
async fn post() -> HttpResponse {
HttpResponse::Ok().body("hello world from post")
}
async fn example() {
let routes = vec![
Route{
method: Method::Get,
path: "/path/to/get",
handler: get
},
Route{
method: Method::Post,
path: "/path/to/post",
handler: post
},
];
route_wrapper::init(vec![], routes).await;
}
I really can't wrap my head around this.
Thanks in advance for taking your time to clear thing up for me.