Compare commits
65 Commits
Author | SHA1 | Date | |
---|---|---|---|
b8e454762e | |||
268734efdd | |||
c71925f61e | |||
c5bbd14a05 | |||
a239a3f80b | |||
00b4e60341 | |||
ed1cafafd0 | |||
bdb10fd54a | |||
1f01c3b964 | |||
3f94789b8b | |||
e4f8604861 | |||
25a179dad6 | |||
6ee44f7ba3 | |||
e9ebbee957 | |||
5211481226 | |||
fd230db0cb | |||
92a95f33d4 | |||
7e13f93ee3 | |||
36afc8732e | |||
abeed1f971 | |||
03a0743517 | |||
cbfd56b33a | |||
9c5208d6e4 | |||
92d7b6668a | |||
97510fa325 | |||
65ba104de1 | |||
9599d934a2 | |||
61e2dd174a | |||
fe00cddd47 | |||
9b73c99922 | |||
ed3da3a2c3 | |||
504476b145 | |||
9bc1b42750 | |||
140265b713 | |||
4446d9b879 | |||
ac8ba338bb | |||
d609f9bf43 | |||
6bc783451e | |||
21243e6cfb | |||
05736ee3ba | |||
d23588172b | |||
ce77854754 | |||
70e83d8cee | |||
284e41c591 | |||
ec8bbe0325 | |||
8d4de48c11 | |||
eb718990ec | |||
60dc242e5a | |||
615460c87b | |||
ba6fc7d394 | |||
5551c6f3e4 | |||
38399b0bcd | |||
ad4d87ca2f | |||
137f264bdf | |||
8cfe84c763 | |||
787ff55444 | |||
3901fe9e9c | |||
e0b6ee69f1 | |||
75398a3613 | |||
a944e8b149 | |||
d9faf25648 | |||
d2977eee32 | |||
569ce3a457 | |||
a77962732e | |||
6438bbba82 |
948
Cargo.lock
generated
948
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
21
Cargo.toml
21
Cargo.toml
@ -1,28 +1,27 @@
|
||||
[package]
|
||||
name = "hoc"
|
||||
version = "0.5.0"
|
||||
version = "0.10.0"
|
||||
authors = ["Valentin Brandl <vbrandl@riseup.net>"]
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0.0-beta.2"
|
||||
actix-web = "1.0.3"
|
||||
badge = "0.2.0"
|
||||
bytes = "0.4.12"
|
||||
futures = "0.1.26"
|
||||
git2 = "0.8.0"
|
||||
futures = "0.1.28"
|
||||
git2 = "0.9.1"
|
||||
lazy_static = "1.3.0"
|
||||
log = "0.4.6"
|
||||
log4rs = "0.8.3"
|
||||
number_prefix = "0.3.0"
|
||||
openssl-probe = "0.1.2"
|
||||
# pretty_env_logger = "0.3.0"
|
||||
reqwest = "0.9.16"
|
||||
serde = "1.0.90"
|
||||
serde_derive = "1.0.90"
|
||||
serde_json = "1.0.39"
|
||||
structopt = "0.2.15"
|
||||
reqwest = "0.9.17"
|
||||
serde = "1.0.93"
|
||||
serde_derive = "1.0.94"
|
||||
serde_json = "1.0.40"
|
||||
structopt = "0.2.18"
|
||||
|
||||
[build-dependencies]
|
||||
ructe = "0.6.2"
|
||||
ructe = "0.6.4"
|
||||
vergen = "3.0.4"
|
||||
|
@ -29,10 +29,14 @@ FROM alpine:latest
|
||||
|
||||
RUN apk --no-cache add --update git
|
||||
|
||||
RUN adduser -D hoc
|
||||
WORKDIR /home/hoc
|
||||
USER hoc
|
||||
|
||||
# once we don't need a git binary anymore, this should be enough
|
||||
# FROM scratch
|
||||
# COPY --from=linuxkit/ca-certificates:v0.7 / /
|
||||
|
||||
COPY --from=builder /home/rust/src/target/x86_64-unknown-linux-musl/release/hoc /hoc
|
||||
COPY --from=builder /home/rust/src/target/x86_64-unknown-linux-musl/release/hoc .
|
||||
|
||||
ENTRYPOINT ["/hoc"]
|
||||
ENTRYPOINT ["/home/hoc/hoc"]
|
||||
|
@ -20,8 +20,14 @@ The API is as simple as
|
||||
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
|
||||
|
||||
|
@ -2,11 +2,10 @@ version: "2"
|
||||
|
||||
services:
|
||||
hoc:
|
||||
# build: .
|
||||
# image: local/hoc:latest
|
||||
image: registry.gitlab.com/vbrandl/hoc:latest
|
||||
image: vbrandl/hits-of-code:latest
|
||||
volumes:
|
||||
- ./repos:/repos
|
||||
- ./cache:/cache
|
||||
ports:
|
||||
- "127.0.0.1:8080:8080"
|
||||
- ./repos:/home/hoc/repos
|
||||
- ./cache:/home/hoc/cache
|
||||
# ports:
|
||||
# - "127.0.0.1:8080:8080"
|
||||
restart: always
|
||||
|
9
scripts/list.sh
Executable file
9
scripts/list.sh
Executable file
@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
set -e
|
||||
|
||||
DIR=${1:-repos}
|
||||
|
||||
find "$DIR" -mindepth 3 -maxdepth 3 -type d \
|
||||
| sed -e "s/$DIR/https:\//g" \
|
||||
| sort
|
26
src/cache.rs
26
src/cache.rs
@ -1,4 +1,4 @@
|
||||
use crate::Error;
|
||||
use crate::error::{Error, Result};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::{create_dir_all, File, OpenOptions},
|
||||
@ -9,7 +9,7 @@ use std::{
|
||||
/// Enum to indicate the state of the cache
|
||||
pub(crate) enum CacheState<'a> {
|
||||
/// Current head and cached head are the same
|
||||
Current(u64),
|
||||
Current { count: u64, commits: u64 },
|
||||
/// Cached head is older than current head
|
||||
Old(Cache<'a>),
|
||||
/// No cache was found
|
||||
@ -17,11 +17,14 @@ pub(crate) enum CacheState<'a> {
|
||||
}
|
||||
|
||||
impl<'a> CacheState<'a> {
|
||||
pub(crate) fn read_from_file(path: impl AsRef<Path>, head: &str) -> Result<CacheState, Error> {
|
||||
pub(crate) fn read_from_file(path: impl AsRef<Path>, head: &str) -> Result<CacheState> {
|
||||
if path.as_ref().exists() {
|
||||
let cache: Cache = serde_json::from_reader(BufReader::new(File::open(path)?))?;
|
||||
if cache.head == head {
|
||||
Ok(CacheState::Current(cache.count))
|
||||
Ok(CacheState::Current {
|
||||
count: cache.count,
|
||||
commits: cache.commits,
|
||||
})
|
||||
} else {
|
||||
Ok(CacheState::Old(cache))
|
||||
}
|
||||
@ -30,26 +33,35 @@ impl<'a> CacheState<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn calculate_new_cache(self, count: u64, head: Cow<'a, str>) -> Cache {
|
||||
pub(crate) fn calculate_new_cache(self, count: u64, commits: u64, head: Cow<'a, str>) -> Cache {
|
||||
match self {
|
||||
CacheState::Old(mut cache) => {
|
||||
cache.head = head;
|
||||
cache.count += count;
|
||||
cache.commits += commits;
|
||||
cache
|
||||
}
|
||||
CacheState::No | CacheState::Current(_) => Cache { head, count },
|
||||
CacheState::No | CacheState::Current { .. } => Cache {
|
||||
head,
|
||||
count,
|
||||
commits,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub(crate) struct Cache<'a> {
|
||||
/// HEAD commit ref
|
||||
pub head: Cow<'a, str>,
|
||||
/// HoC value
|
||||
pub count: u64,
|
||||
/// Number of commits
|
||||
pub commits: u64,
|
||||
}
|
||||
|
||||
impl<'a> Cache<'a> {
|
||||
pub(crate) fn write_to_file(&self, path: impl AsRef<Path>) -> Result<(), Error> {
|
||||
pub(crate) fn write_to_file(&self, path: impl AsRef<Path>) -> Result<()> {
|
||||
create_dir_all(path.as_ref().parent().ok_or(Error::Internal)?)?;
|
||||
serde_json::to_writer(
|
||||
OpenOptions::new()
|
||||
|
@ -46,6 +46,14 @@ pub(crate) struct Opt {
|
||||
)]
|
||||
/// The logfile
|
||||
pub(crate) logfile: PathBuf,
|
||||
#[structopt(subcommand)]
|
||||
pub(crate) migrate: Option<Migration>,
|
||||
}
|
||||
|
||||
#[derive(StructOpt, Debug, Clone, Copy)]
|
||||
pub(crate) enum Migration {
|
||||
#[structopt(name = "migrate-commit-count")]
|
||||
CacheCommitCount,
|
||||
}
|
||||
|
||||
pub(crate) fn init() -> Result<()> {
|
||||
|
24
src/count.rs
Normal file
24
src/count.rs
Normal file
@ -0,0 +1,24 @@
|
||||
use crate::error::Result;
|
||||
use std::{fs::read_dir, path::Path, result::Result as StdResult};
|
||||
|
||||
pub(crate) fn count_repositories<P>(repo_path: P) -> Result<usize>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
Ok(read_dir(repo_path)?
|
||||
.filter_map(StdResult::ok)
|
||||
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
|
||||
.map(|entry| read_dir(entry.path()))
|
||||
.filter_map(StdResult::ok)
|
||||
.flat_map(|dir| {
|
||||
dir.filter_map(StdResult::ok)
|
||||
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
|
||||
})
|
||||
.map(|entry| read_dir(entry.path()))
|
||||
.filter_map(StdResult::ok)
|
||||
.flat_map(|dir| {
|
||||
dir.filter_map(StdResult::ok)
|
||||
.filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
|
||||
})
|
||||
.count())
|
||||
}
|
23
src/error.rs
23
src/error.rs
@ -1,6 +1,9 @@
|
||||
use crate::P500;
|
||||
use crate::{
|
||||
statics::{REPO_COUNT, VERSION_INFO},
|
||||
templates,
|
||||
};
|
||||
use actix_web::{HttpResponse, ResponseError};
|
||||
use std::fmt;
|
||||
use std::{fmt, sync::atomic::Ordering};
|
||||
|
||||
pub(crate) type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
@ -13,6 +16,7 @@ pub(crate) enum Error {
|
||||
Io(std::io::Error),
|
||||
Log(log::SetLoggerError),
|
||||
LogBuilder(log4rs::config::Errors),
|
||||
Parse(std::num::ParseIntError),
|
||||
Serial(serde_json::Error),
|
||||
}
|
||||
|
||||
@ -26,6 +30,7 @@ impl fmt::Display for Error {
|
||||
Error::Io(e) => write!(fmt, "Io({})", e),
|
||||
Error::Log(e) => write!(fmt, "Log({})", e),
|
||||
Error::LogBuilder(e) => write!(fmt, "LogBuilder({})", e),
|
||||
Error::Parse(e) => write!(fmt, "Parse({})", e),
|
||||
Error::Serial(e) => write!(fmt, "Serial({})", e),
|
||||
}
|
||||
}
|
||||
@ -33,15 +38,15 @@ impl fmt::Display for Error {
|
||||
|
||||
impl ResponseError for Error {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
let mut buf = Vec::new();
|
||||
templates::p500(&mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed)).unwrap();
|
||||
HttpResponse::InternalServerError()
|
||||
.content_type("text/html")
|
||||
.body(P500.as_slice())
|
||||
.body(buf)
|
||||
}
|
||||
|
||||
fn render_response(&self) -> HttpResponse {
|
||||
HttpResponse::InternalServerError()
|
||||
.content_type("text/html")
|
||||
.body(P500.as_slice())
|
||||
self.error_response()
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,3 +93,9 @@ impl From<log4rs::config::Errors> for Error {
|
||||
Error::LogBuilder(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::num::ParseIntError> for Error {
|
||||
fn from(err: std::num::ParseIntError) -> Self {
|
||||
Error::Parse(err)
|
||||
}
|
||||
}
|
||||
|
220
src/main.rs
220
src/main.rs
@ -1,3 +1,5 @@
|
||||
#![type_length_limit = "2257138"]
|
||||
|
||||
#[macro_use]
|
||||
extern crate actix_web;
|
||||
#[macro_use]
|
||||
@ -9,15 +11,17 @@ extern crate serde_derive;
|
||||
|
||||
mod cache;
|
||||
mod config;
|
||||
mod count;
|
||||
mod error;
|
||||
mod service;
|
||||
mod statics;
|
||||
|
||||
use crate::{
|
||||
cache::CacheState,
|
||||
config::Migration,
|
||||
error::{Error, Result},
|
||||
service::{Bitbucket, FormService, GitHub, Gitlab, Service},
|
||||
statics::{CLIENT, CSS, FAVICON, INDEX, OPT, P404, P500, VERSION_INFO},
|
||||
statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO},
|
||||
};
|
||||
use actix_web::{
|
||||
error::ErrorBadRequest,
|
||||
@ -26,14 +30,15 @@ use actix_web::{
|
||||
};
|
||||
use badge::{Badge, BadgeOptions};
|
||||
use bytes::Bytes;
|
||||
use futures::{unsync::mpsc, Stream};
|
||||
use futures::{unsync::mpsc, Future, Stream};
|
||||
use git2::Repository;
|
||||
use number_prefix::{NumberPrefix, Prefixed, Standalone};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::create_dir_all,
|
||||
fs::{create_dir_all, read_dir, rename},
|
||||
path::Path,
|
||||
process::Command,
|
||||
sync::atomic::Ordering,
|
||||
sync::Arc,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
@ -52,6 +57,13 @@ struct State {
|
||||
cache: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct JsonResponse<'a> {
|
||||
head: &'a str,
|
||||
count: u64,
|
||||
commits: u64,
|
||||
}
|
||||
|
||||
fn pull(path: impl AsRef<Path>) -> Result<()> {
|
||||
let repo = Repository::open_bare(path)?;
|
||||
let mut origin = repo.find_remote("origin")?;
|
||||
@ -59,17 +71,13 @@ fn pull(path: impl AsRef<Path>) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String)> {
|
||||
fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String, u64)> {
|
||||
let repo_dir = format!("{}/{}", repo_dir, repo);
|
||||
let cache_dir = format!("{}/{}.json", cache_dir, repo);
|
||||
let cache_dir = Path::new(&cache_dir);
|
||||
let head = format!(
|
||||
"{}",
|
||||
Repository::open_bare(&repo_dir)?
|
||||
.head()?
|
||||
.target()
|
||||
.ok_or(Error::Internal)?
|
||||
);
|
||||
let repo = Repository::open_bare(&repo_dir)?;
|
||||
let head = format!("{}", repo.head()?.target().ok_or(Error::Internal)?);
|
||||
let mut arg_commit_count = vec!["rev-list".to_string(), "--count".to_string()];
|
||||
let mut arg = vec![
|
||||
"log".to_string(),
|
||||
"--pretty=tformat:".to_string(),
|
||||
@ -84,16 +92,18 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String)> {
|
||||
];
|
||||
let cache = CacheState::read_from_file(&cache_dir, &head)?;
|
||||
match &cache {
|
||||
CacheState::Current(res) => {
|
||||
CacheState::Current { count, commits } => {
|
||||
info!("Using cache for {}", repo_dir);
|
||||
return Ok((*res, head));
|
||||
return Ok((*count, head, *commits));
|
||||
}
|
||||
CacheState::Old(cache) => {
|
||||
info!("Updating cache for {}", repo_dir);
|
||||
arg.push(format!("{}..HEAD", cache.head));
|
||||
arg_commit_count.push(format!("{}..HEAD", cache.head));
|
||||
}
|
||||
CacheState::No => {
|
||||
info!("Creating cache for {}", repo_dir);
|
||||
arg_commit_count.push("HEAD".to_string());
|
||||
}
|
||||
};
|
||||
arg.push("--".to_string());
|
||||
@ -104,6 +114,13 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String)> {
|
||||
.output()?
|
||||
.stdout;
|
||||
let output = String::from_utf8_lossy(&output);
|
||||
let output_commits = Command::new("git")
|
||||
.args(&arg_commit_count)
|
||||
.current_dir(&repo_dir)
|
||||
.output()?
|
||||
.stdout;
|
||||
let output_commits = String::from_utf8_lossy(&output_commits);
|
||||
let commits: u64 = output_commits.trim().parse()?;
|
||||
let count: u64 = output
|
||||
.lines()
|
||||
.map(|s| {
|
||||
@ -115,10 +132,10 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String)> {
|
||||
})
|
||||
.sum();
|
||||
|
||||
let cache = cache.calculate_new_cache(count, (&head).into());
|
||||
let cache = cache.calculate_new_cache(count, commits, (&head).into());
|
||||
cache.write_to_file(cache_dir)?;
|
||||
|
||||
Ok((cache.count, head))
|
||||
Ok((cache.count, head, commits))
|
||||
}
|
||||
|
||||
fn remote_exists(url: &str) -> Result<bool> {
|
||||
@ -128,6 +145,7 @@ fn remote_exists(url: &str) -> Result<bool> {
|
||||
enum HocResult {
|
||||
Hoc {
|
||||
hoc: u64,
|
||||
commits: u64,
|
||||
hoc_pretty: String,
|
||||
head: String,
|
||||
url: String,
|
||||
@ -141,56 +159,72 @@ fn handle_hoc_request<T, F>(
|
||||
state: web::Data<Arc<State>>,
|
||||
data: web::Path<(String, String)>,
|
||||
mapper: F,
|
||||
) -> Result<HttpResponse>
|
||||
) -> impl Future<Item = HttpResponse, Error = Error>
|
||||
where
|
||||
T: Service,
|
||||
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, commits) = 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,
|
||||
commits,
|
||||
hoc_pretty,
|
||||
head: head.to_string(),
|
||||
url,
|
||||
repo,
|
||||
service_path,
|
||||
})
|
||||
})
|
||||
.and_then(mapper)
|
||||
}
|
||||
|
||||
fn hoc_request<T: Service>(
|
||||
fn json_hoc<T: Service>(
|
||||
state: web::Data<Arc<State>>,
|
||||
data: web::Path<(String, String)>,
|
||||
) -> Result<HocResult> {
|
||||
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)?;
|
||||
}
|
||||
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),
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
let mapper = |r| match r {
|
||||
HocResult::NotFound => p404(),
|
||||
HocResult::Hoc {
|
||||
hoc, head, commits, ..
|
||||
} => Ok(HttpResponse::Ok().json(JsonResponse {
|
||||
head: &head,
|
||||
count: hoc,
|
||||
commits,
|
||||
})),
|
||||
};
|
||||
Ok(HocResult::Hoc {
|
||||
hoc,
|
||||
hoc_pretty,
|
||||
head,
|
||||
url,
|
||||
repo,
|
||||
service_path,
|
||||
})
|
||||
handle_hoc_request::<T, _>(state, data, mapper)
|
||||
}
|
||||
|
||||
fn calculate_hoc<T: Service>(
|
||||
state: web::Data<Arc<State>>,
|
||||
data: web::Path<(String, String)>,
|
||||
) -> Result<HttpResponse> {
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
let mapper = |r| match r {
|
||||
HocResult::NotFound => Ok(p404()),
|
||||
HocResult::NotFound => p404(),
|
||||
HocResult::Hoc { hoc_pretty, .. } => {
|
||||
let badge_opt = BadgeOptions {
|
||||
subject: "Hits-of-Code".to_string(),
|
||||
@ -221,11 +255,12 @@ fn calculate_hoc<T: Service>(
|
||||
fn overview<T: Service>(
|
||||
state: web::Data<Arc<State>>,
|
||||
data: web::Path<(String, String)>,
|
||||
) -> Result<HttpResponse> {
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
let mapper = |r| match r {
|
||||
HocResult::NotFound => Ok(p404()),
|
||||
HocResult::NotFound => p404(),
|
||||
HocResult::Hoc {
|
||||
hoc,
|
||||
commits,
|
||||
hoc_pretty,
|
||||
url,
|
||||
head,
|
||||
@ -236,6 +271,7 @@ fn overview<T: Service>(
|
||||
templates::overview(
|
||||
&mut buf,
|
||||
VERSION_INFO,
|
||||
REPO_COUNT.load(Ordering::Relaxed),
|
||||
&OPT.domain,
|
||||
&service_path,
|
||||
&url,
|
||||
@ -243,6 +279,7 @@ fn overview<T: Service>(
|
||||
&hoc_pretty,
|
||||
&head,
|
||||
&T::commit_url(&repo, &head),
|
||||
commits,
|
||||
)?;
|
||||
|
||||
let (tx, rx_body) = mpsc::unbounded();
|
||||
@ -257,10 +294,15 @@ fn overview<T: Service>(
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/html")
|
||||
.body(INDEX.as_slice())
|
||||
fn index() -> Result<HttpResponse> {
|
||||
let mut buf = Vec::new();
|
||||
templates::index(
|
||||
&mut buf,
|
||||
VERSION_INFO,
|
||||
REPO_COUNT.load(Ordering::Relaxed),
|
||||
&OPT.domain,
|
||||
)?;
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
|
||||
}
|
||||
|
||||
#[post("/generate")]
|
||||
@ -270,6 +312,7 @@ fn generate(params: web::Form<GeneratorForm>) -> Result<HttpResponse> {
|
||||
templates::generate(
|
||||
&mut buf,
|
||||
VERSION_INFO,
|
||||
REPO_COUNT.load(Ordering::Relaxed),
|
||||
&OPT.domain,
|
||||
params.service.url(),
|
||||
params.service.service(),
|
||||
@ -283,10 +326,10 @@ fn generate(params: web::Form<GeneratorForm>) -> Result<HttpResponse> {
|
||||
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
|
||||
}
|
||||
|
||||
fn p404() -> HttpResponse {
|
||||
HttpResponse::NotFound()
|
||||
.content_type("text/html")
|
||||
.body(P404.as_slice())
|
||||
fn p404() -> Result<HttpResponse> {
|
||||
let mut buf = Vec::new();
|
||||
templates::p404(&mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed))?;
|
||||
Ok(HttpResponse::NotFound().content_type("text/html").body(buf))
|
||||
}
|
||||
|
||||
#[get("/tacit-css.min.css")]
|
||||
@ -299,8 +342,7 @@ fn favicon32() -> HttpResponse {
|
||||
HttpResponse::Ok().content_type("image/png").body(FAVICON)
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
config::init()?;
|
||||
fn start_server() -> Result<()> {
|
||||
let interface = format!("{}:{}", OPT.host, OPT.port);
|
||||
let state = Arc::new(State {
|
||||
repos: OPT.outdir.display().to_string(),
|
||||
@ -314,15 +356,55 @@ fn main() -> Result<()> {
|
||||
.service(css)
|
||||
.service(favicon32)
|
||||
.service(generate)
|
||||
.service(web::resource("/github/{user}/{repo}").to(calculate_hoc::<GitHub>))
|
||||
.service(web::resource("/gitlab/{user}/{repo}").to(calculate_hoc::<Gitlab>))
|
||||
.service(web::resource("/bitbucket/{user}/{repo}").to(calculate_hoc::<Bitbucket>))
|
||||
.service(web::resource("/view/github/{user}/{repo}").to(overview::<GitHub>))
|
||||
.service(web::resource("/view/gitlab/{user}/{repo}").to(overview::<Gitlab>))
|
||||
.service(web::resource("/view/bitbucket/{user}/{repo}").to(overview::<Bitbucket>))
|
||||
.default_service(web::resource("").route(web::get().to(p404)))
|
||||
.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("/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/gitlab/{user}/{repo}").to_async(overview::<Gitlab>))
|
||||
.service(web::resource("/view/bitbucket/{user}/{repo}").to_async(overview::<Bitbucket>))
|
||||
.default_service(web::resource("").route(web::get().to_async(p404)))
|
||||
})
|
||||
.workers(OPT.workers)
|
||||
.bind(interface)?
|
||||
.run()?)
|
||||
}
|
||||
|
||||
fn migrate_cache() -> Result<()> {
|
||||
let mut backup_cache = OPT.cachedir.clone();
|
||||
backup_cache.set_extension("bak");
|
||||
rename(&OPT.cachedir, backup_cache)?;
|
||||
let outdir = OPT.outdir.display().to_string();
|
||||
let cachedir = OPT.cachedir.display().to_string();
|
||||
for service in read_dir(&OPT.outdir)? {
|
||||
let service = service?;
|
||||
for namespace in read_dir(service.path())? {
|
||||
let namespace = namespace?;
|
||||
for repo in read_dir(namespace.path())? {
|
||||
let repo_path = repo?.path().display().to_string();
|
||||
let repo_path: String =
|
||||
repo_path
|
||||
.split(&outdir)
|
||||
.fold(String::new(), |mut acc, next| {
|
||||
acc.push_str(next);
|
||||
acc
|
||||
});
|
||||
println!("{}", repo_path);
|
||||
hoc(&repo_path, &outdir, &cachedir)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
config::init()?;
|
||||
match &OPT.migrate {
|
||||
None => start_server(),
|
||||
Some(migration) => match migration {
|
||||
Migration::CacheCommitCount => migrate_cache(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::{config::Opt, templates};
|
||||
use crate::{config::Opt, count::count_repositories};
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use structopt::StructOpt;
|
||||
|
||||
pub struct VersionInfo<'a> {
|
||||
@ -16,19 +17,6 @@ pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png");
|
||||
lazy_static! {
|
||||
pub(crate) static ref CLIENT: reqwest::Client = reqwest::Client::new();
|
||||
pub(crate) static ref OPT: Opt = Opt::from_args();
|
||||
pub(crate) static ref INDEX: Vec<u8> = {
|
||||
let mut buf = Vec::new();
|
||||
templates::index(&mut buf, VERSION_INFO, &OPT.domain).unwrap();
|
||||
buf
|
||||
};
|
||||
pub(crate) static ref P404: Vec<u8> = {
|
||||
let mut buf = Vec::new();
|
||||
templates::p404(&mut buf, VERSION_INFO).unwrap();
|
||||
buf
|
||||
};
|
||||
pub(crate) static ref P500: Vec<u8> = {
|
||||
let mut buf = Vec::new();
|
||||
templates::p500(&mut buf, VERSION_INFO).unwrap();
|
||||
buf
|
||||
};
|
||||
pub(crate) static ref REPO_COUNT: AtomicUsize =
|
||||
AtomicUsize::new(count_repositories(&OPT.outdir).unwrap());
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
@use crate::statics::VersionInfo;
|
||||
|
||||
@(title: &str, header: &str, content: Content, version_info: VersionInfo)
|
||||
@(title: &str, header: &str, content: Content, version_info: VersionInfo, repo_count: usize)
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
@ -38,6 +38,13 @@
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
<nav>
|
||||
<ul>
|
||||
<li>
|
||||
<small>Currently serving @repo_count repositories</small>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><small><a href="https://github.com/vbrandl/hoc">GitHub</a></small></li>
|
||||
|
@ -1,7 +1,7 @@
|
||||
@use super::base;
|
||||
@use crate::statics::VersionInfo;
|
||||
|
||||
@(version_info: VersionInfo, domain: &str, url: &str, service: &str, path: &str)
|
||||
@(version_info: VersionInfo, repo_count: usize, domain: &str, url: &str, service: &str, path: &str)
|
||||
|
||||
@:base("Hits-of-Code Badges", "Badge Generator", {
|
||||
|
||||
@ -20,4 +20,4 @@ It will be rendered like this
|
||||
<pre>
|
||||
<a href="https://@domain/view/@service/@path"><img src="https://@domain/@service/@path" alt="example badge" /></a>
|
||||
</pre>
|
||||
}, version_info)
|
||||
}, version_info, repo_count)
|
||||
|
@ -1,7 +1,7 @@
|
||||
@use super::base;
|
||||
@use crate::statics::VersionInfo;
|
||||
|
||||
@(version_info: VersionInfo, domain: &str)
|
||||
@(version_info: VersionInfo, repo_count: usize, domain: &str)
|
||||
|
||||
@:base("Hits-of-Code Badges", "Hits-of-Code Badges", {
|
||||
|
||||
@ -45,6 +45,22 @@ would render this badge:
|
||||
alt="example badge" /></a>
|
||||
</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 three fields: <code>count</code> (the HoC value), <code>commits</code> (the number of commits) and
|
||||
<code>head</code> (the commit ref of HEAD). 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": "1f01c3b964b018fb0c0c2c5b572bf4ace2968546",
|
||||
"count": 8324,
|
||||
"commits": 223
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2>Badge Generator</h2>
|
||||
|
||||
<form method="post" action="/generate">
|
||||
@ -76,4 +92,4 @@ my <a href="https://mirror.oldsql.cc/key.asc">GPG key</a>
|
||||
(<a href="http://pool.sks-keyservers.net/pks/lookup?op=get&search=0x1FFE431282F4B8CC0A7579167FB009175885FC76">from a
|
||||
keyserver</a>), or by using any other UID from my key.
|
||||
</p>
|
||||
}, version_info)
|
||||
}, version_info, repo_count)
|
||||
|
@ -1,12 +1,13 @@
|
||||
@use super::base;
|
||||
@use crate::statics::VersionInfo;
|
||||
|
||||
@(version_info: VersionInfo, domain: &str, path: &str, url: &str, hoc: u64, hoc_pretty: &str, head: &str, commit_url: &str)
|
||||
@(version_info: VersionInfo, repo_count: usize, domain: &str, path: &str, url: &str, hoc: u64, hoc_pretty: &str, head: &str, commit_url: &str, commits: u64)
|
||||
|
||||
@:base("Hits-of-Code Badges", "Overview", {
|
||||
|
||||
<p>
|
||||
The project <a href="@url">@url</a> has <strong>@hoc_pretty</strong> (exactly @hoc) hits of code at <a href="@commit_url">@head</a>.
|
||||
The project <a href="@url">@url</a> has <strong>@hoc_pretty</strong> (exactly @hoc) hits of code at
|
||||
<a href="@commit_url">@head</a>. The repository contains <strong>@commits</strong> commits.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
@ -16,4 +17,4 @@ To include the badge in your readme, use the following markdown:
|
||||
<pre>
|
||||
[](https://@domain/view/@path)
|
||||
</pre>
|
||||
}, version_info)
|
||||
}, version_info, repo_count)
|
||||
|
@ -1,7 +1,7 @@
|
||||
@use super::base;
|
||||
@use crate::statics::VersionInfo;
|
||||
|
||||
@(version_info: VersionInfo)
|
||||
@(version_info: VersionInfo, repo_count: usize)
|
||||
|
||||
@:base("Page not Found - Hits-of-Code Badges", "404 - Page not Found", {
|
||||
<p>
|
||||
@ -11,4 +11,4 @@
|
||||
<p>
|
||||
If you think, this is a mistake on my side, please <a href="mailto:mail+hoc@@vbrandl.net">drop me a mail</a>.
|
||||
</p>
|
||||
}, version_info)
|
||||
}, version_info, repo_count)
|
||||
|
@ -1,7 +1,7 @@
|
||||
@use super::base;
|
||||
@use crate::statics::VersionInfo;
|
||||
|
||||
@(version_info: VersionInfo)
|
||||
@(version_info: VersionInfo, repo_count: usize)
|
||||
|
||||
@:base("Internal Server Error - Hits-of-Code Badges", "500 - Internal Server Error", {
|
||||
<p>
|
||||
@ -11,4 +11,4 @@
|
||||
<p>
|
||||
If you think, this is a bug, please <a href="mailto:mail+hoc@@vbrandl.net">drop me a mail</a>.
|
||||
</p>
|
||||
}, version_info)
|
||||
}, version_info, repo_count)
|
||||
|
Reference in New Issue
Block a user