From bc76506ebc495a72839772af0832fa12a7fa0666 Mon Sep 17 00:00:00 2001 From: Johannes Hoff Date: Sun, 20 Aug 2017 20:24:10 +0200 Subject: [PATCH] Add a web server --- src/main.rs | 29 +++++++++++++++++++++++++++-- src/site.rs | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/site.rs diff --git a/src/main.rs b/src/main.rs index e8abe3a..19b5454 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,13 @@ #[macro_use] extern crate diesel_codegen; extern crate clap; +extern crate hyper; + +use std::net::{SocketAddr, IpAddr}; mod db; mod schema; +mod site; fn args<'a>() -> clap::ArgMatches<'a> { use clap::{App, Arg}; @@ -13,15 +17,36 @@ fn args<'a>() -> clap::ArgMatches<'a> { .about("A wiki engine") .arg(Arg::with_name("DATABASE") .help("Sets the database file to use") - .required(true) - .index(1)) + .required(true)) + .arg(Arg::with_name("port") + .help("Sets the listening port") + .short("p") + .long("port") + .takes_value(true)) .get_matches() } +fn start_server(bind_host: IpAddr, bind_port: u16) -> Result<(), Box> { + let server = + hyper::server::Http::new() + .bind( + &SocketAddr::new(bind_host, bind_port), + || Ok(site::Site {}) + )?; + + server.run()?; + + Ok(()) +} + fn main() { let args = args(); let db_file = args.value_of("DATABASE").expect("Guaranteed by clap"); + let bind_host = "127.0.0.1".parse().unwrap(); + let bind_port = args.value_of("port").map(|p| p.parse().expect("Port must be an unsigned integer")).unwrap_or(8080); let _db_connection = db::connect_database(db_file, true); + + start_server(bind_host, bind_port).unwrap(); } diff --git a/src/site.rs b/src/site.rs new file mode 100644 index 0000000..005a3d9 --- /dev/null +++ b/src/site.rs @@ -0,0 +1,18 @@ +extern crate futures; + +use hyper; +use hyper::server::*; + +pub struct Site { +} + +impl Service for Site { + type Request = Request; + type Response = Response; + type Error = hyper::Error; + type Future = futures::BoxFuture; + + fn call(&self, _req: Request) -> Self::Future { + unimplemented!() + } +}