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;
extern crate fern;
extern crate log;
extern crate std;
use self::fern::colors::Color;
use self::fern::colors::ColoredLevelConfig;
use fern::colors::Color;
use fern::colors::ColoredLevelConfig;
pub fn init(verbosity: usize) {
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> {
// output SurrealDB 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
kvs::init(matches)?;
kvs::init(path)?;
// Start up the web server
net::init(matches)?;
net::init(bind)?;
// Don't error when done
Ok(())
}

View file

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

View file

@ -22,13 +22,11 @@ use warp::Filter;
const ID: &'static str = "Request-Id";
#[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 = adr.parse().expect("Unable to parse socket address");
//
let web = root::config()
let adr: SocketAddr = bind.parse().expect("Unable to parse socket address");
let net = root::config()
// Version endpoint
.or(version::config())
// Status endpoint
@ -52,22 +50,22 @@ pub async fn init(conf: &clap::ArgMatches) -> Result<(), Error> {
// 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();
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);
warp::serve(web).run(adr).await;
warp::serve(net).run(adr).await;
Ok(())
}