2022-06-21 10:51:22 +00:00
|
|
|
#![doc(html_root_url = "https://homedisk-doc.medzik.xyz")]
|
|
|
|
|
2022-06-08 19:16:12 +00:00
|
|
|
mod auth;
|
2022-04-18 12:13:15 +00:00
|
|
|
mod error;
|
2022-06-08 19:16:12 +00:00
|
|
|
mod fs;
|
|
|
|
mod middleware;
|
2022-04-18 12:13:15 +00:00
|
|
|
|
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-06-08 19:16:12 +00:00
|
|
|
use homedisk_types::config::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
|
|
|
|
2022-06-08 17:08:06 +00:00
|
|
|
/// Handle `/health-check` requests
|
2022-04-18 12:13:15 +00:00
|
|
|
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");
|
2022-04-18 12:13:15 +00:00
|
|
|
info!("Website available at: http://{host}");
|
|
|
|
|
2022-06-07 20:36:26 +00:00
|
|
|
// create http Router
|
2022-04-18 12:13:15 +00:00
|
|
|
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
|
|
|
|
2022-06-07 20:36:26 +00:00
|
|
|
// bind the provided address and serve Router
|
2022-04-18 12:13:15 +00:00
|
|
|
Server::bind(&host.parse()?)
|
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|