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
67
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/:repo", get(crate::handlers::head_repo_handler))
.route("/repository/:repo/tree/*path", get(crate::handlers::head_tree_handler))
.route("/repository/:repo/blob/*path", get(crate::handlers::head_blob_handler))
.route("/repository/:repo/branch/:branch", get(crate::handlers::branch_repo_handler))
.route("/repository/:repo/branch/:branch/tree/*path", get(crate::handlers::branch_tree_handler))
.route("/repository/:repo/branch/:branch/blob/*path", get(crate::handlers::branch_blob_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!("{} v{} now listening on {}", configuration.application_name, env!("CARGO_PKG_VERSION"), listener.local_addr()?);
// Start the server
serve(listener, router).await?;
Ok(())
}