diff options
Diffstat (limited to 'src/server/mod.rs')
| -rw-r--r-- | src/server/mod.rs | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/src/server/mod.rs b/src/server/mod.rs index d7826f6..545fbeb 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,4 +1,3 @@ -use crate::server::response::Response; use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod, SslStream}; use std::collections::HashMap; use std::net::{SocketAddr, TcpListener, TcpStream}; @@ -13,8 +12,26 @@ pub mod response; #[derive(Clone)] pub struct ServerConfig { - defaultHost: Url, - wwwRoot: PathBuf, + default_host: Url, + www_root: PathBuf, + addrs: Vec<SocketAddr>, +} + +impl ServerConfig { + pub fn new(default_host: String, www_root: String) -> ServerConfig { + let mut url = Url::parse("gemini://default").unwrap(); + url.set_host(Some(default_host.as_str())); + + ServerConfig { + default_host: url, + www_root: PathBuf::from(www_root), + addrs: Vec::new(), + } + } + + pub fn add_addr(&mut self, addr: String) { + self.addrs.push(addr.parse().unwrap()); + } } pub struct Server { @@ -23,21 +40,15 @@ pub struct Server { } impl Server { - pub fn new(host: &str, wwwRoot: &Path) -> Server { - let config = ServerConfig { - defaultHost: Url::parse(host).unwrap(), - wwwRoot: PathBuf::from(wwwRoot), - }; - + pub fn new(config: &ServerConfig) -> Server { Server { acceptor: build_acceptor(), - config: config, + config: config.clone(), } } pub fn serve(&self) { - let addrs = [SocketAddr::from(([127, 0, 0, 1], 1965))]; - let listener = TcpListener::bind(&addrs[..]).unwrap(); + let listener = TcpListener::bind(&self.config.addrs[..]).unwrap(); for stream in listener.incoming() { match stream { @@ -62,15 +73,10 @@ fn handle_client(config: &ServerConfig, mut stream: SslStream<TcpStream>) { let location = match Url::parse(&request) { Ok(url) => url, - Err(e) => config.defaultHost.join(&request).unwrap(), - }; - - let response = match handler::handle(config, location) { - Some(response) => response, - None => response::internal_error(), + Err(e) => config.default_host.join(&request).unwrap(), }; - stream.ssl_write(&response.format()); + handler::handle(config, location, &mut stream); } fn build_acceptor() -> std::sync::Arc<SslAcceptor> { |
