Compare commits

...

18 Commits

Author SHA1 Message Date
ec8bbe0325 Bump version number
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2019-06-12 21:53:47 +02:00
8d4de48c11 Calculate repo count on start and increment when a new repo is cloned
All checks were successful
continuous-integration/drone/push Build is passing
2019-06-12 21:51:54 +02:00
eb718990ec Add repo count to templates 2019-06-12 21:51:16 +02:00
60dc242e5a Add repo count to templates 2019-06-12 21:50:45 +02:00
615460c87b Update dependencies
All checks were successful
continuous-integration/drone/push Build is passing
2019-06-12 20:05:55 +02:00
ba6fc7d394 Update git2 to 0.9.1
All checks were successful
continuous-integration/drone/push Build is passing
2019-06-12 20:04:26 +02:00
5551c6f3e4 Update serde and serde_derive to 1.0.92
All checks were successful
continuous-integration/drone/push Build is passing
2019-06-12 20:00:00 +02:00
38399b0bcd Update actix-web to 1.0.0 2019-06-12 19:55:32 +02:00
ad4d87ca2f Update reqwest to 0.9.18 2019-06-12 19:53:51 +02:00
137f264bdf Merge pull request #13 from vbrandl/dependabot/cargo/structopt-0.2.16
All checks were successful
continuous-integration/drone/push Build is passing
Bump structopt from 0.2.15 to 0.2.16
2019-06-01 20:51:42 +02:00
8cfe84c763 Bump structopt from 0.2.15 to 0.2.16
Bumps [structopt](https://github.com/TeXitoi/structopt) from 0.2.15 to 0.2.16.
- [Release notes](https://github.com/TeXitoi/structopt/releases)
- [Changelog](https://github.com/TeXitoi/structopt/blob/master/CHANGELOG.md)
- [Commits](https://github.com/TeXitoi/structopt/compare/v0.2.15...v0.2.16)
2019-05-30 05:32:17 +00:00
787ff55444 Bump version number
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2019-05-28 22:26:55 +02:00
3901fe9e9c Cleanup
All checks were successful
continuous-integration/drone/push Build is passing
2019-05-28 22:06:31 +02:00
e0b6ee69f1 Actually run as user
All checks were successful
continuous-integration/drone/push Build is passing
2019-05-28 21:59:18 +02:00
75398a3613 Use adduser
All checks were successful
continuous-integration/drone/push Build is passing
2019-05-28 21:56:44 +02:00
a944e8b149 Don't run as root in the container
All checks were successful
continuous-integration/drone/push Build is passing
2019-05-28 21:46:47 +02:00
d9faf25648 Make script executable
[skip ci]
2019-05-19 16:05:58 +02:00
d2977eee32 Add script to list repositories
[ci skip]
2019-05-19 16:01:58 +02:00
15 changed files with 335 additions and 321 deletions

500
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,16 @@
[package]
name = "hoc"
version = "0.6.0"
version = "0.8.0"
authors = ["Valentin Brandl <vbrandl@riseup.net>"]
edition = "2018"
build = "build.rs"
[dependencies]
actix-web = "1.0.0-rc"
actix-web = "1.0.0"
badge = "0.2.0"
bytes = "0.4.12"
futures = "0.1.27"
git2 = "0.8.0"
git2 = "0.9.1"
lazy_static = "1.3.0"
log = "0.4.6"
log4rs = "0.8.3"
@ -20,7 +20,7 @@ reqwest = "0.9.17"
serde = "1.0.91"
serde_derive = "1.0.91"
serde_json = "1.0.39"
structopt = "0.2.15"
structopt = "0.2.16"
[build-dependencies]
ructe = "0.6.2"

View File

@ -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"]

View File

@ -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
View 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

24
src/count.rs Normal file
View 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())
}

View File

@ -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>;
@ -33,15 +36,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()
}
}

View File

@ -1,3 +1,5 @@
#![type_length_limit = "2257138"]
#[macro_use]
extern crate actix_web;
#[macro_use]
@ -9,6 +11,7 @@ extern crate serde_derive;
mod cache;
mod config;
mod count;
mod error;
mod service;
mod statics;
@ -17,7 +20,7 @@ use crate::{
cache::CacheState,
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,
@ -34,6 +37,7 @@ use std::{
fs::create_dir_all,
path::Path,
process::Command,
sync::atomic::Ordering,
sync::Arc,
time::{Duration, SystemTime},
};
@ -169,6 +173,7 @@ fn hoc_request<T: Service>(
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)?;
@ -192,7 +197,7 @@ fn calculate_hoc<T: Service>(
data: web::Path<(String, String)>,
) -> 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(),
@ -225,7 +230,7 @@ fn overview<T: Service>(
data: web::Path<(String, String)>,
) -> impl Future<Item = HttpResponse, Error = Error> {
let mapper = |r| match r {
HocResult::NotFound => Ok(p404()),
HocResult::NotFound => p404(),
HocResult::Hoc {
hoc,
hoc_pretty,
@ -238,6 +243,7 @@ fn overview<T: Service>(
templates::overview(
&mut buf,
VERSION_INFO,
REPO_COUNT.load(Ordering::Relaxed),
&OPT.domain,
&service_path,
&url,
@ -259,10 +265,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")]
@ -272,6 +283,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(),
@ -285,10 +297,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")]

View File

@ -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());
}

View File

@ -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">
@ -36,6 +36,9 @@
<li>
<small>HoC v@version_info.version - <a href="https://github.com/vbrandl/hoc/commit/@version_info.commit">@version_info.commit</a></small>
</li>
<li>
<small>Currently serving @repo_count repositories</small>
</li>
</ul>
</nav>
<nav>

View File

@ -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)

View File

@ -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", {
@ -76,4 +76,4 @@ my <a href="https://mirror.oldsql.cc/key.asc">GPG key</a>
(<a href="http://pool.sks-keyservers.net/pks/lookup?op=get&amp;search=0x1FFE431282F4B8CC0A7579167FB009175885FC76">from a
keyserver</a>), or by using any other UID from my key.
</p>
}, version_info)
}, version_info, repo_count)

View File

@ -1,7 +1,7 @@
@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)
@:base("Hits-of-Code Badges", "Overview", {
@ -16,4 +16,4 @@ To include the badge in your readme, use the following markdown:
<pre>
[![Hits-of-Code](https://@domain/@path)](https://@domain/view/@path)
</pre>
}, version_info)
}, version_info, repo_count)

View File

@ -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)

View File

@ -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)