wayver's git archive


a simple self-hosted git server
git clone https://git.wayver.dev/bile

src/lib.rs@7016b1e2e8158b6496a17a520c7f6da5dc38e9ee

raw
Date Commit Message Author Files + -
2026-02-17 22:18 restructured bile library wayverd 3 90 74
...

1#![deny(rust_2018_idioms, unsafe_code)]
2#![warn(
3    absolute_paths_not_starting_with_crate,
4    ambiguous_associated_items,
5    anonymous_parameters,
6    arithmetic_overflow,
7    array_into_iter,
8    asm_sub_register,
9    bad_asm_style,
10    bindings_with_variant_name,
11    break_with_label_and_loop,
12    clashing_extern_declarations,
13    coherence_leak_check,
14    conflicting_repr_hints,
15    confusable_idents,
16    const_evaluatable_unchecked,
17    const_item_mutation,
18    dangling_pointers_from_temporaries,
19    dead_code,
20    deprecated_in_future,
21    deprecated_where_clause_location,
22    deprecated,
23    deref_into_dyn_supertrait,
24    deref_nullptr,
25    drop_bounds,
26    duplicate_macro_attributes,
27    dyn_drop,
28    ellipsis_inclusive_range_patterns,
29    enum_intrinsics_non_enums,
30    explicit_outlives_requirements,
31    exported_private_dependencies,
32    forbidden_lint_groups,
33    function_item_references,
34    future_incompatible,
35    ill_formed_attribute_input,
36    improper_ctypes_definitions,
37    improper_ctypes,
38    incomplete_features,
39    incomplete_include,
40    ineffective_unstable_trait_impl,
41    inline_no_sanitize,
42    invalid_atomic_ordering,
43    invalid_doc_attributes,
44    invalid_type_param_default,
45    invalid_value,
46    irrefutable_let_patterns,
47    keyword_idents,
48    large_assignments,
49    late_bound_lifetime_arguments,
50    legacy_derive_helpers,
51    macro_expanded_macro_exports_accessed_by_absolute_paths,
52    meta_variable_misuse,
53    missing_abi,
54    missing_copy_implementations,
55    mixed_script_confusables,
56    mutable_transmutes,
57    named_arguments_used_positionally,
58    named_asm_labels,
59    no_mangle_const_items,
60    no_mangle_generic_items,
61    non_ascii_idents,
62    non_camel_case_types,
63    non_fmt_panics,
64    non_shorthand_field_patterns,
65    non_snake_case,
66    non_upper_case_globals,
67    nonstandard_style,
68    noop_method_call,
69    overflowing_literals,
70    overlapping_range_endpoints,
71    path_statements,
72    patterns_in_fns_without_body,
73    proc_macro_derive_resolution_fallback,
74    pub_use_of_private_extern_crate,
75    redundant_semicolons,
76    repr_transparent_external_private_fields,
77    rust_2021_incompatible_closure_captures,
78    rust_2021_incompatible_or_patterns,
79    rust_2021_prefixes_incompatible_syntax,
80    rust_2021_prelude_collisions,
81    semicolon_in_expressions_from_macros,
82    soft_unstable,
83    stable_features,
84    text_direction_codepoint_in_comment,
85    text_direction_codepoint_in_literal,
86    trivial_bounds,
87    trivial_casts,
88    trivial_numeric_casts,
89    type_alias_bounds,
90    tyvar_behind_raw_pointer,
91    uncommon_codepoints,
92    unconditional_panic,
93    unconditional_recursion,
94    unexpected_cfgs,
95    uninhabited_static,
96    unknown_crate_types,
97    unnameable_test_items,
98    unreachable_code,
99    unreachable_patterns,
100    unreachable_pub,
101    unsafe_op_in_unsafe_fn,
102    unstable_features,
103    unstable_name_collisions,
104    unused_allocation,
105    unused_assignments,
106    unused_attributes,
107    unused_braces,
108    unused_comparisons,
109    unused_crate_dependencies,
110    unused_doc_comments,
111    unused_extern_crates,
112    unused_features,
113    unused_import_braces,
114    unused_imports,
115    unused_labels,
116    unused_lifetimes,
117    unused_macro_rules,
118    unused_macros,
119    unused_must_use,
120    unused_mut,
121    unused_parens,
122    unused_qualifications,
123    unused_unsafe,
124    unused_variables,
125    useless_deprecated,
126    while_true
127)]
128#![warn(
129    clippy::all,
130    clippy::await_holding_lock,
131    clippy::char_lit_as_u8,
132    clippy::checked_conversions,
133    clippy::cognitive_complexity,
134    clippy::dbg_macro,
135    clippy::debug_assert_with_mut_call,
136    clippy::disallowed_script_idents,
137    clippy::doc_link_with_quotes,
138    clippy::doc_markdown,
139    clippy::empty_enum,
140    clippy::empty_line_after_outer_attr,
141    clippy::empty_structs_with_brackets,
142    clippy::enum_glob_use,
143    clippy::equatable_if_let,
144    clippy::exit,
145    clippy::expl_impl_clone_on_copy,
146    clippy::explicit_deref_methods,
147    clippy::explicit_into_iter_loop,
148    clippy::fallible_impl_from,
149    clippy::filter_map_next,
150    clippy::flat_map_option,
151    clippy::float_cmp_const,
152    clippy::float_cmp,
153    clippy::float_equality_without_abs,
154    clippy::fn_params_excessive_bools,
155    clippy::fn_to_numeric_cast_any,
156    clippy::from_iter_instead_of_collect,
157    clippy::if_let_mutex,
158    clippy::implicit_clone,
159    clippy::imprecise_flops,
160    clippy::index_refutable_slice,
161    clippy::inefficient_to_string,
162    clippy::invalid_upcast_comparisons,
163    clippy::iter_not_returning_iterator,
164    clippy::large_digit_groups,
165    clippy::large_stack_arrays,
166    clippy::large_types_passed_by_value,
167    clippy::let_unit_value,
168    clippy::linkedlist,
169    clippy::lossy_float_literal,
170    clippy::macro_use_imports,
171    clippy::manual_ok_or,
172    clippy::map_err_ignore,
173    clippy::map_flatten,
174    clippy::map_unwrap_or,
175    clippy::match_same_arms,
176    clippy::match_wild_err_arm,
177    clippy::match_wildcard_for_single_variants,
178    clippy::mem_forget,
179    clippy::missing_const_for_fn,
180    clippy::missing_enforced_import_renames,
181    clippy::mut_mut,
182    clippy::mutex_integer,
183    clippy::needless_borrow,
184    clippy::needless_continue,
185    clippy::needless_for_each,
186    clippy::needless_pass_by_value,
187    clippy::negative_feature_names,
188    clippy::nonstandard_macro_braces,
189    clippy::nursery,
190    clippy::option_if_let_else,
191    clippy::option_option,
192    clippy::path_buf_push_overwrite,
193    clippy::pedantic,
194    clippy::print_stderr,
195    clippy::print_stdout,
196    clippy::ptr_as_ptr,
197    clippy::rc_mutex,
198    clippy::ref_option_ref,
199    clippy::rest_pat_in_fully_bound_structs,
200    clippy::same_functions_in_if_condition,
201    clippy::semicolon_if_nothing_returned,
202    clippy::shadow_unrelated,
203    clippy::similar_names,
204    clippy::single_match_else,
205    clippy::string_add_assign,
206    clippy::string_add,
207    clippy::string_lit_as_bytes,
208    clippy::suspicious_operation_groupings,
209    clippy::todo,
210    clippy::trailing_empty_array,
211    clippy::trait_duplication_in_bounds,
212    clippy::trivially_copy_pass_by_ref,
213    clippy::unimplemented,
214    clippy::unnecessary_wraps,
215    clippy::unnested_or_patterns,
216    clippy::unseparated_literal_suffix,
217    clippy::unused_self,
218    clippy::use_debug,
219    clippy::use_self,
220    clippy::used_underscore_binding,
221    clippy::useless_let_if_seq,
222    clippy::useless_transmute,
223    clippy::verbose_file_reads,
224    clippy::wildcard_dependencies,
225    clippy::wildcard_imports,
226    clippy::zero_sized_map_values
227)]
228#![allow(clippy::missing_errors_doc)]
229
230pub mod handlers;
231pub mod utils;
232
233pub mod config;
234
235use std::{
236    str,
237    sync::{LazyLock, OnceLock},
238    time::Duration,
239};
240
241use axum::{Router, http::StatusCode, routing::get};
242use axum_response_cache::CacheLayer;
243use syntect::parsing::SyntaxSet;
244use tower_helmet::HelmetLayer;
245use tower_http::{timeout::TimeoutLayer, trace::TraceLayer};
246
247use crate::{
248    config::Config,
249    utils::response::{Css, Ico, Json, Png, Text},
250};
251
252#[global_allocator]
253static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
254
255static CONFIG: OnceLock<Config> = OnceLock::new();
256static SYNTAXES: LazyLock<SyntaxSet> = LazyLock::new(two_face::syntax::extra_newlines);
257
258static APPLE_TOUCH_ICON_PNG: &[u8] = include_bytes!("../assets/apple-touch-icon.png");
259static FAVICON_ICO: &[u8] = include_bytes!("../assets/favicon.ico");
260static ICON_192_MASKABLE: &[u8] = include_bytes!("../assets/icon-192-maskable.png");
261static ICON_192: &[u8] = include_bytes!("../assets/icon-192.png");
262static ICON_512_MASKABLE: &[u8] = include_bytes!("../assets/icon-512-maskable.png");
263static ICON_512: &[u8] = include_bytes!("../assets/icon-512.png");
264static MANIFEST_JSON: &str = include_str!("../assets/manifest.json");
265static ROBOTS_TXT: &str = include_str!("../assets/robots.txt");
266static STYLE_CSS: &str = include_str!("../assets/style.css");
267
268static META_PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION");
269
270pub(crate) fn config() -> &'static Config {
271    CONFIG
272        .get()
273        .unwrap_or_else(|| unreachable!("failed to get global config, this should not happen"))
274}
275
276#[allow(missing_copy_implementations, clippy::empty_structs_with_brackets)]
277pub struct Bile {}
278
279impl Bile {
280    /// Create a new Bile 'instance'
281    ///
282    /// # Panics
283    ///
284    /// This will panic if you create multiple Bile instances.
285    ///
286    /// Which you shouldn't BTW.
287    pub fn init(config: Config) -> Self {
288        CONFIG.set(config).expect("config already set");
289
290        Self {}
291    }
292
293    #[rustfmt::skip]
294    pub fn routes(&self) -> Router {
295        // let backend = MokaBackend::builder().max_entries(10_000).build();
296
297        // let config = hitbox::Config::builder()
298        //     .request_predicate(predicates::request::Method::new(http::Method::GET).unwrap())
299        //     .response_predicate(hitbox::Neutral::new().status_code_class(predicates::response::StatusClass::Success))
300        //     .extractor(extractors::Method::new())
301        //     .policy(
302        //         hitbox::policy::PolicyConfig::builder()
303        //             .ttl(Duration::from_secs(60))
304        //             .stale(Duration::from_secs(30))
305        //             .build(),
306        //     )
307        //     .build();
308
309        // let cache = hitbox_tower::Cache::builder()
310        //     .backend(backend.clone())
311        //     .config(config)
312        //     .build();
313
314        Router::new()
315            .route("/", get(handlers::index::get))
316            // assets
317            .route("/apple-touch-icon.png", get(async || Png(APPLE_TOUCH_ICON_PNG)))
318            .route("/favicon.ico", get(async || Ico(FAVICON_ICO)))
319            .route("/icon-192-maskable.png", get(async || Png(ICON_192_MASKABLE)))
320            .route("/icon-192.png", get(async || Png(ICON_192)))
321            .route("/icon-512-maskable.png", get(async || Png(ICON_512_MASKABLE)))
322            .route("/icon-512.png", get(async || Png(ICON_512)))
323            .route("/manifest.json", get(async || Json(MANIFEST_JSON)))
324            .route("/robots.txt", get(async || Text(ROBOTS_TXT)))
325            .route("/style.css", get(async || Css(STYLE_CSS)))
326            //
327            .route("/{repo_name}", get(handlers::repo_home::get))
328            .route("/{repo_name}/", get(handlers::repo_home::get))
329            // git clone stuff
330            .route("/{repo_name}/info/refs", get(handlers::git::get_1))
331            .route("/{repo_name}/HEAD", get(handlers::git::get_1))
332            .route("/{repo_name}/objects/{*obj}", get(handlers::git::get_2))
333            // web pages
334            .route("/{repo_name}/commit/{commit}", get(handlers::repo_commit::get))
335            .route("/{repo_name}/refs", get(handlers::repo_refs::get))
336            .route("/{repo_name}/refs/", get(handlers::repo_refs::get))
337            .route("/{repo_name}/refs.xml", get(handlers::repo_refs_feed::get))
338            .route("/{repo_name}/refs/{tag}", get(handlers::repo_tag::get))
339            //
340            .route("/{repo_name}/log", get(handlers::repo_log::get_1))
341            .route("/{repo_name}/log/", get(handlers::repo_log::get_1))
342            .route("/{repo_name}/log.xml", get(handlers::repo_log_feed::get_1))
343            .route("/{repo_name}/log/{ref}", get(handlers::repo_log::get_2))
344            .route("/{repo_name}/log/{ref}/", get(handlers::repo_log::get_2))
345            .route("/{repo_name}/log/{ref}/feed.xml", get(handlers::repo_log_feed::get_2))
346            .route("/{repo_name}/log/{ref}/{*object_name}", get(handlers::repo_log::get_3))
347            //
348            .route("/{repo_name}/tree", get(handlers::repo_file::get_1))
349            .route("/{repo_name}/tree/", get(handlers::repo_file::get_1))
350            .route("/{repo_name}/tree/{ref}", get(handlers::repo_file::get_2))
351            .route("/{repo_name}/tree/{ref}/", get(handlers::repo_file::get_2))
352            .route("/{repo_name}/tree/{ref}/item/{*object_name}", get(handlers::repo_file::get_3))
353            .route("/{repo_name}/tree/{ref}/raw/{*object_name}", get(handlers::repo_file_raw::get))
354            //
355            .layer((
356                TraceLayer::new_for_http(),
357                TimeoutLayer::with_status_code(StatusCode::REQUEST_TIMEOUT, Duration::from_secs(10)),
358                CacheLayer::with_lifespan(Duration::from_secs(60)).use_stale_on_failure(),
359                HelmetLayer::with_defaults(),
360            ))
361    }
362}
363