1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-28 13:20:37 +02:00

add ServerConfig param for server service

This commit is contained in:
Nikolay Kim
2019-03-08 19:43:13 -08:00
parent 4850cf41ff
commit 7db29544f9
11 changed files with 221 additions and 31 deletions

View File

@ -0,0 +1,18 @@
[package]
name = "actix-server-config"
version = "0.1.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix server config utils"
homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-net.git"
license = "MIT/Apache-2.0"
edition = "2018"
workspace = ".."
[lib]
name = "actix_server_config"
path = "src/lib.rs"
[dependencies]
actix-service = { path="../actix-service" }
futures = "0.1.25"

View File

@ -0,0 +1,88 @@
use std::cell::Cell;
use std::marker::PhantomData;
use std::net::SocketAddr;
use std::rc::Rc;
use actix_service::{FnService, IntoService, NewService};
use futures::future::{ok, FutureResult, IntoFuture};
#[derive(Debug, Clone)]
pub struct ServerConfig {
addr: SocketAddr,
secure: Rc<Cell<bool>>,
}
impl ServerConfig {
pub fn new(addr: SocketAddr) -> Self {
ServerConfig {
addr,
secure: Rc::new(Cell::new(false)),
}
}
/// Returns the address of the local half of this TCP server socket
pub fn local_addr(&self) -> SocketAddr {
self.addr
}
/// Returns true if connection is secure (tls enabled)
pub fn secure(&self) -> bool {
self.secure.as_ref().get()
}
/// Set secure flag
pub fn set_secure(&self) {
self.secure.as_ref().set(true)
}
}
pub fn server_fn<F, U, Req, Out>(f: F) -> impl NewService<Req, ServerConfig>
where
F: Fn(&ServerConfig) -> U + Clone + 'static,
U: FnMut(Req) -> Out + Clone + 'static,
Out: IntoFuture,
{
ServerFnNewService { f, _t: PhantomData }
}
struct ServerFnNewService<F, U, Req, Out>
where
F: Fn(&ServerConfig) -> U + Clone + 'static,
U: FnMut(Req) -> Out + Clone + 'static,
Out: IntoFuture,
{
f: F,
_t: PhantomData<(U, Req, Out)>,
}
impl<F, U, Req, Out> NewService<Req, ServerConfig> for ServerFnNewService<F, U, Req, Out>
where
F: Fn(&ServerConfig) -> U + Clone + 'static,
U: FnMut(Req) -> Out + Clone + 'static,
Out: IntoFuture,
{
type Response = Out::Item;
type Error = Out::Error;
type Service = FnService<U, Req, Out>;
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
ok((self.f)(cfg).into_service())
}
}
impl<F, U, Req, Out> Clone for ServerFnNewService<F, U, Req, Out>
where
F: Fn(&ServerConfig) -> U + Clone + 'static,
U: FnMut(Req) -> Out + Clone,
Out: IntoFuture,
{
fn clone(&self) -> Self {
Self {
f: self.f.clone(),
_t: PhantomData,
}
}
}