2022-05-10 07:17:47 +00:00
|
|
|
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;
|
2022-05-10 07:17:47 +00:00
|
|
|
mod index;
|
2022-06-20 11:29:06 +00:00
|
|
|
mod jwt;
|
2020-06-29 15:36:01 +00:00
|
|
|
mod key;
|
|
|
|
mod log;
|
2022-02-05 23:06:16 +00:00
|
|
|
mod output;
|
2022-05-10 07:29:25 +00:00
|
|
|
mod session;
|
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;
|
2022-05-10 07:17:47 +00:00
|
|
|
use config::Config;
|
2022-02-17 07:39:40 +00:00
|
|
|
use once_cell::sync::OnceCell;
|
2022-02-22 14:16:50 +00:00
|
|
|
use surrealdb::Datastore;
|
2022-01-13 17:36:41 +00:00
|
|
|
use warp::Filter;
|
2021-03-29 15:43:37 +00:00
|
|
|
|
2022-05-10 07:17:47 +00:00
|
|
|
static DB: OnceCell<Datastore> = OnceCell::new();
|
|
|
|
|
|
|
|
static CF: OnceCell<Config> = OnceCell::new();
|
2022-02-17 07:39:40 +00:00
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[tokio::main]
|
2022-05-10 07:17:47 +00:00
|
|
|
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?;
|
2022-02-17 07:39:40 +00:00
|
|
|
// Store database instance
|
2022-05-10 07:17:47 +00:00
|
|
|
let _ = DB.set(dbs);
|
|
|
|
// Store config options
|
|
|
|
let _ = CF.set(cfg);
|
2022-02-17 07:39:40 +00:00
|
|
|
// Setup web routes
|
2022-05-10 07:17:47 +00:00
|
|
|
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())
|
2022-01-13 17:36:41 +00:00
|
|
|
// SQL query endpoint
|
2020-06-29 15:36:01 +00:00
|
|
|
.or(sql::config())
|
2022-01-13 17:36:41 +00:00
|
|
|
// 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
|
|
|
|
;
|
2022-02-17 07:39:40 +00:00
|
|
|
// Enable response compression
|
2022-02-13 23:33:52 +00:00
|
|
|
let net = net.with(warp::compression::gzip());
|
2022-02-17 07:39:40 +00:00
|
|
|
// Specify a generic version header
|
2022-02-13 23:33:52 +00:00
|
|
|
let net = net.with(head::version());
|
2022-02-17 07:39:40 +00:00
|
|
|
// Specify a generic server header
|
|
|
|
let net = net.with(head::server());
|
|
|
|
// Log all requests to the console
|
2022-02-13 23:33:52 +00:00
|
|
|
let net = net.with(log::write());
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-05-10 07:17:47 +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
|
|
|
|
2022-05-10 07:22:04 +00:00
|
|
|
if let (Some(crt), Some(key)) = (&opt.crt, &opt.key) {
|
|
|
|
warp::serve(net).tls().cert_path(crt).key_path(key).run(opt.bind).await
|
|
|
|
} else {
|
|
|
|
warp::serve(net).run(opt.bind).await
|
|
|
|
};
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|