gitten.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
mod configuration; mod errors; mod git; mod handlers; mod state; mod templates; use crate::errors::{ Result }; use crate::git::{ Git }; use crate::state::{ Gitten }; use axum::{ Router, serve }; use axum::routing::{ get }; use tera::{ Tera }; use tokio::net::{ TcpListener }; use tower_http::services::{ ServeDir }; use tower_http::trace::{ TraceLayer }; use std::path::{ Path }; use std::process::{ ExitCode }; use std::sync::{ Arc }; #[tokio::main] async fn main() -> ExitCode { let result = run_server().await; match result { Ok(_) => ExitCode::SUCCESS, Err(e) => { eprintln!("{}", e); ExitCode::FAILURE }, } } async fn run_server() -> Result<()> { let configuration = configuration::configure()?; // Configure the shared server state object let application_state = Gitten { application_name: configuration.application_name.clone(), git: Git::open(Path::new(&configuration.git_root))?, tera: Tera::new("templates/**/*.tera.html")?, }; // Define the application routing structure let router = Router::new() .layer(TraceLayer::new_for_http()) .route("/", get(crate::handlers::index_handler)) .route("/repository/:repository_name", get(crate::handlers::repository_handler)) .route("/repository/:repository_name/tree/path/*tree_path", get(crate::handlers::tree_path_handler)) .route("/repository/:repository_name/tree/id/:tree_id", get(crate::handlers::tree_id_handler)) .route("/repository/:repository_name/blob/path/*tree_path", get(crate::handlers::blob_path_handler)) .route("/repository/:repository_name/blob/id/:blob_id", get(crate::handlers::blob_id_handler)) .nest_service("/static", ServeDir::new("static")) .with_state(Arc::new(application_state)); // Bind to an address let address = format!("{}:{}", configuration.network_host, configuration.network_port); let listener = TcpListener::bind(address).await?; tracing::debug!("{} v0.1.0 now listening on {}", configuration.application_name, listener.local_addr()?); // Start the server serve(listener, router).await?; Ok(()) }