surrealpatch/src/net/mod.rs

94 lines
2.1 KiB
Rust
Raw Normal View History

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 health;
2020-06-29 15:36:01 +00:00
mod import;
mod index;
2020-06-29 15:36:01 +00:00
mod key;
mod log;
mod output;
mod rpc;
mod session;
2020-06-29 15:36:01 +00:00
mod signin;
mod signup;
mod sql;
mod status;
mod sync;
mod version;
use crate::cli::CF;
2022-02-16 23:45:23 +00:00
use crate::err::Error;
use warp::Filter;
2021-03-29 15:43:37 +00:00
2022-07-19 08:28:24 +00:00
const LOG: &str = "surrealdb::net";
pub async fn init() -> Result<(), Error> {
// 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())
// Health endpoint
.or(health::config())
2020-06-29 15:36:01 +00:00
// 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())
// RPC query endpoint
.or(rpc::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
;
// Specify a generic version header
let net = net.with(head::version());
// Specify a generic server header
let net = net.with(head::server());
// Set cors headers on all requests
let net = net.with(head::cors());
// 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();
2022-07-19 08:28:24 +00:00
info!(target: LOG, "Starting web server on {}", &opt.bind);
if let (Some(c), Some(k)) = (&opt.crt, &opt.key) {
// Bind the server to the desired port
let (adr, srv) = warp::serve(net)
.tls()
.cert_path(c)
.key_path(k)
.bind_with_graceful_shutdown(opt.bind, async move {
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal");
});
// Log the server startup status
info!(target: LOG, "Started web server on {}", &adr);
// Run the server forever
srv.await
2022-05-10 07:22:04 +00:00
} else {
// Bind the server to the desired port
let (adr, srv) = warp::serve(net).bind_with_graceful_shutdown(opt.bind, async move {
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal");
});
// Log the server startup status
info!(target: LOG, "Started web server on {}", &adr);
// Run the server forever
srv.await
2022-05-10 07:22:04 +00:00
};
2020-06-29 15:36:01 +00:00
Ok(())
}