src/lib.rs@main
raw
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, clippy::redundant_pub_crate)]
229
230pub(crate) mod git;
231pub(crate) mod handlers;
232pub(crate) mod http;
233pub(crate) mod utils;
234
235pub mod config;
236pub mod error;
237
238use std::{str, time::Duration};
239
240use axum::{Router, http::StatusCode, routing::get};
241use axum_response_cache::CacheLayer;
242use tower_helmet::HelmetLayer;
243use tower_http::{timeout::TimeoutLayer, trace::TraceLayer};
244
245use crate::{
246 config::Config,
247 http::{
248 BileState,
249 response::{Css, Ico, Json, Png, Text},
250 },
251};
252
253#[global_allocator]
254static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
255
256static APPLE_TOUCH_ICON_PNG: &[u8] = include_bytes!("../assets/apple-touch-icon.png");
257static FAVICON_ICO: &[u8] = include_bytes!("../assets/favicon.ico");
258static ICON_192_MASKABLE: &[u8] = include_bytes!("../assets/icon-192-maskable.png");
259static ICON_192: &[u8] = include_bytes!("../assets/icon-192.png");
260static ICON_512_MASKABLE: &[u8] = include_bytes!("../assets/icon-512-maskable.png");
261static ICON_512: &[u8] = include_bytes!("../assets/icon-512.png");
262static MANIFEST_JSON: &str = include_str!("../assets/manifest.json");
263static ROBOTS_TXT: &str = include_str!("../assets/robots.txt");
264static STYLE_CSS: &str = include_str!("../assets/style.css");
265
266static META_PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION");
267
268#[allow(missing_copy_implementations, clippy::empty_structs_with_brackets)]
269pub struct Bile {
270 state: BileState,
271}
272
273impl Bile {
274 #[must_use]
282 pub fn init(config: Config) -> Self {
283 Self {
284 state: BileState::new(config, two_face::syntax::extra_newlines()),
285 }
286 }
287
288 #[rustfmt::skip]
289 pub fn routes(&self) -> Router {
290 Router::new()
291 .route("/", get(handlers::index::get))
292 .route("/apple-touch-icon.png", get(async || Png(APPLE_TOUCH_ICON_PNG)))
294 .route("/favicon.ico", get(async || Ico(FAVICON_ICO)))
295 .route("/icon-192-maskable.png", get(async || Png(ICON_192_MASKABLE)))
296 .route("/icon-192.png", get(async || Png(ICON_192)))
297 .route("/icon-512-maskable.png", get(async || Png(ICON_512_MASKABLE)))
298 .route("/icon-512.png", get(async || Png(ICON_512)))
299 .route("/manifest.json", get(async || Json(MANIFEST_JSON)))
300 .route("/robots.txt", get(async || Text(ROBOTS_TXT)))
301 .route("/style.css", get(async || Css(STYLE_CSS)))
302 .route("/{repo_name}", get(handlers::repo_home::get))
304 .route("/{repo_name}/", get(handlers::repo_home::get))
305 .route("/{repo_name}/info/refs", get(handlers::git::get_1))
307 .route("/{repo_name}/HEAD", get(handlers::git::get_1))
308 .route("/{repo_name}/objects/{*obj}", get(handlers::git::get_2))
309 .route("/{repo_name}/commit/{commit}", get(handlers::repo_commit::get))
311 .route("/{repo_name}/refs", get(handlers::repo_refs::get))
312 .route("/{repo_name}/refs/", get(handlers::repo_refs::get))
313 .route("/{repo_name}/refs.xml", get(handlers::repo_refs_feed::get))
314 .route("/{repo_name}/refs/{tag}", get(handlers::repo_tag::get))
315 .route("/{repo_name}/log", get(handlers::repo_log::get_1))
317 .route("/{repo_name}/log/", get(handlers::repo_log::get_1))
318 .route("/{repo_name}/log.xml", get(handlers::repo_log_feed::get_1))
319 .route("/{repo_name}/log/{ref}", get(handlers::repo_log::get_2))
320 .route("/{repo_name}/log/{ref}/", get(handlers::repo_log::get_2))
321 .route("/{repo_name}/log/{ref}/feed.xml", get(handlers::repo_log_feed::get_2))
322 .route("/{repo_name}/log/{ref}/{*object_name}", get(handlers::repo_log::get_3))
323 .route("/{repo_name}/tree", get(handlers::repo_file::get_1))
325 .route("/{repo_name}/tree/", get(handlers::repo_file::get_1))
326 .route("/{repo_name}/tree/{ref}", get(handlers::repo_file::get_2))
327 .route("/{repo_name}/tree/{ref}/", get(handlers::repo_file::get_2))
328 .route("/{repo_name}/tree/{ref}/item/{*object_name}", get(handlers::repo_file::get_3))
329 .route("/{repo_name}/tree/{ref}/raw/{*object_name}", get(handlers::repo_file_raw::get))
330 .with_state(self.state.clone())
332 .layer((
334 TraceLayer::new_for_http(),
335 TimeoutLayer::with_status_code(StatusCode::REQUEST_TIMEOUT, Duration::from_secs(10)),
336 CacheLayer::with_lifespan(Duration::from_secs(60)).use_stale_on_failure(),
337 {
338 let mut layer = HelmetLayer::with_defaults();
339 layer.enable(http::PermissionsPolicy);
340 layer
341 },
342 ))
343 }
344}
345