2022-04-18 12:13:15 +00:00
|
|
|
pub mod auth;
|
2022-04-24 19:31:50 +00:00
|
|
|
pub mod fs;
|
2022-04-23 19:21:04 +00:00
|
|
|
pub mod middleware;
|
2022-04-23 18:44:02 +00:00
|
|
|
|
2022-04-18 12:13:15 +00:00
|
|
|
mod error;
|
|
|
|
|
2022-04-19 11:05:47 +00:00
|
|
|
use axum::{http::HeaderValue, routing::get, Extension, Router, Server};
|
2022-04-23 18:44:02 +00:00
|
|
|
use homedisk_database::Database;
|
2022-04-19 19:10:36 +00:00
|
|
|
use homedisk_types::config::types::Config;
|
2022-04-18 12:13:15 +00:00
|
|
|
use log::{debug, info};
|
2022-04-29 19:33:44 +00:00
|
|
|
use tower_http::cors::{AllowOrigin, CorsLayer};
|
2022-04-18 12:13:15 +00:00
|
|
|
|
|
|
|
async fn health_check() -> &'static str {
|
|
|
|
"I'm alive!"
|
|
|
|
}
|
|
|
|
|
2022-04-19 13:14:17 +00:00
|
|
|
pub async fn serve(
|
|
|
|
host: String,
|
|
|
|
origins: Vec<HeaderValue>,
|
|
|
|
db: Database,
|
|
|
|
config: Config,
|
|
|
|
) -> error::Result<()> {
|
2022-04-18 12:13:15 +00:00
|
|
|
debug!("starting http server");
|
|
|
|
info!("Website available at: http://{host}");
|
|
|
|
|
|
|
|
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())
|
2022-04-25 19:09:22 +00:00
|
|
|
.layer(CorsLayer::new().allow_origin(AllowOrigin::list(origins)))
|
2022-04-19 13:14:17 +00:00
|
|
|
.layer(Extension(db))
|
|
|
|
.layer(Extension(config));
|
2022-04-18 12:13:15 +00:00
|
|
|
|
|
|
|
Server::bind(&host.parse()?)
|
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|