surwiki/src/main.rs

65 lines
1.5 KiB
Rust
Raw Normal View History

2017-08-19 23:48:51 +03:00
#[macro_use] extern crate diesel;
#[macro_use] extern crate diesel_codegen;
extern crate clap;
2017-08-20 21:24:10 +03:00
extern crate hyper;
2017-08-20 21:40:08 +03:00
use std::net::SocketAddr;
2017-08-19 23:48:51 +03:00
mod db;
mod schema;
2017-08-20 21:24:10 +03:00
mod site;
2017-08-19 23:48:51 +03:00
fn args<'a>() -> clap::ArgMatches<'a> {
use clap::{App, Arg};
App::new("sausagewiki")
.about("A wiki engine")
.arg(Arg::with_name("DATABASE")
.help("Sets the database file to use")
2017-08-20 21:24:10 +03:00
.required(true))
.arg(Arg::with_name("port")
.help("Sets the listening port")
.short("p")
.long("port")
2017-08-20 21:40:08 +03:00
.validator(|x| match x.parse::<u16>() {
Ok(_) => Ok(()),
Err(_) => Err("Must be an integer in the range [0, 65535]".to_owned())
})
2017-08-20 21:24:10 +03:00
.takes_value(true))
2017-08-19 23:48:51 +03:00
.get_matches()
}
2017-08-20 21:40:08 +03:00
fn core_main() -> Result<(), Box<std::error::Error>> {
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("Guaranteed by validator"))
.unwrap_or(8080);
let _db_connection = db::connect_database(db_file, true);
2017-08-20 21:24:10 +03:00
let server =
hyper::server::Http::new()
.bind(
&SocketAddr::new(bind_host, bind_port),
|| Ok(site::Site {})
)?;
server.run()?;
Ok(())
}
2017-08-19 23:48:51 +03:00
fn main() {
2017-08-20 21:40:08 +03:00
match core_main() {
Ok(()) => (),
Err(err) => {
eprintln!("{:#?}", err);
std::process::exit(1)
}
}
2017-08-19 23:48:51 +03:00
}