gitscope
← rgit / 0.1.4 / src/main.rs
https://dev.kronoz.win/r/rgit.git
src/main.rs · Rust
Blame Raw
  1#![deny(clippy::pedantic)]
  2
  3use std::{
  4    borrow::Cow,
  5    fmt::{Display, Formatter},
  6    future::IntoFuture,
  7    net::SocketAddr,
  8    path::PathBuf,
  9    str::FromStr,
 10    sync::{Arc, OnceLock},
 11    time::Duration,
 12};
 13
 14use anyhow::Context;
 15use askama::Template;
 16use axum::{
 17    body::Body,
 18    http,
 19    http::{HeaderValue, StatusCode},
 20    response::{IntoResponse, Response},
 21    routing::get,
 22    Extension, Router,
 23};
 24use clap::Parser;
 25use const_format::formatcp;
 26use database::schema::SCHEMA_VERSION;
 27use rocksdb::{Options, SliceTransform};
 28use tokio::{
 29    net::TcpListener,
 30    signal::unix::{signal, SignalKind},
 31    sync::mpsc,
 32};
 33use tower_http::{cors::CorsLayer, timeout::TimeoutLayer};
 34use tower_layer::layer_fn;
 35use tracing::{error, info, instrument, warn};
 36use tracing_subscriber::{
 37    fmt::format::FmtSpan, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter,
 38};
 39use xxhash_rust::const_xxh3;
 40
 41use crate::{
 42    database::schema::prefixes::{
 43        COMMIT_COUNT_FAMILY, COMMIT_FAMILY, REFERENCE_FAMILY, REPOSITORY_FAMILY, TAG_FAMILY,
 44    },
 45    git::Git,
 46    layers::logger::LoggingMiddleware,
 47    syntax_highlight::prime_highlighters,
 48    theme::Theme,
 49};
 50
 51mod database;
 52mod git;
 53mod layers;
 54mod methods;
 55mod syntax_highlight;
 56mod theme;
 57mod unified_diff_builder;
 58
 59const CRATE_VERSION: &str = clap::crate_version!();
 60
 61const GLOBAL_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/statics/css/style.css"));
 62const GLOBAL_CSS_HASH: &str = const_hex::Buffer::<16, false>::new()
 63    .const_format(&const_xxh3::xxh3_128(GLOBAL_CSS).to_be_bytes())
 64    .as_str();
 65
 66static HIGHLIGHT_CSS_HASH: OnceLock<Box<str>> = OnceLock::new();
 67static DARK_HIGHLIGHT_CSS_HASH: OnceLock<Box<str>> = OnceLock::new();
 68
 69#[derive(Parser, Debug)]
 70#[clap(author, version, about)]
 71pub struct Args {
 72    /// Path to a directory in which the `RocksDB` database should be stored, will be created if it doesn't already exist
 73    ///
 74    /// The `RocksDB` database is very quick to generate, so this can be pointed to temporary storage
 75    #[clap(short, long, value_parser)]
 76    db_store: PathBuf,
 77    /// The socket address to bind to (eg. 0.0.0.0:3333)
 78    bind_address: SocketAddr,
 79    /// The path in which your bare Git repositories reside (will be scanned recursively)
 80    scan_path: PathBuf,
 81    /// Configures the metadata refresh interval (eg. "never" or "60s")
 82    #[clap(long, default_value_t = RefreshInterval::Duration(Duration::from_secs(300)))]
 83    refresh_interval: RefreshInterval,
 84    /// Configures the request timeout.
 85    #[clap(long, default_value_t = Duration::from_secs(10).into())]
 86    request_timeout: humantime::Duration,
 87}
 88
 89#[derive(Debug, Clone, Copy)]
 90pub enum RefreshInterval {
 91    Never,
 92    Duration(Duration),
 93}
 94
 95impl Display for RefreshInterval {
 96    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 97        match self {
 98            Self::Never => write!(f, "never"),
 99            Self::Duration(s) => write!(f, "{}", humantime::format_duration(*s)),
100        }
101    }
102}
103
104impl FromStr for RefreshInterval {
105    type Err = &'static str;
106
107    fn from_str(s: &str) -> Result<Self, Self::Err> {
108        if s == "never" {
109            Ok(Self::Never)
110        } else if let Ok(v) = humantime::parse_duration(s) {
111            Ok(Self::Duration(v))
112        } else {
113            Err("must be seconds, a human readable duration (eg. '10m') or 'never'")
114        }
115    }
116}
117
118#[tokio::main]
119#[allow(clippy::too_many_lines)]
120async fn main() -> Result<(), anyhow::Error> {
121    let args: Args = Args::parse();
122
123    if std::env::var_os("RUST_LOG").is_none() {
124        std::env::set_var("RUST_LOG", "info");
125    }
126
127    let logger_layer = tracing_subscriber::fmt::layer().with_span_events(FmtSpan::CLOSE);
128    let env_filter = EnvFilter::from_default_env();
129
130    tracing_subscriber::registry()
131        .with(env_filter)
132        .with(logger_layer)
133        .init();
134
135    let db = open_db(&args)?;
136
137    let indexer_wakeup_task =
138        run_indexer(db.clone(), args.scan_path.clone(), args.refresh_interval);
139
140    let css = {
141        let theme = basic_toml::from_str::<Theme>(include_str!("../themes/github_light.toml"))
142            .unwrap()
143            .build_css();
144        let css = Box::leak(
145            format!(r#"@media (prefers-color-scheme: light){{{theme}}}"#)
146                .into_boxed_str()
147                .into_boxed_bytes(),
148        );
149        HIGHLIGHT_CSS_HASH.set(build_asset_hash(css)).unwrap();
150        css
151    };
152
153    let dark_css = {
154        let theme = basic_toml::from_str::<Theme>(include_str!("../themes/onedark.toml"))
155            .unwrap()
156            .build_css();
157        let css = Box::leak(
158            format!(r#"@media (prefers-color-scheme: dark){{{theme}}}"#)
159                .into_boxed_str()
160                .into_boxed_bytes(),
161        );
162        DARK_HIGHLIGHT_CSS_HASH.set(build_asset_hash(css)).unwrap();
163        css
164    };
165
166    let static_favicon = |content: &'static [u8]| {
167        move || async move {
168            let mut resp = Response::new(Body::from(content));
169            resp.headers_mut().insert(
170                http::header::CONTENT_TYPE,
171                HeaderValue::from_static("image/x-icon"),
172            );
173            resp
174        }
175    };
176
177    let static_css = |content: &'static [u8]| {
178        move || async move {
179            let mut resp = Response::new(Body::from(content));
180            resp.headers_mut().insert(
181                http::header::CONTENT_TYPE,
182                HeaderValue::from_static("text/css"),
183            );
184            resp
185        }
186    };
187
188    info!("Priming highlighters...");
189    prime_highlighters();
190    info!("Server starting up...");
191
192    let app = Router::new()
193        .route("/", get(methods::index::handle))
194        .route(
195            formatcp!("/style-{}.css", GLOBAL_CSS_HASH),
196            get(static_css(GLOBAL_CSS)),
197        )
198        .route(
199            &format!("/highlight-{}.css", HIGHLIGHT_CSS_HASH.get().unwrap()),
200            get(static_css(css)),
201        )
202        .route(
203            &format!(
204                "/highlight-dark-{}.css",
205                DARK_HIGHLIGHT_CSS_HASH.get().unwrap()
206            ),
207            get(static_css(dark_css)),
208        )
209        .route(
210            "/favicon.ico",
211            get(static_favicon(include_bytes!("../statics/favicon.ico"))),
212        )
213        .fallback(methods::repo::service)
214        .layer(TimeoutLayer::new(args.request_timeout.into()))
215        .layer(layer_fn(LoggingMiddleware))
216        .layer(Extension(Arc::new(Git::new())))
217        .layer(Extension(db))
218        .layer(Extension(Arc::new(args.scan_path)))
219        .layer(CorsLayer::new());
220
221    let listener = TcpListener::bind(&args.bind_address).await?;
222    let app = app.into_make_service_with_connect_info::<SocketAddr>();
223    let server = axum::serve(listener, app).into_future();
224
225    tokio::select! {
226        res = server => res.context("failed to run server"),
227        res = indexer_wakeup_task => res.context("failed to run indexer"),
228        _ = tokio::signal::ctrl_c() => {
229            info!("Received ctrl-c, shutting down");
230            Ok(())
231        }
232    }
233}
234
235fn open_db(args: &Args) -> Result<Arc<rocksdb::DB>, anyhow::Error> {
236    loop {
237        let mut db_options = Options::default();
238        db_options.create_missing_column_families(true);
239        db_options.create_if_missing(true);
240
241        let mut commit_family_options = Options::default();
242        commit_family_options.set_prefix_extractor(SliceTransform::create(
243            "commit_prefix",
244            |input| memchr::memchr(b'\0', input).map_or(input, |idx| &input[..idx]),
245            None,
246        ));
247
248        let mut tag_family_options = Options::default();
249        tag_family_options.set_prefix_extractor(SliceTransform::create_fixed_prefix(
250            std::mem::size_of::<u64>(),
251        )); // repository id prefix
252
253        let db = rocksdb::DB::open_cf_with_opts(
254            &db_options,
255            &args.db_store,
256            vec![
257                (COMMIT_FAMILY, commit_family_options),
258                (REPOSITORY_FAMILY, Options::default()),
259                (TAG_FAMILY, tag_family_options),
260                (REFERENCE_FAMILY, Options::default()),
261                (COMMIT_COUNT_FAMILY, Options::default()),
262            ],
263        )?;
264
265        let needs_schema_regen = match db.get("schema_version")? {
266            Some(v) if v.as_slice() != SCHEMA_VERSION.as_bytes() => Some(Some(v)),
267            Some(_) => None,
268            None => {
269                db.put("schema_version", SCHEMA_VERSION)?;
270                None
271            }
272        };
273
274        if let Some(version) = needs_schema_regen {
275            let old_version = version
276                .as_deref()
277                .map_or(Cow::Borrowed("unknown"), String::from_utf8_lossy);
278
279            warn!("Clearing outdated database ({old_version} != {SCHEMA_VERSION})");
280
281            drop(db);
282            rocksdb::DB::destroy(&Options::default(), &args.db_store)?;
283        } else {
284            break Ok(Arc::new(db));
285        }
286    }
287}
288
289async fn run_indexer(
290    db: Arc<rocksdb::DB>,
291    scan_path: PathBuf,
292    refresh_interval: RefreshInterval,
293) -> Result<(), tokio::task::JoinError> {
294    let (indexer_wakeup_send, mut indexer_wakeup_recv) = mpsc::channel(10);
295
296    std::thread::spawn(move || loop {
297        info!("Running periodic index");
298        crate::database::indexer::run(&scan_path, &db);
299        info!("Finished periodic index");
300
301        if indexer_wakeup_recv.blocking_recv().is_none() {
302            break;
303        }
304    });
305
306    tokio::spawn({
307        let mut sighup = signal(SignalKind::hangup()).expect("could not subscribe to sighup");
308        let build_sleeper = move || async move {
309            match refresh_interval {
310                RefreshInterval::Never => futures_util::future::pending().await,
311                RefreshInterval::Duration(v) => tokio::time::sleep(v).await,
312            };
313        };
314
315        async move {
316            loop {
317                tokio::select! {
318                    _ = sighup.recv() => {},
319                    () = build_sleeper() => {},
320                }
321
322                if indexer_wakeup_send.send(()).await.is_err() {
323                    error!("Indexing thread has died and is no longer accepting wakeup messages");
324                }
325            }
326        }
327    })
328    .await
329}
330
331#[must_use]
332pub fn build_asset_hash(v: &[u8]) -> Box<str> {
333    let hasher = const_xxh3::xxh3_128(v);
334    let out = const_hex::encode(hasher.to_be_bytes());
335    Box::from(out)
336}
337
338pub struct TemplateResponse<T> {
339    template: T,
340}
341
342impl<T: Template> IntoResponse for TemplateResponse<T> {
343    #[instrument(skip_all)]
344    fn into_response(self) -> Response {
345        match self.template.render() {
346            Ok(body) => {
347                let headers = [(
348                    http::header::CONTENT_TYPE,
349                    HeaderValue::from_static(T::MIME_TYPE),
350                )];
351
352                (headers, body).into_response()
353            }
354            Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
355        }
356    }
357}
358
359pub fn into_response<T: Template>(template: T) -> impl IntoResponse {
360    TemplateResponse { template }
361}
362
363pub enum ResponseEither<A, B> {
364    Left(A),
365    Right(B),
366}
367
368impl<A: IntoResponse, B: IntoResponse> IntoResponse for ResponseEither<A, B> {
369    fn into_response(self) -> Response {
370        match self {
371            Self::Left(a) => a.into_response(),
372            Self::Right(b) => b.into_response(),
373        }
374    }
375}