surrealpatch/src/web/mod.rs

73 lines
1.3 KiB
Rust
Raw Normal View History

mod conf;
2020-06-29 15:36:01 +00:00
mod export;
2022-01-14 17:13:44 +00:00
mod fail;
2020-06-29 15:36:01 +00:00
mod head;
mod import;
mod key;
mod log;
mod root;
mod signin;
mod signup;
mod sql;
mod status;
mod sync;
mod version;
2021-03-29 15:43:37 +00:00
use anyhow::Error;
2020-06-29 15:36:01 +00:00
use std::net::SocketAddr;
2021-03-29 15:43:37 +00:00
use uuid::Uuid;
use warp::Filter;
2021-03-29 15:43:37 +00:00
const ID: &'static str = "Request-Id";
2020-06-29 15:36:01 +00:00
#[tokio::main]
pub async fn init(conf: &clap::ArgMatches) -> Result<(), Error> {
2020-06-29 15:36:01 +00:00
//
let adr = conf.value_of("bind").unwrap();
2020-06-29 15:36:01 +00:00
//
let adr: SocketAddr = adr.parse().expect("Unable to parse socket address");
//
2020-06-29 15:36:01 +00:00
let web = root::config()
// Version endpoint
.or(version::config())
// Status endpoint
.or(status::config())
// Signup endpoint
.or(signup::config())
// Signin endpoint
.or(signin::config())
// Export endpoint
.or(export::config())
// Import endpoint
.or(import::config())
// Backup endpoint
.or(sync::config())
// SQL query endpoint
2020-06-29 15:36:01 +00:00
.or(sql::config())
// API query endpoint
2020-06-29 15:36:01 +00:00
.or(key::config())
2022-01-14 17:13:44 +00:00
// Catch all errors
.recover(fail::recover)
2020-06-29 15:36:01 +00:00
// End routes setup
;
let web = web.with(warp::compression::gzip());
2021-03-29 15:43:37 +00:00
let web = web.with(head::server());
2020-06-29 15:36:01 +00:00
let web = web.with(head::version());
2021-03-29 15:43:37 +00:00
let web = web.map(|reply| {
let val = Uuid::new_v4().to_string();
warp::reply::with_header(reply, ID, val)
});
2020-06-29 15:36:01 +00:00
let web = web.with(log::write());
info!("Starting web server on {}", adr);
warp::serve(web).run(adr).await;
Ok(())
}