|
1 | 1 | #![allow(unused_imports)] |
2 | | -use axum::{http::StatusCode, response::IntoResponse, Router}; |
| 2 | +use axum::extract::State; |
| 3 | +use axum::http::{Method, Uri}; |
| 4 | +use axum::routing::{get, post}; |
| 5 | +use axum::{http::StatusCode, response::IntoResponse, Router, ServiceExt}; |
| 6 | +#[cfg(feature = "https")] |
| 7 | +use axum_server::tls_rustls::RustlsConfig; |
3 | 8 | use std::io; |
| 9 | +use std::net::SocketAddr; |
4 | 10 | use tokio::net::TcpListener; |
5 | | -use tracing::{error, info}; |
| 11 | +use tracing::{error, info, Level}; |
6 | 12 | use tracing_appender::rolling::{RollingFileAppender, Rotation}; |
| 13 | +use tracing_subscriber::fmt::format; |
| 14 | +use tracing_subscriber::layer::Filter; |
7 | 15 | use tracing_subscriber::{fmt, prelude::*, Registry}; |
8 | 16 |
|
| 17 | +#[derive(Clone)] |
| 18 | +struct GitServer { |
| 19 | + instance_url: String, |
| 20 | + router: Router<GitServer>, |
| 21 | + addr: SocketAddr, |
| 22 | +} |
9 | 23 | #[tokio::main] |
10 | 24 | async fn main() { |
11 | 25 | let file_appender = RollingFileAppender::new(Rotation::DAILY, "logs", "app.log"); |
12 | 26 |
|
13 | | - let stdout_layer = fmt::layer().with_writer(io::stdout).with_ansi(true); |
| 27 | + let stdout_layer = fmt::layer() |
| 28 | + .with_writer(io::stdout) |
| 29 | + .with_ansi(true) |
| 30 | + .pretty() |
| 31 | + .without_time() |
| 32 | + .with_filter(tracing_subscriber::filter::LevelFilter::DEBUG); |
14 | 33 |
|
15 | 34 | let file_layer = fmt::layer().with_writer(file_appender).with_ansi(false); |
16 | 35 |
|
17 | 36 | let subscriber = Registry::default().with(stdout_layer).with(file_layer); |
18 | 37 |
|
19 | 38 | tracing::subscriber::set_global_default(subscriber).expect("Failed to set global subscriber"); |
20 | 39 |
|
21 | | - let app = Router::new().fallback(fallback); |
22 | | - |
23 | 40 | let listener = TcpListener::bind("0.0.0.0:80").await.unwrap(); |
24 | 41 | let addr = listener.local_addr().unwrap(); |
25 | 42 |
|
| 43 | + let router = Router::new() |
| 44 | + .route("/init/{user}/{repo_name}", post(init)) |
| 45 | + .route("/u/{user}/{repo_name}/{*path}", get(handle_repo)) |
| 46 | + .fallback(fallback); |
| 47 | + let state = GitServer { |
| 48 | + addr: addr.clone(), |
| 49 | + instance_url: format!("http://{}", addr), |
| 50 | + router: router.clone(), |
| 51 | + }; |
| 52 | + let router = router.with_state(state); |
| 53 | + |
26 | 54 | info!("Server listening on {}", addr); |
27 | 55 |
|
28 | | - axum::serve(listener, app) |
| 56 | + if cfg!(feature = "https") { |
| 57 | + #[cfg(feature = "https")] |
| 58 | + { |
| 59 | + serve_tls(addr, router).await; |
| 60 | + } |
| 61 | + } else { |
| 62 | + axum::serve(listener, router) |
| 63 | + .with_graceful_shutdown(shutdown_signal()) |
| 64 | + .await |
| 65 | + .unwrap(); |
| 66 | + } |
| 67 | +} |
| 68 | +#[cfg(feature = "https")] |
| 69 | +async fn serve_tls(addr: std::net::SocketAddr, app: Router<GitServer>) { |
| 70 | + let config = RustlsConfig::from_pem_file( |
| 71 | + "examples/self-signed-certs/cert.pem", |
| 72 | + "examples/self-signed-certs/key.pem", |
| 73 | + ) |
| 74 | + .await |
| 75 | + .unwrap(); |
| 76 | + axum_server::bind_rustls(addr, config) |
| 77 | + .serve(app.into_make_service()) |
29 | 78 | .with_graceful_shutdown(shutdown_signal()) |
30 | 79 | .await |
31 | 80 | .unwrap(); |
32 | 81 | } |
33 | 82 |
|
34 | | -// Graceful shutdown handler. |
35 | 83 | async fn shutdown_signal() { |
36 | 84 | tokio::signal::ctrl_c() |
37 | 85 | .await |
38 | 86 | .expect("Failed to install CTRL+C signal handler"); |
39 | 87 | info!("Shutting down server..."); |
40 | 88 | } |
41 | 89 |
|
42 | | -async fn fallback(uri: axum::http::Uri) -> impl IntoResponse { |
43 | | - error!("404 - Not Found: {}", uri); |
44 | | - (StatusCode::NOT_FOUND, "404 - Not Found") |
| 90 | +async fn init( |
| 91 | + axum::extract::Path((user, repo_name)): axum::extract::Path<(String, String)>, |
| 92 | +) -> impl IntoResponse { |
| 93 | + let repo_path = format!("repos/{}/{}", user, repo_name); |
| 94 | + let repo = git2::Repository::init_bare(&repo_path) |
| 95 | + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR.into_response()); |
| 96 | + match repo { |
| 97 | + Ok(repo) => { |
| 98 | + // Create the info/refs file |
| 99 | + let refs_path = format!("{}/info/refs", repo_path); |
| 100 | + if let Err(e) = std::fs::File::create(&refs_path) { |
| 101 | + error!("Failed to create info/refs file: {:#?}", e); |
| 102 | + return ( |
| 103 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 104 | + "Failed to initialize repository".to_string(), |
| 105 | + ) |
| 106 | + .into_response(); |
| 107 | + } |
| 108 | + info!("Initialized repository: {}", repo.path().display()); |
| 109 | + ( |
| 110 | + StatusCode::CREATED, |
| 111 | + format!("Initialized repository: {}", repo.path().display()), |
| 112 | + ) |
| 113 | + .into_response() |
| 114 | + } |
| 115 | + Err(e) => { |
| 116 | + error!("Failed to initialize repository: {:#?}", e); |
| 117 | + ( |
| 118 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 119 | + "Failed to initialize repository".to_string(), |
| 120 | + ) |
| 121 | + .into_response() |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | +async fn handle_repo( |
| 126 | + axum::extract::Path((user, repo_name, path)): axum::extract::Path<(String, String, String)>, |
| 127 | +) -> impl IntoResponse { |
| 128 | + let repo_path = format!("repos/{}/{}", user, repo_name); |
| 129 | + let file_path = format!("{}/{}", repo_path, path); |
| 130 | + |
| 131 | + match tokio::fs::metadata(&file_path).await { |
| 132 | + Ok(metadata) => { |
| 133 | + if metadata.is_dir() { |
| 134 | + info!("Directory: {}", file_path); |
| 135 | + (StatusCode::OK, format!("Directory: {}", file_path)).into_response() |
| 136 | + } else { |
| 137 | + match tokio::fs::read(&file_path).await { |
| 138 | + Ok(contents) => (StatusCode::OK, contents).into_response(), |
| 139 | + Err(_) => ( |
| 140 | + StatusCode::NOT_FOUND, |
| 141 | + format!("File not found: {}", file_path), |
| 142 | + ) |
| 143 | + .into_response(), |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + Err(_) => ( |
| 148 | + StatusCode::NOT_FOUND, |
| 149 | + format!("Path not found: {}", file_path), |
| 150 | + ) |
| 151 | + .into_response(), |
| 152 | + } |
| 153 | +} |
| 154 | +async fn fallback( |
| 155 | + uri: axum::http::Uri, |
| 156 | + State(state): State<GitServer>, |
| 157 | + method: axum::http::Method, |
| 158 | +) -> impl IntoResponse { |
| 159 | + let mut msg = format!("404 - Not Found: {} {}", method, uri); |
| 160 | + error!("{}", msg); |
| 161 | + if let Some(uri) = uri.query() { |
| 162 | + let uri = uri.to_string(); |
| 163 | + if uri.contains("service=git") { |
| 164 | + let instance_url = state.instance_url; |
| 165 | + } |
| 166 | + } |
| 167 | + error!("{}", msg); |
| 168 | + (StatusCode::NOT_FOUND, msg) |
45 | 169 | } |
0 commit comments