use std::sync::{Arc, Mutex};
use actix_web::{web };
use crate::rubase::{BaseEntity};
use actix_web::{ Responder};
use std::future::Future;
use crate::ruweb::webrouter::ctl::ctl::{manual_hello, home};
pub struct RouteBuilder {
configs: Arc<Mutex<Vec<Box<dyn Fn(&mut web::ServiceConfig) + Send + Sync>>>>,
}
impl BaseEntity for RouteBuilder {}
impl RouteBuilder {
pub fn new() -> Self {
RouteBuilder { configs: Arc::new(Mutex::new(Vec::new())) }
}
pub fn add<F>(&mut self, f: F) -> &mut Self
where
F: Fn(&mut web::ServiceConfig) + Send + Sync + 'static,
{
self.configs.lock().unwrap().push(Box::new(f));
self
}
pub fn apply(&self, cfg: &mut web::ServiceConfig) {
for f in self.configs.lock().unwrap().iter() {
f(cfg);
}
}
}
pub fn route_get<F, Fut, R>(cfg: &mut web::ServiceConfig, path: &str, handler: F)
where
F: Fn() -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = R> + Send + 'static,
R: Responder + 'static,
{
cfg.route(path, web::get().to(handler));
}
pub fn route_post<F, Fut, R>(cfg: &mut web::ServiceConfig, path: &str, handler: F)
where
F: Fn() -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = R> + Send + 'static,
R: Responder + 'static,
{
cfg.route(path, web::post().to(handler));
}
pub fn route_put<F, Fut, R>(cfg: &mut web::ServiceConfig, path: &str, handler: F)
where
F: Fn() -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = R> + Send + 'static,
R: Responder + 'static,
{
cfg.route(path, web::put().to(handler));
}
pub fn route_delete<F, Fut, R>(cfg: &mut web::ServiceConfig, path: &str, handler: F)
where
F: Fn() -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = R> + Send + 'static,
R: Responder + 'static,
{
cfg.route(path, web::delete().to(handler));
}
pub fn config_routes(cfg: &mut web::ServiceConfig) {
route_get(cfg, "/hey", manual_hello);
route_get(cfg, "/hey1", home);
}