mirror of
https://forge.pointfixe.fr/hubert/gitust.git
synced 2026-02-04 12:17:29 +01:00
Compare commits
15 Commits
6d87adb2e6
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 6beae6629f | |||
|
|
ba19d67f66 | ||
|
|
f9bc5b2e39 | ||
|
|
dbba11a416 | ||
|
|
edf6f9f81a | ||
|
|
5f6f7b7efe | ||
|
|
1f4d08aff1 | ||
|
|
f71e8ff1b3 | ||
|
|
4134982739 | ||
|
|
1e7c773c19 | ||
|
|
9832f30360 | ||
|
|
22836e1f3a | ||
|
|
90ea298a61 | ||
|
|
e3008262fc | ||
|
|
abc1f367bb |
@@ -78,4 +78,28 @@ impl GitRepo {
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
pub fn get_revisions_nbr(&self) -> u32 {
|
||||
let mut res = 0;
|
||||
let mut revwalk = self.git2.revwalk().expect("revisions walk is not defined");
|
||||
revwalk.push_head(); // todo : voir ce qu'il faut réellement pousser si on veux avoir TOUS les commits (genre par exemple si le HEAD n'est pas fils de tous les commits)
|
||||
for _ in revwalk{
|
||||
res = res + 1;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
pub fn get_branches_nbr(&self) -> u32 {
|
||||
let mut res = 0;
|
||||
for _ in self.git2.branches(Some(BranchType::Local)){
|
||||
res = res + 1;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
pub fn get_tags_nbr(&self) -> u32 {
|
||||
let mut res = 0;
|
||||
self.git2.tag_foreach(|_,_| {res = res + 1;true});
|
||||
res
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
34
src/main.rs
34
src/main.rs
@@ -9,14 +9,14 @@ use actix_files::Files;
|
||||
use actix_session::{CookieSession, Session};
|
||||
use actix_web::{App, Error, get, HttpMessage, HttpRequest, HttpResponse, HttpServer, post, Responder};
|
||||
use actix_web::client::PayloadError;
|
||||
use actix_web::dev::ServiceRequest;
|
||||
use actix_web::dev::{ServiceRequest, Service};
|
||||
use actix_web::http::{header, StatusCode};
|
||||
use actix_web::http::header::Header;
|
||||
use actix_web::http::header::IntoHeaderValue;
|
||||
use actix_web::middleware::Logger;
|
||||
use actix_web::web as webx;
|
||||
use actix_web::web::{Buf, Bytes};
|
||||
use actix_web_httpauth::extractors::AuthenticationError;
|
||||
use actix_web_httpauth::extractors::{AuthenticationError, AuthExtractor};
|
||||
use actix_web_httpauth::extractors::basic::{BasicAuth, Config};
|
||||
use actix_web_httpauth::headers::authorization::{Authorization, Basic};
|
||||
use actix_web_httpauth::middleware::HttpAuthentication;
|
||||
@@ -41,6 +41,8 @@ use crate::gitust::Gitust;
|
||||
use crate::ite::SuperIterator;
|
||||
use crate::reader::ToStream;
|
||||
use crate::writer::Writer;
|
||||
use crate::web::repo::GitWebQ;
|
||||
use crate::webutils::auth::{TestValidator, AuthValidator, User};
|
||||
|
||||
mod git;
|
||||
mod ite;
|
||||
@@ -150,6 +152,11 @@ fn validate_credentials(user_id: &str, user_password: Option<&str>) -> Result<bo
|
||||
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Authentication failed!"));
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LoginQ {
|
||||
login: Option<bool>,
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
@@ -172,7 +179,28 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(hello_test)
|
||||
.service(index)
|
||||
.service(askama)
|
||||
.service(repo::git_main)
|
||||
.service(
|
||||
webx::resource("/git/{owner}/{repo}.git")
|
||||
.wrap_fn(|req, serv| {
|
||||
let query : Result<webx::Query<LoginQ>, _> = webx::Query::from_query(req.query_string());
|
||||
let authFut = BasicAuth::from_service_request(&req);
|
||||
let resFut = serv.call(req);
|
||||
async {
|
||||
let webx::Query(LoginQ{login}) = query?;
|
||||
match login {
|
||||
Some(true) => {
|
||||
let auth = authFut.await?;
|
||||
match TestValidator.check_basic(&auth) {
|
||||
None => {Err(AuthenticationError::from(Config::default().realm("auth")).into())}
|
||||
Some(_) => {resFut.await}
|
||||
}
|
||||
}
|
||||
_ => {resFut.await}
|
||||
}
|
||||
}
|
||||
})
|
||||
.route(webx::get().to(repo::git_main::<auth::TestValidator>))
|
||||
)
|
||||
.service(hello_session)
|
||||
.service(chunk)
|
||||
//.service(git_proto)
|
||||
|
||||
112
src/web/repo.rs
112
src/web/repo.rs
@@ -1,3 +1,4 @@
|
||||
use crate::auth::AuthValidator;
|
||||
use std::path::Path;
|
||||
|
||||
use actix_web::web;
|
||||
@@ -16,6 +17,7 @@ use crate::gitutils::gitdir::GitDir;
|
||||
use crate::gitutils::gitfile::GitFile;
|
||||
use crate::gitutils::gitrepo::GitRepo;
|
||||
use crate::ite::SuperIterator;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
struct User {
|
||||
name : String,
|
||||
@@ -37,58 +39,122 @@ pub enum Entry {
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "git.html")]
|
||||
pub struct GitMainTemplate<TS, ROOT>
|
||||
pub struct GitMainTemplate<TS, BREADCRUMB>
|
||||
where for <'a> &'a TS : IntoIterator<Item = &'a Entry>,
|
||||
for <'a> &'a ROOT : IntoIterator<Item = &'a (String, String)>,
|
||||
for <'a> &'a BREADCRUMB: IntoIterator<Item = &'a (String, String)>,
|
||||
{
|
||||
repo : Repository,
|
||||
browse : TS,
|
||||
root : ROOT,
|
||||
query : GitWebQ,
|
||||
breadcrumb: BREADCRUMB,
|
||||
user_opt : Option<User>,
|
||||
revisions_nbr : u32,
|
||||
branches_nbr : u32,
|
||||
tags_nbr : u32,
|
||||
size : String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub struct GitWebQ {
|
||||
commit: Option<String>,
|
||||
path: Option<String>,
|
||||
branch: Option<String>,
|
||||
}
|
||||
|
||||
#[get("/git/{owner}/{repo}.git")]
|
||||
pub async fn git_main(
|
||||
impl GitWebQ {
|
||||
fn clone_with_commit(&self, commit : Option<String>) -> GitWebQ {
|
||||
let mut res = self.clone();
|
||||
res.commit = commit;
|
||||
return res;
|
||||
}
|
||||
|
||||
fn clone_with_path(&self, path : Option<String>) -> GitWebQ {
|
||||
let mut res = self.clone();
|
||||
res.path = path;
|
||||
return res;
|
||||
}
|
||||
|
||||
fn clone_with_branch(&self, branch : Option<String>) -> GitWebQ {
|
||||
let mut res = self.clone();
|
||||
res.branch = branch;
|
||||
return res;
|
||||
}
|
||||
|
||||
fn root_query(&self) -> GitWebQ {
|
||||
self.clone_with_path(None)
|
||||
}
|
||||
|
||||
fn add_path(&self, name : &&String) -> GitWebQ {
|
||||
match &self.path {
|
||||
None => {self.clone_with_path(Some(name.to_string()))}
|
||||
Some(path) => {if path.ends_with("/") {
|
||||
self.clone_with_path(Some(path.clone() + name))
|
||||
} else {
|
||||
self.clone_with_path(Some(path.clone() + "/" + name))
|
||||
}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for GitWebQ{
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "?{}{}{}",
|
||||
match &self.commit {
|
||||
None => {"".to_string()}
|
||||
Some(c) => {format!("commit={}", c)}
|
||||
},
|
||||
match &self.path {
|
||||
None => {"".to_string()}
|
||||
Some(p) => {format!("&path={}", p)}
|
||||
},
|
||||
match &self.branch {
|
||||
None => {"".to_string()}
|
||||
Some(b) => {format!("&branch={}", b)}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
//#[get("/git/{owner}/{repo}.git")]
|
||||
pub async fn git_main<T : AuthValidator>(
|
||||
web::Path((ownername, reponame)): web::Path<(String, String)>,
|
||||
web::Query(GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt}) : web::Query<GitWebQ>,
|
||||
query : web::Query<GitWebQ>,
|
||||
gitust : web::Data<Gitust>,
|
||||
auth : Option<basic::BasicAuth>,
|
||||
validator : web::Data<T>,
|
||||
//auth : BasicAuth,
|
||||
) -> Result<GitMainTemplate<Vec<Entry>, Vec<(String, String)>>, error::Error> {
|
||||
// let authtorization = auth.ok_or(error::Error::Unauthorized("safe repo".to_string()))?;
|
||||
let rootname = match pathopt {
|
||||
//let authorization = auth.and_then(|cred| validator.check_basic(&cred)).ok_or(error::Error::Unauthorized("safe repo".to_string()))?;
|
||||
let web::Query(query_struct) = query;
|
||||
// let GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt} = query_struct;
|
||||
let rootname = match &query_struct.path {
|
||||
None => {"".to_string()}
|
||||
Some(s) => {
|
||||
if s.starts_with("/") {
|
||||
(&s[1..]).to_string()
|
||||
} else {
|
||||
s
|
||||
s.clone()
|
||||
}
|
||||
}
|
||||
};
|
||||
let commit = match commitnameopt {
|
||||
None => {match branchopt {
|
||||
let commit = match &query_struct.commit {
|
||||
None => {match &query_struct.branch {
|
||||
None => {GitRef::Head}
|
||||
Some(s) => {GitRef::Branch(s)}
|
||||
Some(s) => {GitRef::Branch(s.clone())}
|
||||
}}
|
||||
Some(s) => {GitRef::Commit(s)}
|
||||
Some(s) => {GitRef::Commit(s.clone())}
|
||||
};
|
||||
let owner = Owner { name : ownername};
|
||||
let repo = Repository {name : reponame, owner};
|
||||
// let user = User { name : "Hubert".to_string()};
|
||||
let user = auth.map(|auth| User{name : auth.user_id().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)
|
||||
let path = rootname.split("/").map_accum(query_struct.clone_with_path(Some("".to_string())), |direname, b| {
|
||||
let newpath = (&b.path).as_ref().map(|path| {path.clone() + "/" + direname});
|
||||
let newb = b.clone_with_path(newpath);
|
||||
((direname.to_string(), format!("{}", newb)), newb)
|
||||
}).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()))?;
|
||||
@@ -106,5 +172,17 @@ pub async fn git_main(
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(GitMainTemplate { repo, browse : entries, root : path, user_opt : user})
|
||||
Ok(GitMainTemplate {
|
||||
repo,
|
||||
browse : entries,
|
||||
query : query_struct,
|
||||
breadcrumb: path,
|
||||
user_opt : user,
|
||||
revisions_nbr : gitrepo.get_revisions_nbr(),
|
||||
branches_nbr : gitrepo.get_branches_nbr(),
|
||||
tags_nbr : gitrepo.get_tags_nbr(),
|
||||
size : "16 KiB".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,19 +1,32 @@
|
||||
use actix_session::Session;
|
||||
use actix_web::Error;
|
||||
use actix_web_httpauth::extractors::basic::BasicAuth;
|
||||
|
||||
pub trait AuthValidator {
|
||||
fn check_user(&self, name : &String, pwd : &String) -> Option<User>;
|
||||
fn check_user(&self, name : &String, pwd : &String) -> bool;
|
||||
|
||||
fn check_basic(&self, basic : &BasicAuth) -> Option<User> {
|
||||
basic.password().and_then(|pwd| self.check_user(&basic.user_id().to_string(), &pwd.to_string()))
|
||||
match basic.password() {
|
||||
Option::Some(pwd) => {
|
||||
let username = basic.user_id().to_string();
|
||||
if self.check_user(&username, &pwd.to_string()) {
|
||||
Some(User(username))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Option::None => {None}
|
||||
}
|
||||
//basic.password().and_then(|pwd| self.check_user(&basic.user_id().to_string(), &pwd.to_string()))
|
||||
}
|
||||
|
||||
fn check_session(&self, session : &Session) -> Option<User> {
|
||||
let result = session.get::<String>("user");
|
||||
match result {
|
||||
Ok(username) => {username.map(|u| User(u))}
|
||||
Err(e) => {None}
|
||||
Err(e) => {
|
||||
println!("{}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,12 +34,8 @@ pub trait AuthValidator {
|
||||
pub struct TestValidator;
|
||||
|
||||
impl AuthValidator for TestValidator {
|
||||
fn check_user(&self, name: &String, pwd: &String) -> Option<User> {
|
||||
if pwd.eq(&(name.clone() + "pwd")) {
|
||||
Some(User(name.clone()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
fn check_user(&self, name: &String, pwd: &String) -> bool {
|
||||
pwd.eq(&(name.clone() + "pwd"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
</div>
|
||||
</div>
|
||||
{% when None %}
|
||||
<a href="#" class="w3-bar-item w3-button w3-right"><i class="fa fa-sign-in"></i> Log in</a>
|
||||
<a href="?login=true" class="w3-bar-item w3-button w3-right"><i class="fa fa-sign-in"></i> Log in</a>
|
||||
{% endmatch %}
|
||||
</div>
|
||||
{% block content %}{% endblock %}
|
||||
|
||||
@@ -26,16 +26,16 @@
|
||||
|
||||
<div class="w3-panel w3-border w3-cell-row">
|
||||
<div class="w3-center w3-cell">
|
||||
<i class="fa fa-history"></i> 112 revisions
|
||||
<i class="fa fa-history"></i> {{ revisions_nbr }} revisions
|
||||
</div>
|
||||
<div class="w3-center w3-cell">
|
||||
<i class="fa fa-code-branch"></i> 10 branches
|
||||
<i class="fa fa-code-branch"></i> {{ branches_nbr }} branches
|
||||
</div>
|
||||
<div class="w3-center w3-cell">
|
||||
<i class="fa fa-tag"></i> 5 tags
|
||||
<i class="fa fa-tag"></i> {{ tags_nbr }} tags
|
||||
</div>
|
||||
<div class="w3-center w3-cell">
|
||||
<i class="fa fa-database"></i> 10 KiB
|
||||
<i class="fa fa-database"></i> {{ size }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -60,8 +60,8 @@
|
||||
<div class="w3-cell-row">
|
||||
<div class="w3-cell">
|
||||
<ul class="breadcrumb">
|
||||
<li><a href="#">{{repo.name}}</a></li>
|
||||
{% for (dir, prev) in root %}
|
||||
<li><a href="{{query.root_query()}}">{{repo.name}}.git</a></li>
|
||||
{% for (dir, prev) in breadcrumb %}
|
||||
<li><a href="{{prev}}">{{dir}}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
@@ -77,7 +77,7 @@
|
||||
{% for entry in browse %}
|
||||
{% match entry %}
|
||||
{% when Entry::Dir with (name) %}
|
||||
<li><i class="fa fa-folder"></i> {{ name }}</li>
|
||||
<li><i class="fa fa-folder"></i> <a href="{{query.add_path(name)}}">{{ name }}</a></li>
|
||||
{% when Entry::File with (name) %}
|
||||
<li><i class="fa fa-file"></i> {{ name }}</li>
|
||||
{% endmatch %}
|
||||
|
||||
Reference in New Issue
Block a user