Skip to content
Clone
2f5d35e
84 lines 3.2 KB Rust
use axum::extract::{Path, State};
use axum::response::Response;
use maud::html;
use crate::handler::html::{
HtmlRepoPath, clone_https_url, clone_ssh_url, empty_state, resolve, response_html,
};
use crate::service::GitService;
use crate::share::access::BasicAuth;
#[utoipa::path(
get,
path = "/repos/{space_name}/{repo_name}/html/quickstart",
tag = "html",
description = "渲染空仓库或已初始化仓库的快速上手指引。",
operation_id = "render_quickstart",
params(HtmlRepoPath),
responses(
(status = 200, description = "empty repo quickstart html fragment", content_type = "text/html"),
(status = 404, description = "repo not found")
)
)]
pub async fn render_quickstart(
State(service): State<GitService>,
Path(path): Path<HtmlRepoPath>,
auth: BasicAuth,
) -> Response {
let ctx = match resolve(service.clone(), path.clone(), auth).await {
Ok(c) => c,
Err(resp) => return resp,
};
let is_empty = empty_state(&ctx.dto);
let base = "git-quickstart space-y-3 rounded-lg border border-dashed border-zinc-300 p-4";
let cls = if ctx.dto.is_archived {
format!("{base} opacity-60")
} else {
base.to_string()
};
let (https_url, _) = clone_https_url(&service, &ctx.space_name, &ctx.repo_name);
let (ssh_url, _) = clone_ssh_url(&service, &ctx.space_name, &ctx.repo_name);
let clone_url = if !https_url.is_empty() {
https_url.clone()
} else {
ssh_url.clone()
};
let markup = html! {
div class=(cls)
data-component="git-quickstart"
data-space=(ctx.space_name)
data-repo=(ctx.repo_name)
data-empty=(if is_empty { "true" } else { "false" })
data-default-branch=(ctx.dto.default_branch) {
h3 class="text-sm font-semibold text-zinc-700" { "Quick start" }
@if is_empty {
div class="rounded bg-white p-3 text-xs text-zinc-600" {
"This repository is empty. Push an existing Git repository to get started."
}
} @else {
div class="rounded bg-white p-3 text-xs text-zinc-600" {
"Repository is initialized. Clone it locally to start contributing."
}
}
@if !clone_url.is_empty() {
pre class="overflow-x-auto rounded bg-zinc-900 p-3 font-mono text-xs text-zinc-100" {
code { "git clone " (clone_url) }
}
pre class="overflow-x-auto rounded bg-zinc-900 p-3 font-mono text-xs text-zinc-100" {
code {
"cd " (ctx.repo_name) "\n"
"touch README.md\n"
"git add README.md\n"
"git commit -m \"first commit\"\n"
"git push -u origin " (ctx.dto.default_branch)
}
}
} @else {
p class="text-xs text-amber-600" data-warn="missing-base-url" {
"Clone URL unavailable. Configure [git].base_url to enable."
}
}
}
};
response_html(markup, 30)
}