altaria/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
pub extern crate async_trait;
pub extern crate paste;

mod parser;
pub mod request;
mod encoder;
pub mod response;
mod protocol;
pub mod router;
pub mod extractor;
mod middleware;

use crate::protocol::HttpProtocol;
use crate::router::{Router};
use std::net::{Ipv4Addr, SocketAddr};
use thiserror::Error;

type Result<T> = anyhow::Result<T>;

pub struct HttpServer {
    pub protocol: Box<dyn HttpProtocol>
}

impl HttpServer {
    pub fn http1(router: Router) -> HttpServer {
        HttpServer {
            protocol: Box::new(protocol::alpha::AlphaHttpProtocol::link_router(router))
        }
    }

    pub fn http2() -> HttpServer {
        HttpServer {
            protocol: Box::new(protocol::beta::BetaHttpProtocol::new())
        }
    }

    pub fn set_router(&mut self, router: Router) {
        self.protocol.set_router(router)
    }

    pub async fn bind(&mut self, addr: &str) -> Result<&mut Self> {
        self.protocol.connect(addr).await.map(|_| self)
    }

    pub async fn listen(self) -> Result<()> {
        let static_ref = Box::leak(self.protocol);
        static_ref.listen().await
    }
}

impl Default for HttpServer {
    fn default() -> Self {
        HttpServer {
            protocol: Box::new(protocol::alpha::AlphaHttpProtocol::new())
        }
    }
}

pub struct Server {
    address: Option<SocketAddr>,
    router: Option<Router>
}

#[derive(Debug, Error)]
pub enum ServerBuildError {
    #[error("Address must be defined")]
    UndefinedAddress
}

impl Server {
    pub fn builder() -> Self {
        Server {
            address: None,
            router: None
        }
    }

    pub fn local_port(self, port: u16) -> Self {
        let ip = Ipv4Addr::new(127, 0, 0, 1);
        let addr = SocketAddr::new(ip.into(), port);
        self.address(addr)
    }

    pub fn addr(mut self, addr: &str) -> Self {
        self.address = Some(addr.parse().expect("Invalid address"));
        self
    }

    pub fn address(mut self, address: SocketAddr) -> Self {
        self.address = Some(address);
        self
    }

    pub fn router(mut self, router: Router) -> Self {
        self.router = Some(router);
        self
    }

    pub async fn start(self) -> Result<()> {
        let mut server = HttpServer::default();
        if let Some(router) = self.router {
            server.set_router(router)
        }
        let addr = self.address.ok_or(ServerBuildError::UndefinedAddress)?;

        server
            .bind(&addr.to_string())
            .await?;

        server
            .listen()
            .await?;
        Ok(())
    }
}