Skip to content

Commit db289bc

Browse files
committed
feat: implement basic git http backend based on axum and git command
1 parent 3d9dfc3 commit db289bc

File tree

11 files changed

+412
-30
lines changed

11 files changed

+412
-30
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# The Server port.
2+
PORT=8080
3+
4+
# The path to git repositories.
5+
ROOT=/tmp/repos/

.env.sample

Lines changed: 0 additions & 2 deletions
This file was deleted.

Cargo.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ path = "src/lib.rs"
1212
[dependencies]
1313
anyhow = "1.0.98"
1414
axum = { version = "0.8.4" }
15-
clap = { version = "4.5.39", features = ["derive", "env"] }
15+
clap = { version = "4.5.40", features = ["derive", "env"] }
1616
dotenv = "0.15.0"
1717
serde = { version = "1.0.219", features = ["derive"] }
1818
serde_json = "1.0.140"

src/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
// For development convenience, these can also be read from a `.env` file in the working
2525
// directory where the application is started.
2626
//
27-
// See `.env.sample` in the repository root for details.
27+
// See `.env.example` in the repository root for details.
28+
29+
use std::path::PathBuf;
2830

2931
#[derive(Clone, clap::Parser)]
3032
pub struct Config {
3133
/// The Server port.
3234
#[clap(long, env = "PORT", default_value = "8080")]
3335
pub port: u16,
36+
37+
/// The path to git repositories.
38+
#[clap(long, env = "ROOT")]
39+
pub root: PathBuf,
3440
}

src/errors.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub enum ApiError {
2727
#[error("Not Found")]
2828
NotFound,
2929

30+
#[error("Bad Request")]
31+
BadRequest(String),
32+
3033
#[error("Not Found Workflow: {0}")]
3134
NotFoundWorkflow(String),
3235

@@ -38,18 +41,38 @@ pub enum ApiError {
3841

3942
#[error("Bad Workflow Request: {0}")]
4043
BadWorkflowRequest(String),
44+
45+
#[error("Invalid path")]
46+
InvalidPath,
47+
48+
#[error("Axum error: {0}")]
49+
AxumError(String),
50+
51+
#[error("Git command failed: {0}")]
52+
GitCommandFailed(String),
53+
}
54+
55+
impl From<axum::http::Error> for ApiError {
56+
fn from(err: axum::http::Error) -> Self {
57+
ApiError::AxumError(err.to_string())
58+
}
4159
}
4260

4361
impl IntoResponse for ApiError {
4462
fn into_response(self) -> axum::response::Response {
45-
let (status, message) = match self {
46-
Self::InternalServerError => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
47-
Self::NotFound => (StatusCode::NOT_FOUND, self.to_string()),
48-
Self::NotFoundWorkflow(e) => (StatusCode::NOT_FOUND, e.to_string()),
49-
Self::FailedToCreateWorkflow(e) => (StatusCode::BAD_REQUEST, e.to_string()),
50-
Self::FailedToDeleteWorkflow(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
51-
Self::BadWorkflowRequest(e) => (StatusCode::BAD_REQUEST, e.to_string()),
63+
let status = match self {
64+
Self::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
65+
Self::NotFound => StatusCode::NOT_FOUND,
66+
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
67+
Self::NotFoundWorkflow(_) => StatusCode::NOT_FOUND,
68+
Self::FailedToCreateWorkflow(_) => StatusCode::BAD_REQUEST,
69+
Self::FailedToDeleteWorkflow(_) => StatusCode::INTERNAL_SERVER_ERROR,
70+
Self::BadWorkflowRequest(_) => StatusCode::BAD_REQUEST,
71+
Self::InvalidPath => StatusCode::BAD_REQUEST,
72+
Self::AxumError(_) => StatusCode::INTERNAL_SERVER_ERROR,
73+
Self::GitCommandFailed(_) => StatusCode::INTERNAL_SERVER_ERROR,
5274
};
75+
let message = self.to_string();
5376

5477
error!("{} - {}", status, message);
5578
(status, Json(json!({ "message": message }))).into_response()

0 commit comments

Comments
 (0)