Echo Writes Code

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
mod breadcrumbs;
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(handlers::index_handler))
		.route("/repository/:repository/head", get(handlers::head_repository_handler))
		.route("/repository/:repository/head/tree/*path", get(handlers::head_tree_handler))
		.route("/repository/:repository/head/blob/*path", get(handlers::head_blob_handler))
		.route("/repository/:repository/head/history/:current_page", get(handlers::head_history_handler))
		.route("/repository/:repository/branch/:branch", get(handlers::branch_repository_handler))
		.route("/repository/:repository/branch/:branch/tree/*path", get(handlers::branch_tree_handler))
		.route("/repository/:repository/branch/:branch/blob/*path", get(handlers::branch_blob_handler))
		.route("/repository/:repository/branch/:branch/history/:current_page", get(handlers::branch_history_handler))
		.route("/repository/:repository/tag/:tag", get(handlers::tag_repository_handler))
		.route("/repository/:repository/tag/:tag/tree/*path", get(handlers::tag_tree_handler))
		.route("/repository/:repository/tag/:tag/blob/*path", get(handlers::tag_blob_handler))
		.route("/repository/:repository/tag/:tag/history/:current_page", get(handlers::tag_history_handler))
		.route("/repository/:repository/commit/:id", get(handlers::commit_repository_handler))
		.route("/repository/:repository/commit/:id/tree/*path", get(handlers::commit_tree_handler))
		.route("/repository/:repository/commit/:id/blob/*path", get(handlers::commit_blob_handler))
		.route("/repository/:repository/commit/:id/history/:current_page", get(handlers::commit_history_handler))
		.route("/repository/:repository/branches", get(handlers::branches_handler))
		.route("/repository/:repository/tags", get(handlers::tags_handler))
		.nest_service("/static", ServeDir::new("static"))
		.fallback(handlers::not_found_handler)
		.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!("gitten v{} now listening on {}", env!("CARGO_PKG_VERSION"), listener.local_addr()?);

	// Start the server
	serve(listener, router).await?;
	Ok(())
}