HomeDisk/server/src/lib.rs

42 lines
1.0 KiB
Rust
Raw Normal View History

pub mod auth;
2022-04-24 19:31:50 +00:00
pub mod fs;
pub mod middleware;
mod error;
use axum::{http::HeaderValue, routing::get, Extension, Router, Server};
use homedisk_database::Database;
2022-04-19 19:10:36 +00:00
use homedisk_types::config::types::Config;
use log::{debug, info};
2022-04-29 19:33:44 +00:00
use tower_http::cors::{AllowOrigin, CorsLayer};
async fn health_check() -> &'static str {
"I'm alive!"
}
2022-06-07 20:36:26 +00:00
pub async fn run_http_server(
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("/auth", auth::app())
2022-04-24 19:31:50 +00:00
.nest("/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(())
}