save refacto

This commit is contained in:
Hubert 2021-07-20 06:46:24 +02:00
parent 510bbe7381
commit 0875a78cf8
3 changed files with 111 additions and 91 deletions

View File

@ -32,6 +32,7 @@ use gitutils::gitcommit::GitRef;
use gitutils::gitdir::GitDir;
use gitutils::gitfile::GitFile;
use gitutils::gitrepo::GitRepo;
use web::repo;
use crate::git::GitBrowseEntry;
use crate::gitust::Gitust;
@ -54,36 +55,6 @@ struct HelloTemplate<'a> {
name: &'a str,
}
struct User {
name : String,
}
struct Owner {
name : String,
}
struct Repository {
name : String,
owner : Owner,
}
enum Entry {
Dir(String),
File(String),
}
#[derive(Template)]
#[template(path = "git.html")]
struct GitMainTemplate<TS, ROOT>
where for <'a> &'a TS : IntoIterator<Item = &'a Entry>,
for <'a> &'a ROOT : IntoIterator<Item = &'a (String, String)>,
{
repo : Repository,
browse : TS,
root : ROOT,
user_opt : Option<User>,
}
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Bonjour monde !")
@ -119,14 +90,6 @@ async fn hello_auth(req : HttpRequest) -> impl Responder {
}
#[derive(Deserialize)]
struct GitWebQ {
commit: Option<String>,
path: Option<String>,
branch: Option<String>,
}
#[get("/chunk")]
async fn chunk() -> HttpResponse {
let (tx, rx) = actix_utils::mpsc::channel::<Result<Bytes, Error>>();
@ -145,57 +108,6 @@ async fn chunk() -> HttpResponse {
HttpResponse::Ok().streaming(rx)
}
#[get("/git/{owner}/{repo}.git")]
async fn git_main(
webx::Path((ownername, reponame)): webx::Path<(String, String)>,
webx::Query(GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt}) : webx::Query<GitWebQ>,
gitust : webx::Data<Gitust>,
auth : Option<BasicAuth>,
//auth : BasicAuth,
) -> Result<GitMainTemplate<Vec<Entry>, Vec<(String, String)>>, error::Error> {
let rootname = match pathopt {
None => {"".to_string()}
Some(s) => {
if s.starts_with("/") {
(&s[1..]).to_string()
} else {
s
}
}
};
let commit = match commitnameopt {
None => {match branchopt {
None => {GitRef::Head}
Some(s) => {GitRef::Branch(s)}
}}
Some(s) => {GitRef::Commit(s)}
};
let owner = Owner { name : ownername};
let repo = Repository {name : reponame, owner};
let user = User { name : "Hubert".to_string()};
// il faut ajouter le commit/branch dans la query
let path = rootname.split("/").map_accum("/git/".to_string() + &repo.owner.name + "/" + &repo.name, |str_ref, b| {
let href = b + "/" + str_ref;
((str_ref.to_string(), href.clone()), href)
}).collect();
let gitrepo = GitRepo::new(format!("{}/{}/{}.git", &gitust.repo_root_path, &repo.owner.name, &repo.name).as_str())?;
let root = gitrepo.get_tree(&commit, Path::new(rootname.as_str()))?;
let browse = gitrepo.browse(&root).await?;
let mut entries : Vec<Entry> = Vec::new();
for entry in browse {
match entry {
GitBrowseEntry::EGitFile(GitFile(fullname)) => {
let osstr = fullname.file_name().ok_or(git2::Error::from_str("file name not defined"))?;
entries.push(Entry::File(format!("{}",osstr.to_string_lossy())));
}
GitBrowseEntry::EGitDir(GitDir{fullname, .. }) => {
let osstr = fullname.file_name().ok_or(git2::Error::from_str("file name not defined"))?;
entries.push(Entry::Dir(format!("{}",osstr.to_string_lossy())));
}
}
}
Ok(GitMainTemplate { repo, browse : entries, root : path, user_opt : Some(user)})
}
//#[get("/git/{owner}/{repo}.git/{path:.*}")]
async fn git_proto(
mut payload : webx::Payload,
@ -342,7 +254,7 @@ async fn main() -> std::io::Result<()> {
.service(hello_test)
.service(index)
.service(askama)
.service(git_main)
.service(repo::git_main)
.service(hello_session)
.service(chunk)
//.service(git_proto)

View File

@ -1 +1 @@
mod repo;
pub mod repo;

View File

@ -0,0 +1,108 @@
use std::path::Path;
use actix_web::web;
use actix_web_httpauth::extractors::basic::BasicAuth;
use actix_web::get;
use askama_actix::Template;
use serde::Deserialize;
use crate::error;
use crate::git::GitBrowseEntry;
use crate::gitust::Gitust;
use crate::gitutils::gitcommit::GitRef;
use crate::gitutils::gitdir::GitDir;
use crate::gitutils::gitfile::GitFile;
use crate::gitutils::gitrepo::GitRepo;
use crate::ite::SuperIterator;
struct User {
name : String,
}
struct Owner {
name : String,
}
struct Repository {
name : String,
owner : Owner,
}
pub enum Entry {
Dir(String),
File(String),
}
#[derive(Template)]
#[template(path = "git.html")]
pub struct GitMainTemplate<TS, ROOT>
where for <'a> &'a TS : IntoIterator<Item = &'a Entry>,
for <'a> &'a ROOT : IntoIterator<Item = &'a (String, String)>,
{
repo : Repository,
browse : TS,
root : ROOT,
user_opt : Option<User>,
}
#[derive(Deserialize)]
pub struct GitWebQ {
commit: Option<String>,
path: Option<String>,
branch: Option<String>,
}
#[get("/git/{owner}/{repo}.git")]
pub async fn git_main(
web::Path((ownername, reponame)): web::Path<(String, String)>,
web::Query(GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt}) : web::Query<GitWebQ>,
gitust : web::Data<Gitust>,
auth : Option<BasicAuth>,
//auth : BasicAuth,
) -> Result<GitMainTemplate<Vec<Entry>, Vec<(String, String)>>, error::Error> {
let rootname = match pathopt {
None => {"".to_string()}
Some(s) => {
if s.starts_with("/") {
(&s[1..]).to_string()
} else {
s
}
}
};
let commit = match commitnameopt {
None => {match branchopt {
None => {GitRef::Head}
Some(s) => {GitRef::Branch(s)}
}}
Some(s) => {GitRef::Commit(s)}
};
let owner = Owner { name : ownername};
let repo = Repository {name : reponame, owner};
let user = User { name : "Hubert".to_string()};
// il faut ajouter le commit/branch dans la query
let path = rootname.split("/").map_accum("/git/".to_string() + &repo.owner.name + "/" + &repo.name, |str_ref, b| {
let href = b + "/" + str_ref;
((str_ref.to_string(), href.clone()), href)
}).collect();
let gitrepo = GitRepo::new(format!("{}/{}/{}.git", &gitust.repo_root_path, &repo.owner.name, &repo.name).as_str())?;
let root = gitrepo.get_tree(&commit, Path::new(rootname.as_str()))?;
let browse = gitrepo.browse(&root).await?;
let mut entries : Vec<Entry> = Vec::new();
for entry in browse {
match entry {
GitBrowseEntry::EGitFile(GitFile(fullname)) => {
let osstr = fullname.file_name().ok_or(git2::Error::from_str("file name not defined"))?;
entries.push(Entry::File(format!("{}",osstr.to_string_lossy())));
}
GitBrowseEntry::EGitDir(GitDir{fullname, .. }) => {
let osstr = fullname.file_name().ok_or(git2::Error::from_str("file name not defined"))?;
entries.push(Entry::Dir(format!("{}",osstr.to_string_lossy())));
}
}
}
Ok(GitMainTemplate { repo, browse : entries, root : path, user_opt : Some(user)})
}