surrealpatch/src/net/mod.rs

73 lines
1.6 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 output;
2020-06-29 15:36:01 +00:00
mod root;
mod signin;
mod signup;
mod sql;
mod status;
mod sync;
mod version;
2022-02-16 23:45:23 +00:00
use crate::err::Error;
use once_cell::sync::OnceCell;
2020-06-29 15:36:01 +00:00
use std::net::SocketAddr;
use std::sync::Arc;
use surrealdb::Datastore;
use warp::Filter;
2021-03-29 15:43:37 +00:00
static DB: OnceCell<Arc<Datastore>> = OnceCell::new();
2020-06-29 15:36:01 +00:00
#[tokio::main]
pub async fn init(bind: &str, path: &str) -> Result<(), Error> {
// Parse the desired binding socket address
let adr: SocketAddr = bind.parse().expect("Unable to parse socket address");
// Parse and setup desired datastore
let dbs = Datastore::new(path).await.expect("Unable to parse datastore path");
// Store database instance
let _ = DB.set(Arc::new(dbs));
// Setup web routes
let net = root::config()
2020-06-29 15:36:01 +00:00
// 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
;
// Enable response compression
let net = net.with(warp::compression::gzip());
// Specify a generic version header
let net = net.with(head::version());
// Specify a generic server header
let net = net.with(head::server());
// Log all requests to the console
let net = net.with(log::write());
2020-06-29 15:36:01 +00:00
info!("Starting web server on {}", adr);
warp::serve(net).run(adr).await;
2020-06-29 15:36:01 +00:00
Ok(())
}