surrealpatch/src/net/mod.rs

80 lines
1.7 KiB
Rust
Raw Normal View History

mod conf;
mod config;
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 index;
2020-06-29 15:36:01 +00:00
mod key;
mod log;
mod output;
2020-06-29 15:36:01 +00:00
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 config::Config;
use once_cell::sync::OnceCell;
use surrealdb::Datastore;
use warp::Filter;
2021-03-29 15:43:37 +00:00
static DB: OnceCell<Datastore> = OnceCell::new();
static CF: OnceCell<Config> = OnceCell::new();
2020-06-29 15:36:01 +00:00
#[tokio::main]
pub async fn init(matches: &clap::ArgMatches) -> Result<(), Error> {
// Parse the server config options
let cfg = config::parse(matches);
// Parse and setup the desired kv datastore
let dbs = Datastore::new(&cfg.path).await?;
// Store database instance
let _ = DB.set(dbs);
// Store config options
let _ = CF.set(cfg);
// Setup web routes
let net = index::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
// Get local copy of options
let opt = CF.get().unwrap();
info!("Starting web server on {}", &opt.bind);
2020-06-29 15:36:01 +00:00
warp::serve(net).run(opt.bind).await;
2020-06-29 15:36:01 +00:00
Ok(())
}