surwiki/src/main.rs

77 lines
1.9 KiB
Rust
Raw Normal View History

2017-08-20 23:17:16 +03:00
#[macro_use] extern crate bart_derive;
2017-08-19 23:48:51 +03:00
#[macro_use] extern crate diesel;
#[macro_use] extern crate diesel_codegen;
2017-08-20 21:46:08 +03:00
#[macro_use] extern crate lazy_static;
2017-08-19 23:48:51 +03:00
2017-08-20 23:17:16 +03:00
extern crate bart;
extern crate chrono;
2017-08-19 23:48:51 +03:00
extern crate clap;
2017-08-20 21:46:08 +03:00
extern crate futures;
2017-08-20 21:24:10 +03:00
extern crate hyper;
2017-09-01 17:34:24 +03:00
extern crate pulldown_cmark;
2017-08-20 21:24:10 +03:00
2017-08-20 21:40:08 +03:00
use std::net::SocketAddr;
2017-08-19 23:48:51 +03:00
mod db;
2017-08-20 23:17:16 +03:00
mod models;
2017-08-19 23:48:51 +03:00
mod schema;
2017-08-20 21:24:10 +03:00
mod site;
mod state;
mod web;
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").to_owned();
2017-08-20 21:40:08 +03:00
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);
// Connect to the database and run migrations up front:
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),
move || Ok(site::Site::new(state::State::new(db::connect_database(&db_file, false))))
2017-08-20 21:24:10 +03:00
)?;
2017-08-22 17:40:18 +03:00
println!("Listening on http://{}", server.local_addr().unwrap());
2017-08-20 21:24:10 +03:00
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
}