Simplify server setup configuration options

This commit is contained in:
Tobie Morgan Hitchcock 2022-02-13 23:33:52 +00:00
parent fbd924fbac
commit ae70844524
4 changed files with 20 additions and 24 deletions

View file

@ -1,10 +1,5 @@
extern crate chrono; use fern::colors::Color;
extern crate fern; use fern::colors::ColoredLevelConfig;
extern crate log;
extern crate std;
use self::fern::colors::Color;
use self::fern::colors::ColoredLevelConfig;
pub fn init(verbosity: usize) { pub fn init(verbosity: usize) {
let levels = ColoredLevelConfig::new() let levels = ColoredLevelConfig::new()

View file

@ -16,11 +16,16 @@ Y88b d88P Y88b 888 888 888 Y8b. 888 888 888 888 .d88P 888 d88P
"; ";
pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> { pub fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
// output SurrealDB logo
println!("{}", LOGO); println!("{}", LOGO);
// Parse the database endpoint path
let path = matches.value_of("path").unwrap();
// Parse the server binding address
let bind = matches.value_of("bind").unwrap();
// Start up the kvs storage // Start up the kvs storage
kvs::init(matches)?; kvs::init(path)?;
// Start up the web server // Start up the web server
net::init(matches)?; net::init(bind)?;
// Don't error when done // Don't error when done
Ok(()) Ok(())
} }

View file

@ -24,9 +24,7 @@ pub enum Transaction<'a> {
static DB: OnceCell<Datastore> = OnceCell::new(); static DB: OnceCell<Datastore> = OnceCell::new();
pub fn init(conf: &clap::ArgMatches) -> Result<(), Error> { pub fn init(path: &str) -> Result<(), Error> {
// Parse the database endpoint path
let path = conf.value_of("path").unwrap();
// Instantiate the database endpoint // Instantiate the database endpoint
match path { match path {
"memory" => { "memory" => {

View file

@ -22,13 +22,11 @@ use warp::Filter;
const ID: &'static str = "Request-Id"; const ID: &'static str = "Request-Id";
#[tokio::main] #[tokio::main]
pub async fn init(conf: &clap::ArgMatches) -> Result<(), Error> { pub async fn init(bind: &str) -> Result<(), Error> {
// //
let adr = conf.value_of("bind").unwrap(); let adr: SocketAddr = bind.parse().expect("Unable to parse socket address");
//
let adr: SocketAddr = adr.parse().expect("Unable to parse socket address"); let net = root::config()
//
let web = root::config()
// Version endpoint // Version endpoint
.or(version::config()) .or(version::config())
// Status endpoint // Status endpoint
@ -52,22 +50,22 @@ pub async fn init(conf: &clap::ArgMatches) -> Result<(), Error> {
// End routes setup // End routes setup
; ;
let web = web.with(warp::compression::gzip()); let net = net.with(warp::compression::gzip());
let web = web.with(head::server()); let net = net.with(head::server());
let web = web.with(head::version()); let net = net.with(head::version());
let web = web.map(|reply| { let net = net.map(|reply| {
let val = Uuid::new_v4().to_string(); let val = Uuid::new_v4().to_string();
warp::reply::with_header(reply, ID, val) warp::reply::with_header(reply, ID, val)
}); });
let web = web.with(log::write()); let net = net.with(log::write());
info!("Starting web server on {}", adr); info!("Starting web server on {}", adr);
warp::serve(web).run(adr).await; warp::serve(net).run(adr).await;
Ok(()) Ok(())
} }