surwiki/src/main.rs

70 lines
2.2 KiB
Rust
Raw Normal View History

#[macro_use] extern crate lazy_static;
2017-08-19 23:48:51 +03:00
extern crate clap;
extern crate sausagewiki;
2017-08-20 21:24:10 +03:00
use std::net::IpAddr;
2017-08-19 23:48:51 +03:00
mod build_config;
use build_config::*;
const DATABASE: &str = "DATABASE";
const TRUST_IDENTITY: &str = "trust-identity";
2017-10-30 16:59:39 +03:00
const ADDRESS: &str = "address";
const PORT: &str = "port";
2017-08-19 23:48:51 +03:00
fn args<'a>() -> clap::ArgMatches<'a> {
use clap::{App, Arg};
App::new(PROJECT_NAME)
.version(VERSION.as_str())
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(Arg::with_name(DATABASE)
2017-08-19 23:48:51 +03:00
.help("Sets the database file to use")
2017-08-20 21:24:10 +03:00
.required(true))
.arg(Arg::with_name(PORT)
2017-11-01 13:51:58 +03:00
.help("Sets the listening port")
2017-08-20 21:24:10 +03:00
.short("p")
.long(PORT)
2017-11-01 13:51:58 +03:00
.default_value("8080")
2017-08-20 21:40:08 +03:00
.validator(|x| match x.parse::<u16>() {
Ok(_) => Ok(()),
2017-10-30 16:59:39 +03:00
Err(_) => Err("Must be an integer in the range [0, 65535]".into())
})
.takes_value(true))
.arg(Arg::with_name(ADDRESS)
2017-11-01 13:51:58 +03:00
.help("Sets the IP address to bind to")
2017-10-30 16:59:39 +03:00
.short("a")
.long(ADDRESS)
2017-11-01 13:51:58 +03:00
.default_value("127.0.0.1")
2017-10-30 16:59:39 +03:00
.validator(|x| match x.parse::<IpAddr>() {
Ok(_) => Ok(()),
Err(_) => Err("Must be a valid IP address".into())
2017-08-20 21:40:08 +03:00
})
2017-08-20 21:24:10 +03:00
.takes_value(true))
.arg(Arg::with_name(TRUST_IDENTITY)
.help("Trust the value in the X-Identity header to be an \
authenticated username. This only makes sense when Sausagewiki \
runs behind a reverse proxy which sets this header.")
.long(TRUST_IDENTITY))
2017-08-19 23:48:51 +03:00
.get_matches()
}
fn main() -> Result<(), Box<std::error::Error>> {
2017-08-20 21:40:08 +03:00
let args = args();
2017-11-01 13:51:58 +03:00
const CLAP: &str = "Guaranteed by clap";
const VALIDATOR: &str = "Guaranteed by clap validator";
let db_file = args.value_of(DATABASE).expect(CLAP).to_owned();
let bind_host = args.value_of(ADDRESS).expect(CLAP).parse().expect(VALIDATOR);
let bind_port = args.value_of(PORT).expect(CLAP).parse().expect(VALIDATOR);
2017-08-20 21:40:08 +03:00
let trust_identity = args.is_present(TRUST_IDENTITY);
sausagewiki::main(
db_file,
bind_host,
bind_port,
trust_identity,
)
2017-08-20 21:24:10 +03:00
}