surwiki/src/main.rs

96 lines
2.4 KiB
Rust
Raw Normal View History

#![recursion_limit="128"] // for diesel's infer_schema!
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;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate static_resource_derive;
2017-08-19 23:48:51 +03:00
2017-08-20 23:17:16 +03:00
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-09-08 17:21:24 +03:00
extern crate futures_cpupool;
2017-08-20 21:24:10 +03:00
extern crate hyper;
2017-10-02 00:24:16 +03:00
extern crate percent_encoding;
2017-09-01 17:34:24 +03:00
extern crate pulldown_cmark;
extern crate r2d2;
extern crate r2d2_diesel;
2017-09-05 18:07:57 +03:00
extern crate serde_json;
extern crate serde_urlencoded;
extern crate slug;
extern crate titlecase;
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
2017-09-15 18:28:23 +03:00
mod assets;
2017-08-19 23:48:51 +03:00
mod db;
mod mimes;
2017-08-20 23:17:16 +03:00
mod models;
mod rendering;
2017-10-13 16:21:23 +03:00
mod resources;
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-09-15 18:28:23 +03:00
mod wiki_lookup;
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);
let db_pool = db::create_pool(db_file)?;
2017-09-10 13:29:33 +03:00
let cpu_pool = futures_cpupool::CpuPool::new_num_cpus();
let state = state::State::new(db_pool, cpu_pool);
2017-09-15 18:28:23 +03:00
let lookup = wiki_lookup::WikiLookup::new(state);
2017-08-20 21:24:10 +03:00
let server =
hyper::server::Http::new()
.bind(
&SocketAddr::new(bind_host, bind_port),
2017-09-15 18:28:23 +03:00
move || Ok(site::Site::new(lookup.clone()))
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
}