Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
d609f9bf43 | |||
6bc783451e | |||
21243e6cfb | |||
05736ee3ba | |||
d23588172b | |||
ce77854754 | |||
70e83d8cee | |||
284e41c591 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -761,7 +761,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hoc"
|
name = "hoc"
|
||||||
version = "0.8.0"
|
version = "0.9.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"actix-web 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"badge 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"badge 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "hoc"
|
name = "hoc"
|
||||||
version = "0.8.0"
|
version = "0.9.1"
|
||||||
authors = ["Valentin Brandl <vbrandl@riseup.net>"]
|
authors = ["Valentin Brandl <vbrandl@riseup.net>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
|
@ -20,8 +20,14 @@ The API is as simple as
|
|||||||
https://<host>/<service>/<user>/<repo>
|
https://<host>/<service>/<user>/<repo>
|
||||||
```
|
```
|
||||||
|
|
||||||
where `<service>` is one of `gitub`, `gitlab` or `bitbucket`.
|
where `<service>` is one of `gitub`, `gitlab` or `bitbucket`. The HoC data can also be received as JSON by appending
|
||||||
|
`/json` to the reuqest path:
|
||||||
|
|
||||||
|
```
|
||||||
|
https://<host>/<service>/<user>/<repo>/json
|
||||||
|
```
|
||||||
|
|
||||||
|
There is also an overview page available via `https://<host>/view/<service>/<user>/<repo>`
|
||||||
|
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
|
86
src/main.rs
86
src/main.rs
@ -17,7 +17,7 @@ mod service;
|
|||||||
mod statics;
|
mod statics;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cache::CacheState,
|
cache::{Cache, CacheState},
|
||||||
error::{Error, Result},
|
error::{Error, Result},
|
||||||
service::{Bitbucket, FormService, GitHub, Gitlab, Service},
|
service::{Bitbucket, FormService, GitHub, Gitlab, Service},
|
||||||
statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO},
|
statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO},
|
||||||
@ -150,46 +150,55 @@ where
|
|||||||
T: Service,
|
T: Service,
|
||||||
F: Fn(HocResult) -> Result<HttpResponse>,
|
F: Fn(HocResult) -> Result<HttpResponse>,
|
||||||
{
|
{
|
||||||
hoc_request::<T>(state, data).and_then(mapper)
|
futures::future::result(Ok(()))
|
||||||
|
.and_then(move |_| {
|
||||||
|
let repo = format!("{}/{}", data.0.to_lowercase(), data.1.to_lowercase());
|
||||||
|
let service_path = format!("{}/{}", T::domain(), repo);
|
||||||
|
let path = format!("{}/{}", state.repos, service_path);
|
||||||
|
let file = Path::new(&path);
|
||||||
|
let url = format!("https://{}", service_path);
|
||||||
|
if !file.exists() {
|
||||||
|
if !remote_exists(&url)? {
|
||||||
|
warn!("Repository does not exist: {}", url);
|
||||||
|
return Ok(HocResult::NotFound);
|
||||||
|
}
|
||||||
|
info!("Cloning {} for the first time", url);
|
||||||
|
create_dir_all(file)?;
|
||||||
|
let repo = Repository::init_bare(file)?;
|
||||||
|
repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?;
|
||||||
|
repo.remote_set_url("origin", &url)?;
|
||||||
|
REPO_COUNT.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pull(&path)?;
|
||||||
|
let (hoc, head) = hoc(&service_path, &state.repos, &state.cache)?;
|
||||||
|
let hoc_pretty = match NumberPrefix::decimal(hoc as f64) {
|
||||||
|
Standalone(hoc) => hoc.to_string(),
|
||||||
|
Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
|
||||||
|
};
|
||||||
|
Ok(HocResult::Hoc {
|
||||||
|
hoc,
|
||||||
|
hoc_pretty,
|
||||||
|
head,
|
||||||
|
url,
|
||||||
|
repo,
|
||||||
|
service_path,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.and_then(mapper)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hoc_request<T: Service>(
|
fn json_hoc<T: Service>(
|
||||||
state: web::Data<Arc<State>>,
|
state: web::Data<Arc<State>>,
|
||||||
data: web::Path<(String, String)>,
|
data: web::Path<(String, String)>,
|
||||||
) -> impl Future<Item = HocResult, Error = Error> {
|
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||||
futures::future::result(Ok(())).and_then(move |_| {
|
let mapper = |r| match r {
|
||||||
let repo = format!("{}/{}", data.0.to_lowercase(), data.1.to_lowercase());
|
HocResult::NotFound => p404(),
|
||||||
let service_path = format!("{}/{}", T::domain(), repo);
|
HocResult::Hoc { hoc, head, .. } => Ok(HttpResponse::Ok().json(Cache {
|
||||||
let path = format!("{}/{}", state.repos, service_path);
|
head: head.into(),
|
||||||
let file = Path::new(&path);
|
count: hoc,
|
||||||
let url = format!("https://{}", service_path);
|
})),
|
||||||
if !file.exists() {
|
};
|
||||||
if !remote_exists(&url)? {
|
handle_hoc_request::<T, _>(state, data, mapper)
|
||||||
warn!("Repository does not exist: {}", url);
|
|
||||||
return Ok(HocResult::NotFound);
|
|
||||||
}
|
|
||||||
info!("Cloning {} for the first time", url);
|
|
||||||
create_dir_all(file)?;
|
|
||||||
let repo = Repository::init_bare(file)?;
|
|
||||||
repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?;
|
|
||||||
repo.remote_set_url("origin", &url)?;
|
|
||||||
REPO_COUNT.fetch_add(1, Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
pull(&path)?;
|
|
||||||
let (hoc, head) = hoc(&service_path, &state.repos, &state.cache)?;
|
|
||||||
let hoc_pretty = match NumberPrefix::decimal(hoc as f64) {
|
|
||||||
Standalone(hoc) => hoc.to_string(),
|
|
||||||
Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
|
|
||||||
};
|
|
||||||
Ok(HocResult::Hoc {
|
|
||||||
hoc,
|
|
||||||
hoc_pretty,
|
|
||||||
head,
|
|
||||||
url,
|
|
||||||
repo,
|
|
||||||
service_path,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calculate_hoc<T: Service>(
|
fn calculate_hoc<T: Service>(
|
||||||
@ -331,6 +340,9 @@ fn main() -> Result<()> {
|
|||||||
.service(web::resource("/github/{user}/{repo}").to_async(calculate_hoc::<GitHub>))
|
.service(web::resource("/github/{user}/{repo}").to_async(calculate_hoc::<GitHub>))
|
||||||
.service(web::resource("/gitlab/{user}/{repo}").to_async(calculate_hoc::<Gitlab>))
|
.service(web::resource("/gitlab/{user}/{repo}").to_async(calculate_hoc::<Gitlab>))
|
||||||
.service(web::resource("/bitbucket/{user}/{repo}").to_async(calculate_hoc::<Bitbucket>))
|
.service(web::resource("/bitbucket/{user}/{repo}").to_async(calculate_hoc::<Bitbucket>))
|
||||||
|
.service(web::resource("/github/{user}/{repo}/json").to_async(json_hoc::<GitHub>))
|
||||||
|
.service(web::resource("/gitlab/{user}/{repo}/json").to_async(json_hoc::<Gitlab>))
|
||||||
|
.service(web::resource("/bitbucket/{user}/{repo}/json").to_async(json_hoc::<Bitbucket>))
|
||||||
.service(web::resource("/view/github/{user}/{repo}").to_async(overview::<GitHub>))
|
.service(web::resource("/view/github/{user}/{repo}").to_async(overview::<GitHub>))
|
||||||
.service(web::resource("/view/gitlab/{user}/{repo}").to_async(overview::<Gitlab>))
|
.service(web::resource("/view/gitlab/{user}/{repo}").to_async(overview::<Gitlab>))
|
||||||
.service(web::resource("/view/bitbucket/{user}/{repo}").to_async(overview::<Bitbucket>))
|
.service(web::resource("/view/bitbucket/{user}/{repo}").to_async(overview::<Bitbucket>))
|
||||||
|
@ -36,6 +36,10 @@
|
|||||||
<li>
|
<li>
|
||||||
<small>HoC v@version_info.version - <a href="https://github.com/vbrandl/hoc/commit/@version_info.commit">@version_info.commit</a></small>
|
<small>HoC v@version_info.version - <a href="https://github.com/vbrandl/hoc/commit/@version_info.commit">@version_info.commit</a></small>
|
||||||
</li>
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<small>Currently serving @repo_count repositories</small>
|
<small>Currently serving @repo_count repositories</small>
|
||||||
</li>
|
</li>
|
||||||
|
@ -45,6 +45,21 @@ would render this badge:
|
|||||||
alt="example badge" /></a>
|
alt="example badge" /></a>
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
You can also request the HoC as JSON by appending <code>/json</code> to the request path. This will return a JSON
|
||||||
|
object with two fields: <code>count</code> and <code>head</code> with count being the HoC value and head being the
|
||||||
|
commit ref of <code>HEAD</code>. Requesting
|
||||||
|
<a href="https://@domain/github/vbrandl/hoc/json">https://@domain/github/vbrandl/hoc/json</a> might return something
|
||||||
|
along the lines of
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
{
|
||||||
|
"head" : "05736ee3ba256ec9a7227c436aef2bf43db109ab",
|
||||||
|
"count": 7582
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
<h2>Badge Generator</h2>
|
<h2>Badge Generator</h2>
|
||||||
|
|
||||||
<form method="post" action="/generate">
|
<form method="post" action="/generate">
|
||||||
|
Reference in New Issue
Block a user