Page MenuHomePhorge

main.rs
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None
// A code server backend with a git server over https
// Use jj_lib for git operations
// Use tokio for async operations
// Use axum for the web server
use axum::{
error_handling::HandleError,
http::StatusCode,
response::IntoResponse,
routing::{Router, get},
};
use git::CodeState;
use std::{path::PathBuf, sync::Arc};
use tokio::sync::Mutex;
use tower_http::cors::CorsLayer;
use tracing::info;
use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _};
pub mod git;
pub mod http;
#[dotenvy::load]
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
// axum logs rejections from built-in extractors with the `axum::rejection`
// target, at `TRACE` level. `axum::rejection=trace` enables showing those events
format!(
"{}=debug,tower_http=debug,axum::rejection=trace",
env!("CARGO_CRATE_NAME")
)
.into()
}),
)
.with(tracing_subscriber::fmt::layer())
.init();
let repository_path = dotenvy::var("REPOSITORY_PATH").unwrap_or(String::from("repositories"));
let git_server_address =
dotenvy::var("GIT_SERVER_URL").unwrap_or(String::from("localhost:8080"));
let state = Arc::new(Mutex::new(CodeState::new(PathBuf::from(repository_path))));
// clone zlib-ng for testing (if it doesn't exist)
{
let mut state = state.lock().await;
if std::fs::exists(state.get_repository_directory().join("zlib-ng")).is_ok() {
info!("zlib-ng already exists, skipping clone");
} else {
state
.add_repository_from_url("zlib-ng", "https://github.com/zlib-ng/zlib-ng")
.unwrap_or_else(|err| {
eprintln!("{}", err);
std::process::exit(1);
});
}
}
// Discover repositories in the repository path
{
let mut state = state.lock().await;
state.discover_repositories();
}
let app = Router::new()
.route("/api/v1/health", get(health))
.route(
"/{organization}/{repository}/info/refs",
get(http::info_refs),
)
.layer(CorsLayer::permissive())
.with_state(state.clone());
info!("Listening on {}", git_server_address);
let listener = tokio::net::TcpListener::bind(git_server_address)
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn health() -> impl IntoResponse {
let response = "OK";
(StatusCode::OK, response)
}

File Metadata

Mime Type
text/x-c
Expires
Sun, Apr 20, 11:35 PM (2 d)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
48326
Default Alt Text
main.rs (2 KB)

Event Timeline