rieter/server/src/main.rs

66 lines
1.6 KiB
Rust
Raw Normal View History

mod error;
2023-07-11 13:41:56 +02:00
mod repo;
pub use error::{Result, ServerError};
use axum::extract::FromRef;
2023-07-11 13:41:56 +02:00
use axum::Router;
use repo::RepoGroupManager;
2023-07-11 13:41:56 +02:00
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
2023-07-15 20:07:19 +02:00
use tower_http::trace::TraceLayer;
2023-07-16 19:00:49 +02:00
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
2023-07-11 13:41:56 +02:00
#[derive(Clone)]
pub struct Config {
data_dir: PathBuf,
repo_dir: PathBuf,
pkg_dir: PathBuf,
}
#[derive(Clone)]
pub struct Global {
config: Config,
repo_manager: Arc<RwLock<RepoGroupManager>>,
}
impl FromRef<Global> for Arc<RwLock<RepoGroupManager>> {
fn from_ref(global: &Global) -> Self {
Arc::clone(&global.repo_manager)
}
}
2023-07-11 13:41:56 +02:00
#[tokio::main]
async fn main() {
2023-07-16 19:00:49 +02:00
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG").unwrap_or_else(|_| "tower_http=debug,rieterd=debug".into()),
2023-07-16 19:00:49 +02:00
))
.with(tracing_subscriber::fmt::layer())
.init();
2023-07-11 13:41:56 +02:00
let config = Config {
data_dir: "./data".into(),
repo_dir: "./data/repos".into(),
pkg_dir: "./data/pkgs".into(),
};
let repo_manager = RepoGroupManager::new("./data/repos", "./data/pkgs", "x86_64");
let global = Global {
config,
repo_manager: Arc::new(RwLock::new(repo_manager)),
};
2023-07-11 13:41:56 +02:00
// build our application with a single route
let app = Router::new()
.merge(repo::router(&global))
2023-07-15 20:07:19 +02:00
.with_state(global)
.layer(TraceLayer::new_for_http());
2023-07-11 13:41:56 +02:00
// run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}