From 0875a78cf8c6ea9a9e04b6f86337e49c66fd028d Mon Sep 17 00:00:00 2001 From: Hubert Date: Tue, 20 Jul 2021 06:46:24 +0200 Subject: [PATCH] save refacto --- src/main.rs | 92 +---------------------------------------- src/web/mod.rs | 2 +- src/web/repo.rs | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 91 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9d7c4b6..62f774e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 -where for <'a> &'a TS : IntoIterator, - for <'a> &'a ROOT : IntoIterator, -{ - repo : Repository, - browse : TS, - root : ROOT, - user_opt : Option, -} - #[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, - path: Option, - branch: Option, -} - - #[get("/chunk")] async fn chunk() -> HttpResponse { let (tx, rx) = actix_utils::mpsc::channel::>(); @@ -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, - gitust : webx::Data, - auth : Option, - //auth : BasicAuth, -) -> Result, 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 = 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) diff --git a/src/web/mod.rs b/src/web/mod.rs index ffdecad..35eb3df 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -1 +1 @@ -mod repo; \ No newline at end of file +pub mod repo; \ No newline at end of file diff --git a/src/web/repo.rs b/src/web/repo.rs index e69de29..b94f562 100644 --- a/src/web/repo.rs +++ b/src/web/repo.rs @@ -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 +where for <'a> &'a TS : IntoIterator, + for <'a> &'a ROOT : IntoIterator, +{ + repo : Repository, + browse : TS, + root : ROOT, + user_opt : Option, +} + + +#[derive(Deserialize)] +pub struct GitWebQ { + commit: Option, + path: Option, + branch: Option, +} + +#[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, + gitust : web::Data, + auth : Option, + //auth : BasicAuth, +) -> Result, 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 = 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)}) +}