surrealpatch/src/net/mod.rs

75 lines
1.5 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 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())
// 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);
info!(target: LOG, "Started 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(())
}