HomeDisk/server/src/lib.rs

45 lines
1.1 KiB
Rust
Raw Normal View History

#![doc(html_root_url = "https://homedisk-doc.medzik.xyz")]
2022-06-08 19:16:12 +00:00
mod auth;
mod error;
2022-06-08 19:16:12 +00:00
mod fs;
mod middleware;
use axum::{http::HeaderValue, routing::get, Extension, Router, Server};
use homedisk_database::Database;
2022-06-08 19:16:12 +00:00
use homedisk_types::config::Config;
use log::{debug, info};
2022-04-29 19:33:44 +00:00
use tower_http::cors::{AllowOrigin, CorsLayer};
2022-06-08 17:08:06 +00:00
/// Handle `/health-check` requests
async fn health_check() -> &'static str {
"I'm alive!"
}
2022-06-08 19:16:12 +00:00
/// Start HTTP server
pub async fn serve_http(
2022-04-19 13:14:17 +00:00
host: String,
origins: Vec<HeaderValue>,
db: Database,
config: Config,
) -> error::Result<()> {
2022-06-07 20:36:26 +00:00
debug!("Starting http server");
info!("Website available at: http://{host}");
2022-06-07 20:36:26 +00:00
// create http Router
let app = Router::new()
.route("/health-check", get(health_check))
.nest("/api/auth", auth::app())
.nest("/api/fs", fs::app())
.layer(CorsLayer::new().allow_origin(AllowOrigin::list(origins)))
2022-04-19 13:14:17 +00:00
.layer(Extension(db))
.layer(Extension(config));
2022-06-07 20:36:26 +00:00
// bind the provided address and serve Router
Server::bind(&host.parse()?)
.serve(app.into_make_service())
.await?;
Ok(())
}