Compare commits
166 Commits
v0.14.2
...
feature/tr
Author | SHA1 | Date | |
---|---|---|---|
04f1437d0c | |||
e706176d84 | |||
a2a0047e03 | |||
be7320e24e | |||
55f937aac2 | |||
d929a559b6 | |||
5e7374deea | |||
0821559471 | |||
a9ede0b2c7 | |||
be48ebd2d9 | |||
c80bef7cbd | |||
c9b67eb6e6 | |||
1ab4cb6c71 | |||
9785b38ce0 | |||
492e17b997 | |||
43586e534b | |||
70bca967f3 | |||
61013e43ab | |||
0c100e76ad | |||
000c7457f5 | |||
70361e8b70 | |||
4237f6cc06 | |||
04a90469fe | |||
117a052aa5 | |||
b2bc7fc217 | |||
cd2624ae11 | |||
fa9601bb9a | |||
ae9d31b82c | |||
2afe5fc172 | |||
ea7f074661 | |||
d5e3cad299 | |||
6570daef02 | |||
6ac389e3ee | |||
36196975ac | |||
1dd83829b2 | |||
1b0817bc25 | |||
f57dc8e890 | |||
bdda6292d2 | |||
a2826c9922 | |||
3b2a368df0 | |||
8b7444bd7c | |||
9d719e7d85 | |||
584379e047 | |||
951124e038 | |||
e574f4bdd0 | |||
5c8e3fa35d | |||
4c0df1fa19 | |||
f6b46a1e90 | |||
5eb16ac38e | |||
3486d44bc5 | |||
1311e724ce | |||
5da9e74992 | |||
8dadc0ddea | |||
cba235eadf | |||
5284249acf | |||
d28f56dac0 | |||
ab02a45bf6 | |||
837c1c160b | |||
3d26823425 | |||
d9553871fa | |||
d2bc9c2149 | |||
b0b97653a9 | |||
0b1bc60790 | |||
e93588a6d9 | |||
cd5046a276 | |||
919348d49d | |||
8378b0b51b | |||
0394ae52e9 | |||
fb34f6bf2f | |||
36622ad322 | |||
89bb27e137 | |||
7d4385764c | |||
5df97f0e64 | |||
a3d98a326e | |||
76bf5f2f98 | |||
e827d2ef39 | |||
65576140ce | |||
cea54cf2f3 | |||
8c28c68c17 | |||
b86a080ead | |||
1b0c959a8d | |||
806baf7460 | |||
080f7379ea | |||
7409de5dba | |||
07f4ef518a | |||
820ebdd899 | |||
527d793914 | |||
7df6c3fc75 | |||
17e1af1dd7 | |||
baade8fbc2 | |||
cfc07664fc | |||
52938c9106 | |||
b64f573a38 | |||
ebbd1ea8cb | |||
93a0147a78 | |||
2040e718d7 | |||
5679f67de0 | |||
8babdb978e | |||
b3388912c2 | |||
bce511ce0b | |||
50e36769d1 | |||
9477338331 | |||
c1d0930354 | |||
688067e5c5 | |||
529f36dc40 | |||
d8fdabfca6 | |||
8060b80ae6 | |||
15965c5b5f | |||
1eade03b63 | |||
f41fbc8baf | |||
14cd21dc1c | |||
1bdee4ee36 | |||
9e33742d81 | |||
8e78d13443 | |||
391fa39470 | |||
e6e90214e2 | |||
6f734b103a | |||
413bb824e1 | |||
58ad13dbad | |||
9f8b781f7b | |||
175b7c828b | |||
002119324f | |||
df78b6f1e5 | |||
e1bff0d280 | |||
4c9454aa9e | |||
e9e57495a6 | |||
b6505f6a37 | |||
a2463bf657 | |||
53cb73cd9b | |||
423a3aa0b0 | |||
7a2c6b6f06 | |||
ffb306a7a8 | |||
cef2ae2299 | |||
b91de72d19 | |||
fd08489587 | |||
ce0d6041ea | |||
3af60a82ce | |||
bcdf7db549 | |||
9b2f1f4ebb | |||
84e47237de | |||
909f6585b5 | |||
b48d7f1492 | |||
f1e9d1806f | |||
8c62d01f3c | |||
26a5025a32 | |||
6f931ce46f | |||
2d46592c4a | |||
c2d496f2b4 | |||
19d37806f2 | |||
b4bd9b8830 | |||
3c8227d0e9 | |||
d6409c21ec | |||
990b5acbda | |||
f9e14e2ffd | |||
a73afe6851 | |||
20544b27d9 | |||
288573b1a4 | |||
a12755d7be | |||
a248531ce2 | |||
de7919a031 | |||
3913039010 | |||
078d3cdcf9 | |||
c552a84870 | |||
7c1a14b6ad | |||
c69b8207b8 | |||
a319f400e9 |
7
.env.example
Normal file
7
.env.example
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
HOC_REPODIR='./repos'
|
||||||
|
HOC_CACHEDIR='./cache'
|
||||||
|
HOC_PORT=8080
|
||||||
|
HOC_HOST='0.0.0.0'
|
||||||
|
HOC_WORKERS=4
|
||||||
|
|
||||||
|
HOC_BASE_URL='http://0.0.0.0:8080'
|
10
.github/dependabot.yml
vendored
Normal file
10
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: cargo
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: daily
|
||||||
|
time: "04:00"
|
||||||
|
open-pull-requests-limit: 10
|
||||||
|
assignees:
|
||||||
|
- vbrandl
|
23
.github/workflows/audit.yml
vendored
23
.github/workflows/audit.yml
vendored
@ -4,27 +4,24 @@ on:
|
|||||||
- cron: '0 1 * * *'
|
- cron: '0 1 * * *'
|
||||||
push:
|
push:
|
||||||
paths:
|
paths:
|
||||||
- 'Cargo.toml'
|
- '**/Cargo.toml'
|
||||||
- 'Cargo.lock'
|
- '**/Cargo.lock'
|
||||||
pull_request:
|
pull_request:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
security_audit:
|
security_audit:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Cache cargo registry
|
- name: Cache cargo registry, index and build directory
|
||||||
uses: actions/cache@v1
|
uses: actions/cache@v2
|
||||||
with:
|
with:
|
||||||
path: /usr/share/rust/.cargo/registry
|
path: |
|
||||||
key: ${{ runner.os }}-cargo-registry
|
~/.cargo/registry
|
||||||
|
~/.cargo/git
|
||||||
- name: Cache cargo index
|
./target
|
||||||
uses: actions/cache@v1
|
key: audit-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||||
with:
|
|
||||||
path: /usr/share/rust/.cargo/git
|
|
||||||
key: ${{ runner.os }}-cargo-index
|
|
||||||
|
|
||||||
- uses: actions-rs/audit-check@v1
|
- uses: actions-rs/audit-check@v1
|
||||||
with:
|
with:
|
||||||
|
20
.github/workflows/nix-build.yml
vendored
20
.github/workflows/nix-build.yml
vendored
@ -1,20 +0,0 @@
|
|||||||
name: "Nix Build"
|
|
||||||
|
|
||||||
on: [push]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- uses: cachix/install-nix-action@v8
|
|
||||||
- name: Cache nix store
|
|
||||||
uses: actions/cache@v1
|
|
||||||
with:
|
|
||||||
path: /nix
|
|
||||||
key: ${{ runner.os }}-nix-store
|
|
||||||
- uses: cachix/cachix-action@v5
|
|
||||||
with:
|
|
||||||
name: hitsofcode
|
|
||||||
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
|
|
||||||
attributes: package
|
|
76
.github/workflows/release.yml
vendored
Normal file
76
.github/workflows/release.yml
vendored
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v[0-9]+.[0-9]+.[0-9]+'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
name: Publishing for ${{ matrix.os }}
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [macos-latest, ubuntu-latest, windows-latest]
|
||||||
|
rust: [stable]
|
||||||
|
include:
|
||||||
|
- os: macos-latest
|
||||||
|
artifact_prefix: macos
|
||||||
|
target: x86_64-apple-darwin
|
||||||
|
binary_postfix: ""
|
||||||
|
- os: ubuntu-latest
|
||||||
|
artifact_prefix: linux
|
||||||
|
target: x86_64-unknown-linux-gnu
|
||||||
|
binary_postfix: ""
|
||||||
|
- os: windows-latest
|
||||||
|
artifact_prefix: windows
|
||||||
|
target: x86_64-pc-windows-msvc
|
||||||
|
binary_postfix: ".exe"
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout sources
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Install stable toolchain
|
||||||
|
uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: ${{ matrix.rust }}
|
||||||
|
override: true
|
||||||
|
|
||||||
|
- name: Cache cargo registry, index and build directory
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.cargo/registry
|
||||||
|
~/.cargo/git
|
||||||
|
./target
|
||||||
|
key: release-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|
||||||
|
- name: Cargo build
|
||||||
|
uses: actions-rs/cargo@v1
|
||||||
|
with:
|
||||||
|
command: build
|
||||||
|
toolchain: ${{ matrix.rust }}
|
||||||
|
args: --release --target ${{ matrix.target }}
|
||||||
|
|
||||||
|
- name: Packaging final binary
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
cd target/${{ matrix.target }}/release
|
||||||
|
strip hoc${{ matrix.binary_postfix }}
|
||||||
|
tar czvf hoc-${{ matrix.artifact_prefix }}.tar.gz hoc${{ matrix.binary_postfix }}
|
||||||
|
|
||||||
|
if [[ ${{ runner.os }} == 'Windows' ]]; then
|
||||||
|
certutil -hashfile hoc-${{ matrix.artifact_prefix }}.tar.gz sha256 | grep -E [A-Fa-f0-9]{64} > hoc-${{ matrix.artifact_prefix }}.sha256
|
||||||
|
else
|
||||||
|
shasum -a 256 hoc-${{ matrix.artifact_prefix }}.tar.gz > hoc-${{ matrix.artifact_prefix }}.sha256
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Releasing assets
|
||||||
|
uses: softprops/action-gh-release@v1
|
||||||
|
with:
|
||||||
|
files: |
|
||||||
|
target/${{ matrix.target }}/release/hoc-${{ matrix.artifact_prefix }}.tar.gz
|
||||||
|
target/${{ matrix.target }}/release/hoc-${{ matrix.artifact_prefix }}.sha256
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
94
.github/workflows/rust.yml
vendored
94
.github/workflows/rust.yml
vendored
@ -1,41 +1,30 @@
|
|||||||
name: Rust
|
|
||||||
|
|
||||||
on: [push, pull_request]
|
on: [push, pull_request]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
lint:
|
|
||||||
name: Linting and Formatting Checks
|
rustfmt:
|
||||||
|
name: Rustfmt
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v1
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Install stable toolchain
|
- name: Install stable toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: stable
|
toolchain: stable
|
||||||
|
profile: minimal
|
||||||
override: true
|
override: true
|
||||||
|
components: rustfmt
|
||||||
|
|
||||||
- name: Install rustfmt
|
- name: Cache cargo registry, index and build directory
|
||||||
run: rustup component add rustfmt
|
uses: actions/cache@v2
|
||||||
|
|
||||||
- name: Cache cargo registry
|
|
||||||
uses: actions/cache@v1
|
|
||||||
with:
|
with:
|
||||||
path: ~/.cargo/registry
|
path: |
|
||||||
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
|
~/.cargo/registry
|
||||||
|
~/.cargo/git
|
||||||
- name: Cache cargo index
|
./target
|
||||||
uses: actions/cache@v1
|
key: rustfmt-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||||
with:
|
|
||||||
path: ~/.cargo/git
|
|
||||||
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
|
|
||||||
|
|
||||||
- name: Cache cargo build
|
|
||||||
uses: actions/cache@v1
|
|
||||||
with:
|
|
||||||
path: target
|
|
||||||
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
|
|
||||||
|
|
||||||
- name: Check Formatting
|
- name: Check Formatting
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
@ -43,8 +32,29 @@ jobs:
|
|||||||
command: fmt
|
command: fmt
|
||||||
args: --all -- --check
|
args: --all -- --check
|
||||||
|
|
||||||
- name: Install clippy
|
clippy:
|
||||||
run: rustup component add clippy
|
name: Clippy
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout sources
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Install stable toolchain
|
||||||
|
uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
profile: minimal
|
||||||
|
override: true
|
||||||
|
components: rustfmt
|
||||||
|
|
||||||
|
- name: Cache cargo registry, index and build directory
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.cargo/registry
|
||||||
|
~/.cargo/git
|
||||||
|
./target
|
||||||
|
key: clippy-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|
||||||
- name: Clippy Linting
|
- name: Clippy Linting
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
@ -52,41 +62,35 @@ jobs:
|
|||||||
command: clippy
|
command: clippy
|
||||||
args: -- -D warnings
|
args: -- -D warnings
|
||||||
|
|
||||||
|
|
||||||
test:
|
test:
|
||||||
name: Run Tests
|
name: Test Suite
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
# add windows-latest when it is clear why tests are failing
|
# add windows-latest when it is clear why tests are failing
|
||||||
os: [ubuntu-latest, macos-latest]
|
# os: [ubuntu-latest, macos-latest]
|
||||||
|
os: [ubuntu-latest]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout sources
|
- name: Checkout sources
|
||||||
uses: actions/checkout@v1
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Install stable toolchain
|
- name: Install stable toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
toolchain: stable
|
toolchain: stable
|
||||||
|
profile: minimal
|
||||||
override: true
|
override: true
|
||||||
|
|
||||||
- name: Cache cargo registry
|
- name: Cache cargo registry, index and build directory
|
||||||
uses: actions/cache@v1
|
uses: actions/cache@v2
|
||||||
with:
|
with:
|
||||||
path: ~/.cargo/registry
|
path: |
|
||||||
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
|
~/.cargo/registry
|
||||||
|
~/.cargo/git
|
||||||
- name: Cache cargo index
|
./target
|
||||||
uses: actions/cache@v1
|
key: test-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||||
with:
|
|
||||||
path: ~/.cargo/git
|
|
||||||
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
|
|
||||||
|
|
||||||
- name: Cache cargo build
|
|
||||||
uses: actions/cache@v1
|
|
||||||
with:
|
|
||||||
path: target
|
|
||||||
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
|
|
||||||
|
|
||||||
- name: Run Tests
|
- name: Run Tests
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@ repos
|
|||||||
cache
|
cache
|
||||||
hoc.log
|
hoc.log
|
||||||
result
|
result
|
||||||
|
.env
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
image: docker:19.03
|
|
||||||
services:
|
|
||||||
- docker:19.03-dind
|
|
||||||
|
|
||||||
stages:
|
|
||||||
- build
|
|
||||||
- release
|
|
||||||
|
|
||||||
variables:
|
|
||||||
DOCKER_HOST: tcp://docker:2375
|
|
||||||
DOCKER_DRIVER: overlay2
|
|
||||||
# DOCKER_TLS_CERTDIR: "/certs"
|
|
||||||
DOCKER_TLS_CERTDIR: ""
|
|
||||||
CONTAINER_BUILDER_IMAGE: $CI_REGISTRY_IMAGE:builder-latest
|
|
||||||
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
|
|
||||||
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
|
|
||||||
|
|
||||||
before_script:
|
|
||||||
- export CONTAINER_TAG_IMAGE="$CI_REGISTRY_IMAGE:${CI_COMMIT_TAG:1}"
|
|
||||||
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
|
||||||
|
|
||||||
build:
|
|
||||||
stage: build
|
|
||||||
script:
|
|
||||||
- docker pull $CONTAINER_TEST_IMAGE || true
|
|
||||||
- docker pull $CONTAINER_RELEASE_IMAGE || true
|
|
||||||
- docker pull $CONTAINER_TAG_IMAGE || true
|
|
||||||
- docker pull $CONTAINER_BUILDER_IMAGE || true
|
|
||||||
- docker build --pull -t $CONTAINER_BUILDER_IMAGE --target builder .
|
|
||||||
- docker build --pull -t $CONTAINER_TEST_IMAGE .
|
|
||||||
- docker push $CONTAINER_BUILDER_IMAGE
|
|
||||||
- docker push $CONTAINER_TEST_IMAGE
|
|
||||||
|
|
||||||
release-image:
|
|
||||||
stage: release
|
|
||||||
script:
|
|
||||||
- docker pull $CONTAINER_TEST_IMAGE
|
|
||||||
- docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
|
|
||||||
- docker push $CONTAINER_RELEASE_IMAGE
|
|
||||||
only:
|
|
||||||
- master
|
|
||||||
|
|
||||||
release-tag:
|
|
||||||
stage: release
|
|
||||||
script:
|
|
||||||
- docker pull $CONTAINER_TEST_IMAGE
|
|
||||||
- docker tag $CONTAINER_TEST_IMAGE $CONTAINER_TAG_IMAGE
|
|
||||||
- docker push $CONTAINER_TAG_IMAGE
|
|
||||||
only:
|
|
||||||
- /^v\d+\.\d+\.\d+/
|
|
||||||
except:
|
|
||||||
- branch
|
|
||||||
|
|
17
.travis.yml
17
.travis.yml
@ -1,17 +0,0 @@
|
|||||||
language: rust
|
|
||||||
|
|
||||||
rust:
|
|
||||||
- stable
|
|
||||||
- beta
|
|
||||||
- nightly
|
|
||||||
|
|
||||||
cache:
|
|
||||||
- cargo
|
|
||||||
|
|
||||||
matrix:
|
|
||||||
allow_failures:
|
|
||||||
- rust: nightly
|
|
||||||
|
|
||||||
notifications:
|
|
||||||
email:
|
|
||||||
on_failure: always
|
|
1786
Cargo.lock
generated
1786
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
52
Cargo.toml
52
Cargo.toml
@ -1,31 +1,45 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "hoc"
|
name = "hoc"
|
||||||
version = "0.14.2"
|
version = "0.17.2"
|
||||||
authors = ["Valentin Brandl <vbrandl@riseup.net>"]
|
authors = ["Valentin Brandl <vbrandl@riseup.net>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
path = "src/main.rs"
|
||||||
|
name = "hoc"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "3.0.2"
|
|
||||||
badge = "0.3.0"
|
|
||||||
bytes = "0.5.6"
|
|
||||||
futures = "0.3.5"
|
|
||||||
git2 = "0.13.11"
|
|
||||||
lazy_static = "1.4.0"
|
|
||||||
log = "0.4.11"
|
|
||||||
log4rs = "0.13.0"
|
|
||||||
number_prefix = "0.4.0"
|
|
||||||
openssl-probe = "0.1.2"
|
|
||||||
reqwest = "0.10.8"
|
|
||||||
serde = "1.0.116"
|
|
||||||
serde_derive = "1.0.103"
|
|
||||||
serde_json = "1.0.57"
|
|
||||||
structopt = "0.3.17"
|
|
||||||
actix-rt = "1.1.1"
|
actix-rt = "1.1.1"
|
||||||
|
actix-web = "3.3.2"
|
||||||
|
badge = "0.3.0"
|
||||||
|
bytes = "1.0.1"
|
||||||
|
config = { version = "0.11.0", features = ["toml"] }
|
||||||
|
dotenv = "0.15.0"
|
||||||
|
futures = "0.3.15"
|
||||||
|
git2 = "0.13.18"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
number_prefix = "0.4.0"
|
||||||
|
openssl-probe = "0.1.4"
|
||||||
|
reqwest = "0.10.10"
|
||||||
|
serde = "1.0.126"
|
||||||
|
serde_derive = "1.0.123"
|
||||||
|
serde_json = "1.0.64"
|
||||||
|
tracing = "0.1.26"
|
||||||
|
tracing-actix-web = "0.2.1"
|
||||||
|
tracing-bunyan-formatter = "0.2.4"
|
||||||
|
tracing-futures = "0.2.5"
|
||||||
|
tracing-log = "0.1.2"
|
||||||
|
tracing-subscriber = { version = "0.2.18", features = ["registry", "env-filter"] }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
ructe = "0.12.0"
|
ructe = "0.13.2"
|
||||||
vergen = "3.1.0"
|
vergen = { version = "5.1.2", default-features = false, features = ["git"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.1.0"
|
ructe = "0.13.2"
|
||||||
|
tempfile = "3.2.0"
|
||||||
|
tokio = "0.2.25"
|
||||||
|
27
README.md
27
README.md
@ -1,9 +1,8 @@
|
|||||||
# Hits-of-Code
|
# Hits-of-Code
|
||||||
|
|
||||||
[](https://hitsofcode.com/view/github/vbrandl/hoc)
|
[](https://hitsofcode.com/github/vbrandl/hoc/view)
|
||||||
[](https://drone.vbrandl.net/vbrandl/hoc)
|
[](https://drone.vbrandl.net/vbrandl/hoc)
|
||||||
[](https://gitlab.com/vbrandl/hoc/pipelines)
|
[](https://gitlab.com/vbrandl/hoc/pipelines)
|
||||||
[](https://travis-ci.org/vbrandl/hoc)
|
|
||||||
[](https://deps.rs/repo/github/vbrandl/hoc)
|
[](https://deps.rs/repo/github/vbrandl/hoc)
|
||||||
|
|
||||||
Small webservice, that returns a badge of the Hits-of-Code of a git repository, as described by [Yegor
|
Small webservice, that returns a badge of the Hits-of-Code of a git repository, as described by [Yegor
|
||||||
@ -27,7 +26,7 @@ where `<service>` is one of `gitub`, `gitlab` or `bitbucket`. The HoC data can a
|
|||||||
https://<host>/<service>/<user>/<repo>/json
|
https://<host>/<service>/<user>/<repo>/json
|
||||||
```
|
```
|
||||||
|
|
||||||
There is also an overview page available via `https://<host>/view/<service>/<user>/<repo>`
|
There is also an overview page available via `https://<host>/<service>/<user>/<repo>/view`
|
||||||
|
|
||||||
To delete a repository and the cache from the server, send a `POST` request to
|
To delete a repository and the cache from the server, send a `POST` request to
|
||||||
`https://<host>/<service>/<user>/<repo>/delete`. On the overview page, there is a button to perform this operation. It
|
`https://<host>/<service>/<user>/<repo>/delete`. On the overview page, there is a button to perform this operation. It
|
||||||
@ -49,21 +48,29 @@ $ docker build .
|
|||||||
|
|
||||||
inside the repository.
|
inside the repository.
|
||||||
|
|
||||||
I'm currently working on migrating to [nix](https://nixos.org/nix). To get a
|
I'm currently working on migrating to [nix](https://nixos.org/nix). To get a development shell, run `nix-shell`, to
|
||||||
development shell, run `nix-shell`, to build the package run `nix-build --attr
|
build the package run `nix-build --attr package` and to build the Docker image, run `nix-build --attr dockerImage`.
|
||||||
package` and to build the Docker image, run `nix-build --attr dockerImage`.
|
|
||||||
|
|
||||||
|
|
||||||
## Running
|
## Running
|
||||||
|
|
||||||
Run either the binary produced by cargo, the Docker container you just built (using docker-compose) or pull the image
|
Rename [`hoc.toml.example`](./hoc.toml.example) to `hoc.toml` or [`.env.example`](./.env.example) to `.env` and set the
|
||||||
from [Docker Hub](https://hub.docker.com/r/vbrandl/hits-of-code)
|
correct value for `base_url`/`HOC_BASE_URL`. If you don't want to use a configuration or dotenv file, you can pass all
|
||||||
|
parameters directly via environment variables. For variable names see [`.env.example`](./.env.example).
|
||||||
|
|
||||||
|
To start a local instance of the service just run:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ docker run -it --rm vbrandl/hits-of-code --help
|
$ HOC_BASE_URL='http://0.0.0.0:8080' ./hoc
|
||||||
```
|
```
|
||||||
|
|
||||||
When running the binary directly, you need a git binary in your `PATH`.
|
You can also use the Docker image:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ docker run -p 8080:8080 --env HOC_BASE_URL='http://0.0.0.0:8080' -it --rm vbrandl/hits-of-code
|
||||||
|
```
|
||||||
|
|
||||||
|
When running the binary directly, you need a `git` binary in your `PATH`.
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
7
build.rs
7
build.rs
@ -2,11 +2,12 @@ extern crate ructe;
|
|||||||
extern crate vergen;
|
extern crate vergen;
|
||||||
|
|
||||||
use ructe::Ructe;
|
use ructe::Ructe;
|
||||||
use vergen::{generate_cargo_keys, ConstantsFlags};
|
use vergen::{vergen, Config, ShaKind};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let flags = ConstantsFlags::SHA_SHORT;
|
let mut config = Config::default();
|
||||||
generate_cargo_keys(flags).expect("Unable to generate the cargo keys!");
|
*config.git_mut().sha_kind_mut() = ShaKind::Short;
|
||||||
|
vergen(config).expect("Unable to generate static repo info");
|
||||||
Ructe::from_env()
|
Ructe::from_env()
|
||||||
.expect("ructe")
|
.expect("ructe")
|
||||||
.compile_templates("templates")
|
.compile_templates("templates")
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
version: "2"
|
version: "3"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
hoc:
|
hoc:
|
||||||
@ -9,3 +9,5 @@ services:
|
|||||||
# ports:
|
# ports:
|
||||||
# - "127.0.0.1:8080:8080"
|
# - "127.0.0.1:8080:8080"
|
||||||
restart: always
|
restart: always
|
||||||
|
env_file:
|
||||||
|
- ./.env
|
||||||
|
15
hoc.toml
Normal file
15
hoc.toml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# every parameter can also be set (or overwritten) by passing an environment
|
||||||
|
# variable namend `HOC_<PARAMETERNAME>`, e.g.
|
||||||
|
# `HOC_BASE_URL='https://hitsofcode.com' ./hoc`
|
||||||
|
|
||||||
|
# these config parameters have default values and must not explicitly be set
|
||||||
|
repodir = "./repos"
|
||||||
|
cachedir = "./cache"
|
||||||
|
port = 8080
|
||||||
|
host = "0.0.0.0"
|
||||||
|
workers = 4
|
||||||
|
|
||||||
|
# these parameters don't have default values and must be set
|
||||||
|
|
||||||
|
# this should be the public base URL of the service, e.g. `https://hitsofcode.com`
|
||||||
|
base_url = "http://0.0.0.0:8080"
|
15
hoc.toml.example
Normal file
15
hoc.toml.example
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# every parameter can also be set (or overwritten) by passing an environment
|
||||||
|
# variable namend `HOC_<PARAMETERNAME>`, e.g.
|
||||||
|
# `HOC_BASE_URL='https://hitsofcode.com' ./hoc`
|
||||||
|
|
||||||
|
# these config parameters have default values and must not explicitly be set
|
||||||
|
repodir = "./repos"
|
||||||
|
cachedir = "./cache"
|
||||||
|
port = 8080
|
||||||
|
host = "0.0.0.0"
|
||||||
|
workers = 4
|
||||||
|
|
||||||
|
# these parameters don't have default values and must be set
|
||||||
|
|
||||||
|
# this should be the public base URL of the service, e.g. `https://hitsofcode.com`
|
||||||
|
base_url = "http://0.0.0.0:8080"
|
21
src/cache.rs
21
src/cache.rs
@ -8,6 +8,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
/// Enum to indicate the state of the cache
|
/// Enum to indicate the state of the cache
|
||||||
|
#[derive(Debug)]
|
||||||
pub(crate) enum CacheState<'a> {
|
pub(crate) enum CacheState<'a> {
|
||||||
/// Current head and cached head are the same
|
/// Current head and cached head are the same
|
||||||
Current {
|
Current {
|
||||||
@ -26,11 +27,13 @@ pub(crate) enum CacheState<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CacheState<'a> {
|
impl<'a> CacheState<'a> {
|
||||||
|
#[instrument]
|
||||||
pub(crate) fn read_from_file(
|
pub(crate) fn read_from_file(
|
||||||
path: impl AsRef<Path>,
|
path: impl AsRef<Path> + std::fmt::Debug,
|
||||||
branch: &str,
|
branch: &str,
|
||||||
head: &str,
|
head: &str,
|
||||||
) -> Result<CacheState<'a>> {
|
) -> Result<CacheState<'a>> {
|
||||||
|
trace!("Reading cache");
|
||||||
if path.as_ref().exists() {
|
if path.as_ref().exists() {
|
||||||
let cache: Cache = serde_json::from_reader(BufReader::new(File::open(path)?))?;
|
let cache: Cache = serde_json::from_reader(BufReader::new(File::open(path)?))?;
|
||||||
Ok(cache
|
Ok(cache
|
||||||
@ -38,6 +41,7 @@ impl<'a> CacheState<'a> {
|
|||||||
.get(branch)
|
.get(branch)
|
||||||
.map(|c| {
|
.map(|c| {
|
||||||
if c.head == head {
|
if c.head == head {
|
||||||
|
trace!("Cache is up to date");
|
||||||
CacheState::Current {
|
CacheState::Current {
|
||||||
count: c.count,
|
count: c.count,
|
||||||
commits: c.commits,
|
commits: c.commits,
|
||||||
@ -45,6 +49,7 @@ impl<'a> CacheState<'a> {
|
|||||||
cache: cache.clone(),
|
cache: cache.clone(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
trace!("Cache is out of date");
|
||||||
CacheState::Old {
|
CacheState::Old {
|
||||||
head: c.head.to_string(),
|
head: c.head.to_string(),
|
||||||
// TODO: get rid of clone
|
// TODO: get rid of clone
|
||||||
@ -59,6 +64,7 @@ impl<'a> CacheState<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument]
|
||||||
pub(crate) fn calculate_new_cache(
|
pub(crate) fn calculate_new_cache(
|
||||||
self,
|
self,
|
||||||
count: u64,
|
count: u64,
|
||||||
@ -66,6 +72,7 @@ impl<'a> CacheState<'a> {
|
|||||||
head: Cow<'a, str>,
|
head: Cow<'a, str>,
|
||||||
branch: &'a str,
|
branch: &'a str,
|
||||||
) -> Cache<'a> {
|
) -> Cache<'a> {
|
||||||
|
trace!("Calculating new cache");
|
||||||
match self {
|
match self {
|
||||||
CacheState::Old { mut cache, .. } => {
|
CacheState::Old { mut cache, .. } => {
|
||||||
if let Some(mut cache) = cache.entries.get_mut(branch) {
|
if let Some(mut cache) = cache.entries.get_mut(branch) {
|
||||||
@ -77,6 +84,7 @@ impl<'a> CacheState<'a> {
|
|||||||
}
|
}
|
||||||
CacheState::Current { cache, .. } => cache,
|
CacheState::Current { cache, .. } => cache,
|
||||||
CacheState::NoneForBranch(mut cache) => {
|
CacheState::NoneForBranch(mut cache) => {
|
||||||
|
trace!("Creating new cache for branch");
|
||||||
cache.entries.insert(
|
cache.entries.insert(
|
||||||
branch.into(),
|
branch.into(),
|
||||||
CacheEntry {
|
CacheEntry {
|
||||||
@ -88,13 +96,14 @@ impl<'a> CacheState<'a> {
|
|||||||
cache
|
cache
|
||||||
}
|
}
|
||||||
CacheState::No => {
|
CacheState::No => {
|
||||||
|
trace!("Creating new cache file");
|
||||||
let mut entries = HashMap::with_capacity(1);
|
let mut entries = HashMap::with_capacity(1);
|
||||||
entries.insert(
|
entries.insert(
|
||||||
branch.into(),
|
branch.into(),
|
||||||
CacheEntry {
|
CacheEntry {
|
||||||
commits,
|
|
||||||
head,
|
head,
|
||||||
count,
|
count,
|
||||||
|
commits,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
Cache { entries }
|
Cache { entries }
|
||||||
@ -103,12 +112,12 @@ impl<'a> CacheState<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
pub(crate) struct Cache<'a> {
|
pub(crate) struct Cache<'a> {
|
||||||
pub entries: HashMap<Cow<'a, str>, CacheEntry<'a>>,
|
pub entries: HashMap<Cow<'a, str>, CacheEntry<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
pub(crate) struct CacheEntry<'a> {
|
pub(crate) struct CacheEntry<'a> {
|
||||||
/// HEAD commit ref
|
/// HEAD commit ref
|
||||||
pub head: Cow<'a, str>,
|
pub head: Cow<'a, str>,
|
||||||
@ -119,7 +128,9 @@ pub(crate) struct CacheEntry<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Cache<'a> {
|
impl<'a> Cache<'a> {
|
||||||
pub(crate) fn write_to_file(&self, path: impl AsRef<Path>) -> Result<()> {
|
#[instrument]
|
||||||
|
pub(crate) fn write_to_file(&self, path: impl AsRef<Path> + std::fmt::Debug) -> Result<()> {
|
||||||
|
trace!("Persisting cache to disk");
|
||||||
create_dir_all(path.as_ref().parent().ok_or(Error::Internal)?)?;
|
create_dir_all(path.as_ref().parent().ok_or(Error::Internal)?)?;
|
||||||
serde_json::to_writer(
|
serde_json::to_writer(
|
||||||
OpenOptions::new()
|
OpenOptions::new()
|
||||||
|
@ -1,70 +1,38 @@
|
|||||||
use crate::{error::Result, statics::OPT};
|
use config::{Config, ConfigError, Environment, File};
|
||||||
use log::LevelFilter;
|
|
||||||
use log4rs::{
|
|
||||||
append::{console::ConsoleAppender, file::FileAppender},
|
|
||||||
config::{Appender, Config, Root},
|
|
||||||
encode::pattern::PatternEncoder,
|
|
||||||
};
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use structopt::StructOpt;
|
|
||||||
|
|
||||||
#[derive(StructOpt, Debug)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub(crate) struct Opt {
|
pub struct Settings {
|
||||||
#[structopt(
|
|
||||||
short = "o",
|
|
||||||
long = "outdir",
|
|
||||||
parse(from_os_str),
|
|
||||||
default_value = "./repos"
|
|
||||||
)]
|
|
||||||
/// Path to store cloned repositories
|
/// Path to store cloned repositories
|
||||||
pub(crate) outdir: PathBuf,
|
pub repodir: PathBuf,
|
||||||
#[structopt(
|
|
||||||
short = "c",
|
|
||||||
long = "cachedir",
|
|
||||||
parse(from_os_str),
|
|
||||||
default_value = "./cache"
|
|
||||||
)]
|
|
||||||
/// Path to store cache
|
/// Path to store cache
|
||||||
pub(crate) cachedir: PathBuf,
|
pub cachedir: PathBuf,
|
||||||
#[structopt(short = "p", long = "port", default_value = "8080")]
|
|
||||||
/// Port to listen on
|
/// Port to listen on
|
||||||
pub(crate) port: u16,
|
pub port: u16,
|
||||||
#[structopt(short = "h", long = "host", default_value = "0.0.0.0")]
|
|
||||||
/// Interface to listen on
|
/// Interface to listen on
|
||||||
pub(crate) host: String,
|
pub host: String,
|
||||||
#[structopt(short = "d", long = "domain", default_value = "hitsofcode.com")]
|
/// Base URL
|
||||||
/// Interface to listen on
|
pub base_url: String,
|
||||||
pub(crate) domain: String,
|
|
||||||
#[structopt(short = "w", long = "workers", default_value = "4")]
|
|
||||||
/// Number of worker threads
|
/// Number of worker threads
|
||||||
pub(crate) workers: usize,
|
pub workers: usize,
|
||||||
#[structopt(
|
|
||||||
short = "l",
|
|
||||||
long = "logfile",
|
|
||||||
parse(from_os_str),
|
|
||||||
default_value = "./hoc.log"
|
|
||||||
)]
|
|
||||||
/// The logfile
|
|
||||||
pub(crate) logfile: PathBuf,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn init() -> Result<()> {
|
impl Settings {
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info,hoc=info");
|
#[deprecated]
|
||||||
openssl_probe::init_ssl_cert_env_vars();
|
pub fn new() -> Result<Self, ConfigError> {
|
||||||
let stdout = ConsoleAppender::builder().build();
|
Self::load()
|
||||||
let file = FileAppender::builder()
|
}
|
||||||
.encoder(Box::new(PatternEncoder::new("{d} - {m}{n}")))
|
|
||||||
.build(&OPT.logfile)
|
pub fn load() -> Result<Self, ConfigError> {
|
||||||
.unwrap();
|
let mut config = Config::new();
|
||||||
let config = Config::builder()
|
config
|
||||||
.appender(Appender::builder().build("stdout", Box::new(stdout)))
|
.merge(File::with_name("hoc.toml").required(false))?
|
||||||
.appender(Appender::builder().build("file", Box::new(file)))
|
.merge(Environment::with_prefix("hoc"))?
|
||||||
.build(
|
.set_default("repodir", "./repos")?
|
||||||
Root::builder()
|
.set_default("cachedir", "./cache")?
|
||||||
.appender("stdout")
|
.set_default("workers", 4)?
|
||||||
.appender("file")
|
.set_default("port", 8080)?
|
||||||
.build(LevelFilter::Info),
|
.set_default("host", "0.0.0.0")?;
|
||||||
)?;
|
config.try_into()
|
||||||
log4rs::init_config(config)?;
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use std::{fs::read_dir, path::Path, result::Result as StdResult};
|
use std::{fs::read_dir, path::Path, result::Result as StdResult};
|
||||||
|
|
||||||
|
#[instrument]
|
||||||
pub(crate) fn count_repositories<P>(repo_path: P) -> Result<usize>
|
pub(crate) fn count_repositories<P>(repo_path: P) -> Result<usize>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path> + std::fmt::Debug,
|
||||||
{
|
{
|
||||||
|
trace!("Counting repositories");
|
||||||
std::fs::create_dir_all(&repo_path)?;
|
std::fs::create_dir_all(&repo_path)?;
|
||||||
Ok(read_dir(repo_path)?
|
Ok(read_dir(repo_path)?
|
||||||
.filter_map(StdResult::ok)
|
.filter_map(StdResult::ok)
|
||||||
|
42
src/error.rs
42
src/error.rs
@ -1,9 +1,6 @@
|
|||||||
use crate::{
|
use crate::{statics::VERSION_INFO, templates};
|
||||||
statics::{REPO_COUNT, VERSION_INFO},
|
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
||||||
templates,
|
use std::fmt;
|
||||||
};
|
|
||||||
use actix_web::{HttpResponse, ResponseError};
|
|
||||||
use std::{fmt, sync::atomic::Ordering};
|
|
||||||
|
|
||||||
pub(crate) type Result<T> = std::result::Result<T, Error>;
|
pub(crate) type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
@ -14,8 +11,6 @@ pub(crate) enum Error {
|
|||||||
Git(git2::Error),
|
Git(git2::Error),
|
||||||
Internal,
|
Internal,
|
||||||
Io(std::io::Error),
|
Io(std::io::Error),
|
||||||
Log(log::SetLoggerError),
|
|
||||||
LogBuilder(log4rs::config::Errors),
|
|
||||||
Parse(std::num::ParseIntError),
|
Parse(std::num::ParseIntError),
|
||||||
Serial(serde_json::Error),
|
Serial(serde_json::Error),
|
||||||
BranchNotFound,
|
BranchNotFound,
|
||||||
@ -29,8 +24,6 @@ impl fmt::Display for Error {
|
|||||||
Error::Git(e) => write!(fmt, "Git({})", e),
|
Error::Git(e) => write!(fmt, "Git({})", e),
|
||||||
Error::Internal => write!(fmt, "Internal Error"),
|
Error::Internal => write!(fmt, "Internal Error"),
|
||||||
Error::Io(e) => write!(fmt, "Io({})", e),
|
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::Parse(e) => write!(fmt, "Parse({})", e),
|
||||||
Error::Serial(e) => write!(fmt, "Serial({})", e),
|
Error::Serial(e) => write!(fmt, "Serial({})", e),
|
||||||
Error::BranchNotFound => write!(fmt, "Repo doesn't have master branch"),
|
Error::BranchNotFound => write!(fmt, "Repo doesn't have master branch"),
|
||||||
@ -39,21 +32,22 @@ impl fmt::Display for Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ResponseError for Error {
|
impl ResponseError for Error {
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
match self {
|
||||||
|
Error::BranchNotFound => StatusCode::NOT_FOUND,
|
||||||
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn error_response(&self) -> HttpResponse {
|
fn error_response(&self) -> HttpResponse {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
match self {
|
match self {
|
||||||
Error::BranchNotFound => {
|
Error::BranchNotFound => {
|
||||||
templates::p404_no_master(
|
templates::p404_no_master(&mut buf, VERSION_INFO, 0).unwrap();
|
||||||
&mut buf,
|
|
||||||
VERSION_INFO,
|
|
||||||
REPO_COUNT.load(Ordering::Relaxed),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
HttpResponse::NotFound().content_type("text/html").body(buf)
|
HttpResponse::NotFound().content_type("text/html").body(buf)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
templates::p500(&mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed))
|
templates::p500(&mut buf, VERSION_INFO, 0).unwrap();
|
||||||
.unwrap();
|
|
||||||
HttpResponse::InternalServerError()
|
HttpResponse::InternalServerError()
|
||||||
.content_type("text/html")
|
.content_type("text/html")
|
||||||
.body(buf)
|
.body(buf)
|
||||||
@ -76,12 +70,6 @@ impl From<git2::Error> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<log::SetLoggerError> for Error {
|
|
||||||
fn from(err: log::SetLoggerError) -> Self {
|
|
||||||
Error::Log(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<std::io::Error> for Error {
|
impl From<std::io::Error> for Error {
|
||||||
fn from(err: std::io::Error) -> Self {
|
fn from(err: std::io::Error) -> Self {
|
||||||
Error::Io(err)
|
Error::Io(err)
|
||||||
@ -100,12 +88,6 @@ impl From<reqwest::Error> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<log4rs::config::Errors> for Error {
|
|
||||||
fn from(err: log4rs::config::Errors) -> Self {
|
|
||||||
Error::LogBuilder(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<std::num::ParseIntError> for Error {
|
impl From<std::num::ParseIntError> for Error {
|
||||||
fn from(err: std::num::ParseIntError) -> Self {
|
fn from(err: std::num::ParseIntError) -> Self {
|
||||||
Error::Parse(err)
|
Error::Parse(err)
|
||||||
|
525
src/lib.rs
Normal file
525
src/lib.rs
Normal file
@ -0,0 +1,525 @@
|
|||||||
|
#![type_length_limit = "2257138"]
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate actix_web;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate tracing;
|
||||||
|
|
||||||
|
mod cache;
|
||||||
|
pub mod config;
|
||||||
|
mod count;
|
||||||
|
mod error;
|
||||||
|
mod service;
|
||||||
|
mod statics;
|
||||||
|
pub mod telemetry;
|
||||||
|
mod template;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
cache::CacheState,
|
||||||
|
config::Settings,
|
||||||
|
error::{Error, Result},
|
||||||
|
service::{Bitbucket, FormService, GitHub, Gitlab, Service},
|
||||||
|
statics::{CLIENT, CSS, FAVICON, VERSION_INFO},
|
||||||
|
template::RepoInfo,
|
||||||
|
};
|
||||||
|
use actix_web::{
|
||||||
|
dev::Server,
|
||||||
|
http::header::{CacheControl, CacheDirective, Expires, LOCATION},
|
||||||
|
middleware::{self, normalize::TrailingSlash},
|
||||||
|
web, App, HttpResponse, HttpServer, Responder,
|
||||||
|
};
|
||||||
|
use badge::{Badge, BadgeOptions};
|
||||||
|
use git2::{BranchType, Repository};
|
||||||
|
use number_prefix::NumberPrefix;
|
||||||
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
|
fs::create_dir_all,
|
||||||
|
io,
|
||||||
|
net::TcpListener,
|
||||||
|
path::Path,
|
||||||
|
process::Command,
|
||||||
|
sync::atomic::AtomicUsize,
|
||||||
|
sync::atomic::Ordering,
|
||||||
|
time::{Duration, SystemTime},
|
||||||
|
};
|
||||||
|
use tracing::Instrument;
|
||||||
|
|
||||||
|
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
struct GeneratorForm<'a> {
|
||||||
|
service: FormService,
|
||||||
|
user: Cow<'a, str>,
|
||||||
|
repo: Cow<'a, str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct State {
|
||||||
|
settings: Settings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
fn repos(&self) -> String {
|
||||||
|
self.settings.repodir.display().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cache(&self) -> String {
|
||||||
|
self.settings.cachedir.display().to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct JsonResponse<'a> {
|
||||||
|
head: &'a str,
|
||||||
|
branch: &'a str,
|
||||||
|
count: u64,
|
||||||
|
commits: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
struct BranchQuery {
|
||||||
|
branch: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pull(path: impl AsRef<Path>) -> Result<()> {
|
||||||
|
let repo = Repository::open_bare(path)?;
|
||||||
|
let mut origin = repo.find_remote("origin")?;
|
||||||
|
origin.fetch(&["refs/heads/*:refs/heads/*"], None, None)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hoc(repo: &str, repo_dir: &str, cache_dir: &str, branch: &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 repo = Repository::open_bare(&repo_dir)?;
|
||||||
|
// TODO: do better...
|
||||||
|
let head = repo
|
||||||
|
.find_branch(branch, BranchType::Local)
|
||||||
|
.map_err(|_| Error::BranchNotFound)?
|
||||||
|
.into_reference();
|
||||||
|
let head = format!("{}", head.target().ok_or(Error::BranchNotFound)?);
|
||||||
|
let mut arg_commit_count = vec!["rev-list".to_string(), "--count".to_string()];
|
||||||
|
let mut arg = vec![
|
||||||
|
"log".to_string(),
|
||||||
|
"--pretty=tformat:".to_string(),
|
||||||
|
"--numstat".to_string(),
|
||||||
|
"--ignore-space-change".to_string(),
|
||||||
|
"--ignore-all-space".to_string(),
|
||||||
|
"--ignore-submodules".to_string(),
|
||||||
|
"--no-color".to_string(),
|
||||||
|
"--find-copies-harder".to_string(),
|
||||||
|
"-M".to_string(),
|
||||||
|
"--diff-filter=ACDM".to_string(),
|
||||||
|
];
|
||||||
|
let cache = CacheState::read_from_file(&cache_dir, branch, &head)?;
|
||||||
|
match &cache {
|
||||||
|
CacheState::Current { count, commits, .. } => {
|
||||||
|
info!("Using cache");
|
||||||
|
return Ok((*count, head, *commits));
|
||||||
|
}
|
||||||
|
CacheState::Old { head, .. } => {
|
||||||
|
info!("Updating cache");
|
||||||
|
arg.push(format!("{}..{}", head, branch));
|
||||||
|
arg_commit_count.push(format!("{}..{}", head, branch));
|
||||||
|
}
|
||||||
|
CacheState::No | CacheState::NoneForBranch(..) => {
|
||||||
|
info!("Creating cache");
|
||||||
|
arg.push(branch.to_string());
|
||||||
|
arg_commit_count.push(branch.to_string());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
arg.push("--".to_string());
|
||||||
|
arg.push(".".to_string());
|
||||||
|
let output = Command::new("git")
|
||||||
|
.args(&arg)
|
||||||
|
.current_dir(&repo_dir)
|
||||||
|
.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| {
|
||||||
|
s.split_whitespace()
|
||||||
|
.take(2)
|
||||||
|
.map(str::parse::<u64>)
|
||||||
|
.filter_map(std::result::Result::ok)
|
||||||
|
.sum::<u64>()
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
let cache = cache.calculate_new_cache(count, commits, (&head).into(), branch);
|
||||||
|
cache.write_to_file(cache_dir)?;
|
||||||
|
|
||||||
|
Ok((count, head, commits))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn remote_exists(url: &str) -> Result<bool> {
|
||||||
|
let resp = CLIENT.head(url).send().await?;
|
||||||
|
Ok(resp.status() == reqwest::StatusCode::OK)
|
||||||
|
}
|
||||||
|
|
||||||
|
enum HocResult {
|
||||||
|
Hoc {
|
||||||
|
hoc: u64,
|
||||||
|
commits: u64,
|
||||||
|
hoc_pretty: String,
|
||||||
|
head: String,
|
||||||
|
url: String,
|
||||||
|
repo: String,
|
||||||
|
service_path: String,
|
||||||
|
},
|
||||||
|
NotFound,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_repo_and_cache<T>(
|
||||||
|
state: web::Data<State>,
|
||||||
|
repo_count: web::Data<AtomicUsize>,
|
||||||
|
data: web::Path<(String, String)>,
|
||||||
|
) -> Result<impl Responder>
|
||||||
|
where
|
||||||
|
T: Service,
|
||||||
|
{
|
||||||
|
let data = data.into_inner();
|
||||||
|
let span = info_span!(
|
||||||
|
"deleting repository and cache",
|
||||||
|
service = T::domain(),
|
||||||
|
user = data.0.as_str(),
|
||||||
|
repo = data.1.as_str()
|
||||||
|
);
|
||||||
|
let future = async {
|
||||||
|
let repo = format!(
|
||||||
|
"{}/{}/{}",
|
||||||
|
T::domain(),
|
||||||
|
data.0.to_lowercase(),
|
||||||
|
data.1.to_lowercase()
|
||||||
|
);
|
||||||
|
info!("Deleting cache and repository");
|
||||||
|
let cache_dir = format!("{}/{}.json", &state.cache(), repo);
|
||||||
|
let repo_dir = format!("{}/{}", &state.repos(), repo);
|
||||||
|
std::fs::remove_file(&cache_dir).or_else(|e| {
|
||||||
|
if e.kind() == io::ErrorKind::NotFound {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
std::fs::remove_dir_all(&repo_dir).or_else(|e| {
|
||||||
|
if e.kind() == io::ErrorKind::NotFound {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
repo_count.fetch_sub(1, Ordering::Relaxed);
|
||||||
|
Ok(HttpResponse::TemporaryRedirect()
|
||||||
|
.header(
|
||||||
|
LOCATION,
|
||||||
|
format!("/{}/{}/{}/view", T::url_path(), data.0, data.1),
|
||||||
|
)
|
||||||
|
.finish())
|
||||||
|
};
|
||||||
|
future.instrument(span).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_hoc_request<T, F>(
|
||||||
|
state: web::Data<State>,
|
||||||
|
repo_count: web::Data<AtomicUsize>,
|
||||||
|
data: web::Path<(String, String)>,
|
||||||
|
branch: &str,
|
||||||
|
mapper: F,
|
||||||
|
) -> Result<HttpResponse>
|
||||||
|
where
|
||||||
|
T: Service,
|
||||||
|
F: FnOnce(HocResult) -> Result<HttpResponse>,
|
||||||
|
{
|
||||||
|
let data = data.into_inner();
|
||||||
|
let span = info_span!(
|
||||||
|
"handling hoc calculation",
|
||||||
|
service = T::domain(),
|
||||||
|
user = data.0.as_str(),
|
||||||
|
repo = data.1.as_str(),
|
||||||
|
branch
|
||||||
|
);
|
||||||
|
let future = async {
|
||||||
|
let repo = format!("{}/{}", data.0.to_lowercase(), data.1.to_lowercase());
|
||||||
|
let service_path = format!("{}/{}", T::url_path(), repo);
|
||||||
|
let service_url = format!("{}/{}", T::domain(), repo);
|
||||||
|
let path = format!("{}/{}", state.repos(), service_url);
|
||||||
|
let url = format!("https://{}", service_url);
|
||||||
|
let remote_exists = remote_exists(&url).await?;
|
||||||
|
let file = Path::new(&path);
|
||||||
|
if !file.exists() {
|
||||||
|
if !remote_exists {
|
||||||
|
warn!("Repository does not exist");
|
||||||
|
return mapper(HocResult::NotFound);
|
||||||
|
}
|
||||||
|
info!("Cloning for the first time");
|
||||||
|
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_url, &state.repos(), &state.cache(), branch)?;
|
||||||
|
let hoc_pretty = match NumberPrefix::decimal(hoc as f64) {
|
||||||
|
NumberPrefix::Standalone(hoc) => hoc.to_string(),
|
||||||
|
NumberPrefix::Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
|
||||||
|
};
|
||||||
|
let res = HocResult::Hoc {
|
||||||
|
hoc,
|
||||||
|
commits,
|
||||||
|
hoc_pretty,
|
||||||
|
head,
|
||||||
|
url,
|
||||||
|
repo,
|
||||||
|
service_path,
|
||||||
|
};
|
||||||
|
mapper(res)
|
||||||
|
};
|
||||||
|
future.instrument(span).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn json_hoc<T: Service>(
|
||||||
|
state: web::Data<State>,
|
||||||
|
repo_count: web::Data<AtomicUsize>,
|
||||||
|
data: web::Path<(String, String)>,
|
||||||
|
branch: web::Query<BranchQuery>,
|
||||||
|
) -> Result<HttpResponse> {
|
||||||
|
let branch = branch.branch.as_deref().unwrap_or("master");
|
||||||
|
let rc_clone = repo_count.clone();
|
||||||
|
let mapper = move |r| match r {
|
||||||
|
HocResult::NotFound => p404(rc_clone),
|
||||||
|
HocResult::Hoc {
|
||||||
|
hoc, head, commits, ..
|
||||||
|
} => Ok(HttpResponse::Ok().json(JsonResponse {
|
||||||
|
branch,
|
||||||
|
head: &head,
|
||||||
|
count: hoc,
|
||||||
|
commits,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
handle_hoc_request::<T, _>(state, repo_count, data, branch, mapper).await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn no_cache_response(body: Vec<u8>) -> HttpResponse {
|
||||||
|
let expiration = SystemTime::now() + Duration::from_secs(30);
|
||||||
|
HttpResponse::Ok()
|
||||||
|
.content_type("image/svg+xml")
|
||||||
|
.set(Expires(expiration.into()))
|
||||||
|
.set(CacheControl(vec![
|
||||||
|
CacheDirective::MaxAge(0u32),
|
||||||
|
CacheDirective::MustRevalidate,
|
||||||
|
CacheDirective::NoCache,
|
||||||
|
CacheDirective::NoStore,
|
||||||
|
]))
|
||||||
|
.body(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn calculate_hoc<T: Service>(
|
||||||
|
state: web::Data<State>,
|
||||||
|
repo_count: web::Data<AtomicUsize>,
|
||||||
|
data: web::Path<(String, String)>,
|
||||||
|
branch: web::Query<BranchQuery>,
|
||||||
|
) -> HttpResponse {
|
||||||
|
let rc_clone = repo_count.clone();
|
||||||
|
let mapper = move |r| match r {
|
||||||
|
HocResult::NotFound => p404(rc_clone),
|
||||||
|
HocResult::Hoc { hoc_pretty, .. } => {
|
||||||
|
let badge_opt = BadgeOptions {
|
||||||
|
subject: "Hits-of-Code".to_string(),
|
||||||
|
color: "#007ec6".to_string(),
|
||||||
|
status: hoc_pretty,
|
||||||
|
};
|
||||||
|
let badge = Badge::new(badge_opt)?;
|
||||||
|
// TODO: remove clone
|
||||||
|
let body = badge.to_svg().as_bytes().to_vec();
|
||||||
|
|
||||||
|
Ok(no_cache_response(body))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let branch = branch.branch.as_deref().unwrap_or("master");
|
||||||
|
let error_badge = |_| {
|
||||||
|
let error_badge = Badge::new(BadgeOptions {
|
||||||
|
subject: "Hits-of-Code".to_string(),
|
||||||
|
color: "#ff0000".to_string(),
|
||||||
|
status: "error".to_string(),
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let body = error_badge.to_svg().as_bytes().to_vec();
|
||||||
|
no_cache_response(body)
|
||||||
|
};
|
||||||
|
handle_hoc_request::<T, _>(state, repo_count, data, branch, mapper)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(error_badge)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn overview<T: Service>(
|
||||||
|
state: web::Data<State>,
|
||||||
|
repo_count: web::Data<AtomicUsize>,
|
||||||
|
data: web::Path<(String, String)>,
|
||||||
|
branch: web::Query<BranchQuery>,
|
||||||
|
) -> Result<HttpResponse> {
|
||||||
|
let branch = branch.branch.as_deref().unwrap_or("master");
|
||||||
|
let base_url = state.settings.base_url.clone();
|
||||||
|
let rc_clone = repo_count.clone();
|
||||||
|
let mapper = move |r| match r {
|
||||||
|
HocResult::NotFound => p404(rc_clone),
|
||||||
|
HocResult::Hoc {
|
||||||
|
hoc,
|
||||||
|
commits,
|
||||||
|
hoc_pretty,
|
||||||
|
url,
|
||||||
|
head,
|
||||||
|
repo,
|
||||||
|
service_path,
|
||||||
|
} => {
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
let repo_info = RepoInfo {
|
||||||
|
commit_url: &T::commit_url(&repo, &head),
|
||||||
|
commits,
|
||||||
|
base_url: &base_url,
|
||||||
|
head: &head,
|
||||||
|
hoc,
|
||||||
|
hoc_pretty: &hoc_pretty,
|
||||||
|
path: &service_path,
|
||||||
|
url: &url,
|
||||||
|
branch,
|
||||||
|
};
|
||||||
|
templates::overview(
|
||||||
|
&mut buf,
|
||||||
|
VERSION_INFO,
|
||||||
|
rc_clone.load(Ordering::Relaxed),
|
||||||
|
repo_info,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
handle_hoc_request::<T, _>(state, repo_count, data, branch, mapper).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/health_check")]
|
||||||
|
async fn health_check() -> HttpResponse {
|
||||||
|
HttpResponse::Ok().finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
async fn index(
|
||||||
|
state: web::Data<State>,
|
||||||
|
repo_count: web::Data<AtomicUsize>,
|
||||||
|
) -> Result<HttpResponse> {
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
templates::index(
|
||||||
|
&mut buf,
|
||||||
|
VERSION_INFO,
|
||||||
|
repo_count.load(Ordering::Relaxed),
|
||||||
|
&state.settings.base_url,
|
||||||
|
)?;
|
||||||
|
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/generate")]
|
||||||
|
async fn generate(
|
||||||
|
params: web::Form<GeneratorForm<'_>>,
|
||||||
|
state: web::Data<State>,
|
||||||
|
repo_count: web::Data<AtomicUsize>,
|
||||||
|
) -> Result<HttpResponse> {
|
||||||
|
let repo = format!("{}/{}", params.user, params.repo);
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
templates::generate(
|
||||||
|
&mut buf,
|
||||||
|
VERSION_INFO,
|
||||||
|
repo_count.load(Ordering::Relaxed),
|
||||||
|
&state.settings.base_url,
|
||||||
|
params.service.url(),
|
||||||
|
params.service.service(),
|
||||||
|
&repo,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn p404(repo_count: web::Data<AtomicUsize>) -> 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn async_p404(repo_count: web::Data<AtomicUsize>) -> Result<HttpResponse> {
|
||||||
|
p404(repo_count)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn css() -> HttpResponse {
|
||||||
|
HttpResponse::Ok().content_type("text/css").body(CSS)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn favicon32() -> HttpResponse {
|
||||||
|
HttpResponse::Ok().content_type("image/png").body(FAVICON)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn start_server(listener: TcpListener, settings: Settings) -> std::io::Result<Server> {
|
||||||
|
let workers = settings.workers;
|
||||||
|
let repo_count =
|
||||||
|
// TODO: errorhandling
|
||||||
|
web::Data::new(AtomicUsize::new(count::count_repositories(&settings.repodir).unwrap()));
|
||||||
|
let state = web::Data::new(State { settings });
|
||||||
|
Ok(HttpServer::new(move || {
|
||||||
|
App::new()
|
||||||
|
.app_data(state.clone())
|
||||||
|
.app_data(repo_count.clone())
|
||||||
|
.wrap(tracing_actix_web::TracingLogger)
|
||||||
|
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
|
||||||
|
.service(index)
|
||||||
|
.service(health_check)
|
||||||
|
.service(web::resource("/tacit-css.min.css").route(web::get().to(css)))
|
||||||
|
.service(web::resource("/favicon.ico").route(web::get().to(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("/github/{user}/{repo}/delete")
|
||||||
|
.route(web::post().to(delete_repo_and_cache::<GitHub>)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/gitlab/{user}/{repo}/delete")
|
||||||
|
.route(web::post().to(delete_repo_and_cache::<Gitlab>)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/bitbucket/{user}/{repo}/delete")
|
||||||
|
.route(web::post().to(delete_repo_and_cache::<Bitbucket>)),
|
||||||
|
)
|
||||||
|
.service(web::resource("/github/{user}/{repo}/json").to(json_hoc::<GitHub>))
|
||||||
|
.service(web::resource("/gitlab/{user}/{repo}/json").to(json_hoc::<Gitlab>))
|
||||||
|
.service(web::resource("/bitbucket/{user}/{repo}/json").to(json_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>))
|
||||||
|
.service(web::resource("/github/{user}/{repo}/view").to(overview::<GitHub>))
|
||||||
|
.service(web::resource("/gitlab/{user}/{repo}/view").to(overview::<Gitlab>))
|
||||||
|
.service(web::resource("/bitbucket/{user}/{repo}/view").to(overview::<Bitbucket>))
|
||||||
|
.default_service(web::resource("").route(web::get().to(async_p404)))
|
||||||
|
})
|
||||||
|
.workers(workers)
|
||||||
|
.listen(listener)?
|
||||||
|
.run())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(listener: TcpListener, settings: Settings) -> std::io::Result<Server> {
|
||||||
|
let span = info_span!("hoc", version = env!("CARGO_PKG_VERSION"));
|
||||||
|
let _ = span.enter();
|
||||||
|
start_server(listener, settings).instrument(span).await
|
||||||
|
}
|
465
src/main.rs
465
src/main.rs
@ -1,455 +1,26 @@
|
|||||||
#![type_length_limit = "2257138"]
|
use hoc::{config::Settings, telemetry};
|
||||||
|
|
||||||
#[macro_use]
|
use std::net::TcpListener;
|
||||||
extern crate actix_web;
|
|
||||||
#[macro_use]
|
|
||||||
extern crate lazy_static;
|
|
||||||
#[macro_use]
|
|
||||||
extern crate log;
|
|
||||||
#[macro_use]
|
|
||||||
extern crate serde_derive;
|
|
||||||
|
|
||||||
mod cache;
|
fn init() {
|
||||||
mod config;
|
dotenv::dotenv().ok();
|
||||||
mod count;
|
openssl_probe::init_ssl_cert_env_vars();
|
||||||
mod error;
|
|
||||||
mod service;
|
|
||||||
mod statics;
|
|
||||||
mod template;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
telemetry::init_subscriber(telemetry::get_subscriber("hoc", "info"))
|
||||||
mod tests;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
cache::CacheState,
|
|
||||||
error::{Error, Result},
|
|
||||||
service::{Bitbucket, FormService, GitHub, Gitlab, Service},
|
|
||||||
statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO},
|
|
||||||
template::RepoInfo,
|
|
||||||
};
|
|
||||||
use actix_web::{
|
|
||||||
http::header::{CacheControl, CacheDirective, Expires, LOCATION},
|
|
||||||
middleware::{self, normalize::TrailingSlash},
|
|
||||||
web, App, HttpResponse, HttpServer, Responder,
|
|
||||||
};
|
|
||||||
use badge::{Badge, BadgeOptions};
|
|
||||||
use git2::{BranchType, Repository};
|
|
||||||
use number_prefix::NumberPrefix;
|
|
||||||
use std::{
|
|
||||||
borrow::Cow,
|
|
||||||
fs::create_dir_all,
|
|
||||||
io,
|
|
||||||
path::Path,
|
|
||||||
process::Command,
|
|
||||||
sync::atomic::Ordering,
|
|
||||||
sync::Arc,
|
|
||||||
time::{Duration, SystemTime},
|
|
||||||
};
|
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
|
||||||
struct GeneratorForm<'a> {
|
|
||||||
service: FormService,
|
|
||||||
user: Cow<'a, str>,
|
|
||||||
repo: Cow<'a, str>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct State {
|
|
||||||
repos: String,
|
|
||||||
cache: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
struct JsonResponse<'a> {
|
|
||||||
head: &'a str,
|
|
||||||
branch: &'a str,
|
|
||||||
count: u64,
|
|
||||||
commits: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct BranchQuery {
|
|
||||||
branch: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn pull(path: impl AsRef<Path>) -> Result<()> {
|
|
||||||
let repo = Repository::open_bare(path)?;
|
|
||||||
let mut origin = repo.find_remote("origin")?;
|
|
||||||
origin.fetch(&["refs/heads/*:refs/heads/*"], None, None)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hoc(repo: &str, repo_dir: &str, cache_dir: &str, branch: &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 repo = Repository::open_bare(&repo_dir)?;
|
|
||||||
// TODO: do better...
|
|
||||||
let head = repo
|
|
||||||
.find_branch(branch, BranchType::Local)
|
|
||||||
.map_err(|_| Error::BranchNotFound)?
|
|
||||||
.into_reference();
|
|
||||||
let head = format!("{}", head.target().ok_or(Error::BranchNotFound)?);
|
|
||||||
let mut arg_commit_count = vec!["rev-list".to_string(), "--count".to_string()];
|
|
||||||
let mut arg = vec![
|
|
||||||
"log".to_string(),
|
|
||||||
"--pretty=tformat:".to_string(),
|
|
||||||
"--numstat".to_string(),
|
|
||||||
"--ignore-space-change".to_string(),
|
|
||||||
"--ignore-all-space".to_string(),
|
|
||||||
"--ignore-submodules".to_string(),
|
|
||||||
"--no-color".to_string(),
|
|
||||||
"--find-copies-harder".to_string(),
|
|
||||||
"-M".to_string(),
|
|
||||||
"--diff-filter=ACDM".to_string(),
|
|
||||||
];
|
|
||||||
let cache = CacheState::read_from_file(&cache_dir, branch, &head)?;
|
|
||||||
match &cache {
|
|
||||||
CacheState::Current { count, commits, .. } => {
|
|
||||||
info!("Using cache for {}", repo_dir);
|
|
||||||
return Ok((*count, head, *commits));
|
|
||||||
}
|
|
||||||
CacheState::Old { head, .. } => {
|
|
||||||
info!("Updating cache for {}", repo_dir);
|
|
||||||
arg.push(format!("{}..{}", head, branch));
|
|
||||||
arg_commit_count.push(format!("{}..{}", head, branch));
|
|
||||||
}
|
|
||||||
CacheState::No | CacheState::NoneForBranch(..) => {
|
|
||||||
info!("Creating cache for {}", repo_dir);
|
|
||||||
arg.push(branch.to_string());
|
|
||||||
arg_commit_count.push(branch.to_string());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
arg.push("--".to_string());
|
|
||||||
arg.push(".".to_string());
|
|
||||||
let output = Command::new("git")
|
|
||||||
.args(&arg)
|
|
||||||
.current_dir(&repo_dir)
|
|
||||||
.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| {
|
|
||||||
s.split_whitespace()
|
|
||||||
.take(2)
|
|
||||||
.map(str::parse::<u64>)
|
|
||||||
.filter_map(std::result::Result::ok)
|
|
||||||
.sum::<u64>()
|
|
||||||
})
|
|
||||||
.sum();
|
|
||||||
|
|
||||||
let cache = cache.calculate_new_cache(count, commits, (&head).into(), branch);
|
|
||||||
cache.write_to_file(cache_dir)?;
|
|
||||||
|
|
||||||
Ok((count, head, commits))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn remote_exists(url: &str) -> Result<bool> {
|
|
||||||
let resp = CLIENT.head(url).send().await?;
|
|
||||||
Ok(resp.status() == reqwest::StatusCode::OK)
|
|
||||||
}
|
|
||||||
|
|
||||||
enum HocResult {
|
|
||||||
Hoc {
|
|
||||||
hoc: u64,
|
|
||||||
commits: u64,
|
|
||||||
hoc_pretty: String,
|
|
||||||
head: String,
|
|
||||||
url: String,
|
|
||||||
repo: String,
|
|
||||||
service_path: String,
|
|
||||||
},
|
|
||||||
NotFound,
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn delete_repo_and_cache<T>(
|
|
||||||
state: web::Data<Arc<State>>,
|
|
||||||
data: web::Path<(String, String)>,
|
|
||||||
) -> Result<impl Responder>
|
|
||||||
where
|
|
||||||
T: Service,
|
|
||||||
{
|
|
||||||
let data = data.into_inner();
|
|
||||||
let repo = format!(
|
|
||||||
"{}/{}/{}",
|
|
||||||
T::domain(),
|
|
||||||
data.0.to_lowercase(),
|
|
||||||
data.1.to_lowercase()
|
|
||||||
);
|
|
||||||
info!("Deleting cache and repository for {}", repo);
|
|
||||||
let cache_dir = format!("{}/{}.json", &state.cache, repo);
|
|
||||||
let repo_dir = format!("{}/{}", &state.repos, repo);
|
|
||||||
std::fs::remove_file(&cache_dir).or_else(|e| {
|
|
||||||
if e.kind() == io::ErrorKind::NotFound {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(e)
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
std::fs::remove_dir_all(&repo_dir).or_else(|e| {
|
|
||||||
if e.kind() == io::ErrorKind::NotFound {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(e)
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
REPO_COUNT.fetch_sub(1, Ordering::Relaxed);
|
|
||||||
Ok(HttpResponse::TemporaryRedirect()
|
|
||||||
.header(
|
|
||||||
LOCATION,
|
|
||||||
format!("/view/{}/{}/{}", T::url_path(), data.0, data.1),
|
|
||||||
)
|
|
||||||
.finish())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_hoc_request<T, F>(
|
|
||||||
state: web::Data<Arc<State>>,
|
|
||||||
data: web::Path<(String, String)>,
|
|
||||||
branch: &str,
|
|
||||||
mapper: F,
|
|
||||||
) -> Result<HttpResponse>
|
|
||||||
where
|
|
||||||
T: Service,
|
|
||||||
F: Fn(HocResult) -> Result<HttpResponse>,
|
|
||||||
{
|
|
||||||
let data = data.into_inner();
|
|
||||||
let repo = format!("{}/{}", data.0.to_lowercase(), data.1.to_lowercase());
|
|
||||||
let service_path = format!("{}/{}", T::url_path(), repo);
|
|
||||||
let service_url = format!("{}/{}", T::domain(), repo);
|
|
||||||
let path = format!("{}/{}", state.repos, service_url);
|
|
||||||
let url = format!("https://{}", service_url);
|
|
||||||
let remote_exists = remote_exists(&url).await?;
|
|
||||||
let file = Path::new(&path);
|
|
||||||
if !file.exists() {
|
|
||||||
if !remote_exists {
|
|
||||||
warn!("Repository does not exist: {}", url);
|
|
||||||
return mapper(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_url, &state.repos, &state.cache, branch)?;
|
|
||||||
let hoc_pretty = match NumberPrefix::decimal(hoc as f64) {
|
|
||||||
NumberPrefix::Standalone(hoc) => hoc.to_string(),
|
|
||||||
NumberPrefix::Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
|
|
||||||
};
|
|
||||||
let res = HocResult::Hoc {
|
|
||||||
hoc,
|
|
||||||
commits,
|
|
||||||
hoc_pretty,
|
|
||||||
head,
|
|
||||||
url,
|
|
||||||
repo,
|
|
||||||
service_path,
|
|
||||||
};
|
|
||||||
mapper(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) async fn json_hoc<T: Service>(
|
|
||||||
state: web::Data<Arc<State>>,
|
|
||||||
data: web::Path<(String, String)>,
|
|
||||||
branch: web::Query<BranchQuery>,
|
|
||||||
) -> Result<HttpResponse> {
|
|
||||||
let branch = branch.branch.as_deref().unwrap_or("master");
|
|
||||||
let mapper = |r| match r {
|
|
||||||
HocResult::NotFound => p404(),
|
|
||||||
HocResult::Hoc {
|
|
||||||
hoc, head, commits, ..
|
|
||||||
} => Ok(HttpResponse::Ok().json(JsonResponse {
|
|
||||||
branch,
|
|
||||||
head: &head,
|
|
||||||
count: hoc,
|
|
||||||
commits,
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
handle_hoc_request::<T, _>(state, data, branch, mapper).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) async fn calculate_hoc<T: Service>(
|
|
||||||
state: web::Data<Arc<State>>,
|
|
||||||
data: web::Path<(String, String)>,
|
|
||||||
branch: web::Query<BranchQuery>,
|
|
||||||
) -> Result<HttpResponse> {
|
|
||||||
let mapper = move |r| match r {
|
|
||||||
HocResult::NotFound => p404(),
|
|
||||||
HocResult::Hoc { hoc_pretty, .. } => {
|
|
||||||
let badge_opt = BadgeOptions {
|
|
||||||
subject: "Hits-of-Code".to_string(),
|
|
||||||
color: "#007ec6".to_string(),
|
|
||||||
status: hoc_pretty,
|
|
||||||
};
|
|
||||||
let badge = Badge::new(badge_opt)?;
|
|
||||||
// TODO: remove clone
|
|
||||||
let body = badge.to_svg().as_bytes().to_vec();
|
|
||||||
|
|
||||||
let expiration = SystemTime::now() + Duration::from_secs(30);
|
|
||||||
Ok(HttpResponse::Ok()
|
|
||||||
.content_type("image/svg+xml")
|
|
||||||
.set(Expires(expiration.into()))
|
|
||||||
.set(CacheControl(vec![
|
|
||||||
CacheDirective::MaxAge(0u32),
|
|
||||||
CacheDirective::MustRevalidate,
|
|
||||||
CacheDirective::NoCache,
|
|
||||||
CacheDirective::NoStore,
|
|
||||||
]))
|
|
||||||
.body(body))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let branch = branch.branch.as_deref().unwrap_or("master");
|
|
||||||
handle_hoc_request::<T, _>(state, data, branch, mapper).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn overview<T: Service>(
|
|
||||||
state: web::Data<Arc<State>>,
|
|
||||||
data: web::Path<(String, String)>,
|
|
||||||
branch: web::Query<BranchQuery>,
|
|
||||||
) -> Result<HttpResponse> {
|
|
||||||
let branch = branch.branch.as_deref().unwrap_or("master");
|
|
||||||
let mapper = |r| match r {
|
|
||||||
HocResult::NotFound => p404(),
|
|
||||||
HocResult::Hoc {
|
|
||||||
hoc,
|
|
||||||
commits,
|
|
||||||
hoc_pretty,
|
|
||||||
url,
|
|
||||||
head,
|
|
||||||
repo,
|
|
||||||
service_path,
|
|
||||||
} => {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
let repo_info = RepoInfo {
|
|
||||||
commit_url: &T::commit_url(&repo, &head),
|
|
||||||
commits,
|
|
||||||
domain: &OPT.domain,
|
|
||||||
head: &head,
|
|
||||||
hoc,
|
|
||||||
hoc_pretty: &hoc_pretty,
|
|
||||||
path: &service_path,
|
|
||||||
url: &url,
|
|
||||||
branch,
|
|
||||||
};
|
|
||||||
templates::overview(
|
|
||||||
&mut buf,
|
|
||||||
VERSION_INFO,
|
|
||||||
REPO_COUNT.load(Ordering::Relaxed),
|
|
||||||
repo_info,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
handle_hoc_request::<T, _>(state, data, branch, mapper).await
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/")]
|
|
||||||
async 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")]
|
|
||||||
async fn generate(params: web::Form<GeneratorForm<'_>>) -> Result<HttpResponse> {
|
|
||||||
let repo = format!("{}/{}", params.user, params.repo);
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
templates::generate(
|
|
||||||
&mut buf,
|
|
||||||
VERSION_INFO,
|
|
||||||
REPO_COUNT.load(Ordering::Relaxed),
|
|
||||||
&OPT.domain,
|
|
||||||
params.service.url(),
|
|
||||||
params.service.service(),
|
|
||||||
&repo,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
|
|
||||||
}
|
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn async_p404() -> Result<HttpResponse> {
|
|
||||||
p404()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn css() -> HttpResponse {
|
|
||||||
HttpResponse::Ok().content_type("text/css").body(CSS)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn favicon32() -> HttpResponse {
|
|
||||||
HttpResponse::Ok().content_type("image/png").body(FAVICON)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn start_server() -> std::io::Result<()> {
|
|
||||||
let interface = format!("{}:{}", OPT.host, OPT.port);
|
|
||||||
let state = Arc::new(State {
|
|
||||||
repos: OPT.outdir.display().to_string(),
|
|
||||||
cache: OPT.cachedir.display().to_string(),
|
|
||||||
});
|
|
||||||
HttpServer::new(move || {
|
|
||||||
App::new()
|
|
||||||
.data(state.clone())
|
|
||||||
.wrap(middleware::Logger::default())
|
|
||||||
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
|
|
||||||
.service(index)
|
|
||||||
.service(web::resource("/tacit-css.min.css").route(web::get().to(css)))
|
|
||||||
// TODO
|
|
||||||
.service(web::resource("/favicon.ico").route(web::get().to(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("/github/{user}/{repo}/delete")
|
|
||||||
.route(web::post().to(delete_repo_and_cache::<GitHub>)),
|
|
||||||
)
|
|
||||||
.service(
|
|
||||||
web::resource("/gitlab/{user}/{repo}/delete")
|
|
||||||
.route(web::post().to(delete_repo_and_cache::<Gitlab>)),
|
|
||||||
)
|
|
||||||
.service(
|
|
||||||
web::resource("/bitbucket/{user}/{repo}/delete")
|
|
||||||
.route(web::post().to(delete_repo_and_cache::<Bitbucket>)),
|
|
||||||
)
|
|
||||||
.service(web::resource("/github/{user}/{repo}/json").to(json_hoc::<GitHub>))
|
|
||||||
.service(web::resource("/gitlab/{user}/{repo}/json").to(json_hoc::<Gitlab>))
|
|
||||||
.service(web::resource("/bitbucket/{user}/{repo}/json").to(json_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(async_p404)))
|
|
||||||
})
|
|
||||||
.workers(OPT.workers)
|
|
||||||
.bind(interface)?
|
|
||||||
.run()
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
config::init().await.unwrap();
|
init();
|
||||||
start_server().await
|
|
||||||
|
// TODO: error handling
|
||||||
|
let settings = Settings::load().expect("Cannot load config");
|
||||||
|
|
||||||
|
let address = format!("{}:{}", settings.host, settings.port);
|
||||||
|
// TODO: error handling
|
||||||
|
let listener = TcpListener::bind(address)?;
|
||||||
|
hoc::run(listener, settings)
|
||||||
|
.await
|
||||||
|
.expect("Server error")
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,10 @@
|
|||||||
use crate::{config::Opt, count::count_repositories};
|
|
||||||
use std::sync::atomic::AtomicUsize;
|
|
||||||
use structopt::StructOpt;
|
|
||||||
|
|
||||||
pub struct VersionInfo<'a> {
|
pub struct VersionInfo<'a> {
|
||||||
pub commit: &'a str,
|
pub commit: &'a str,
|
||||||
pub version: &'a str,
|
pub version: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) const VERSION_INFO: VersionInfo = VersionInfo {
|
pub(crate) const VERSION_INFO: VersionInfo = VersionInfo {
|
||||||
commit: env!("VERGEN_SHA_SHORT"),
|
commit: env!("VERGEN_GIT_SHA_SHORT"),
|
||||||
version: env!("CARGO_PKG_VERSION"),
|
version: env!("CARGO_PKG_VERSION"),
|
||||||
};
|
};
|
||||||
pub(crate) const CSS: &str = include_str!("../static/tacit-css.min.css");
|
pub(crate) const CSS: &str = include_str!("../static/tacit-css.min.css");
|
||||||
@ -16,7 +12,4 @@ pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png");
|
|||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub(crate) static ref CLIENT: reqwest::Client = reqwest::Client::new();
|
pub(crate) static ref CLIENT: reqwest::Client = reqwest::Client::new();
|
||||||
pub(crate) static ref OPT: Opt = Opt::from_args();
|
|
||||||
pub(crate) static ref REPO_COUNT: AtomicUsize =
|
|
||||||
AtomicUsize::new(count_repositories(&OPT.outdir).unwrap());
|
|
||||||
}
|
}
|
||||||
|
21
src/telemetry.rs
Normal file
21
src/telemetry.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
use tracing::{subscriber::set_global_default, Subscriber};
|
||||||
|
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
|
||||||
|
use tracing_log::LogTracer;
|
||||||
|
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
|
||||||
|
|
||||||
|
pub fn get_subscriber(name: &str, env_filter: &str) -> impl Subscriber + Send + Sync {
|
||||||
|
let env_filter =
|
||||||
|
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter));
|
||||||
|
|
||||||
|
let formatting_layer = BunyanFormattingLayer::new(name.to_string(), std::io::stdout);
|
||||||
|
|
||||||
|
Registry::default()
|
||||||
|
.with(env_filter)
|
||||||
|
.with(JsonStorageLayer)
|
||||||
|
.with(formatting_layer)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
|
||||||
|
LogTracer::init().expect("Failed to set logger");
|
||||||
|
set_global_default(subscriber).expect("Failed to set tracing subscriber");
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
pub struct RepoInfo<'a> {
|
pub struct RepoInfo<'a> {
|
||||||
pub commit_url: &'a str,
|
pub commit_url: &'a str,
|
||||||
pub commits: u64,
|
pub commits: u64,
|
||||||
pub domain: &'a str,
|
pub base_url: &'a str,
|
||||||
pub head: &'a str,
|
pub head: &'a str,
|
||||||
pub hoc: u64,
|
pub hoc: u64,
|
||||||
pub hoc_pretty: &'a str,
|
pub hoc_pretty: &'a str,
|
||||||
|
66
src/tests.rs
66
src/tests.rs
@ -1,66 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
calculate_hoc, index, json_hoc,
|
|
||||||
service::{Bitbucket, GitHub, Gitlab, Service},
|
|
||||||
State,
|
|
||||||
};
|
|
||||||
|
|
||||||
use actix_web::{http, test, web, App};
|
|
||||||
use tempfile::tempdir;
|
|
||||||
|
|
||||||
macro_rules! test_app {
|
|
||||||
($path: expr) => {
|
|
||||||
test::init_service(App::new().service($path)).await
|
|
||||||
};
|
|
||||||
($state: expr, $path: expr) => {
|
|
||||||
test::init_service(App::new().data($state).service($path)).await
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! test_service {
|
|
||||||
($name: ident, $path: tt, $what: ident) => {
|
|
||||||
async fn $name<T: 'static + Service>(req_path: &str) {
|
|
||||||
let repo_dir = dbg!(tempdir().unwrap());
|
|
||||||
let cache_dir = dbg!(tempdir().unwrap());
|
|
||||||
let repos = format!("{}/", repo_dir.path().display());
|
|
||||||
let cache = format!("{}/", cache_dir.path().display());
|
|
||||||
let state = dbg!(State { repos, cache });
|
|
||||||
|
|
||||||
let mut app = test_app!(state, web::resource($path).to($what::<T>));
|
|
||||||
|
|
||||||
let req = dbg!(test::TestRequest::with_uri(req_path).to_request());
|
|
||||||
let resp = dbg!(test::call_service(&mut app, req).await);
|
|
||||||
|
|
||||||
assert_eq!(resp.status(), http::StatusCode::OK);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn test_index() {
|
|
||||||
let mut app = test::init_service(App::new().service(index)).await;
|
|
||||||
|
|
||||||
let req = dbg!(test::TestRequest::with_uri("/").to_request());
|
|
||||||
let resp = dbg!(test::call_service(&mut app, req).await);
|
|
||||||
|
|
||||||
assert_eq!(resp.status(), http::StatusCode::OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: fix this test
|
|
||||||
// #[actix_rt::test]
|
|
||||||
async fn test_json() {
|
|
||||||
test_service!(test_json_service, "/service/{user}/{repo}/json", json_hoc);
|
|
||||||
|
|
||||||
test_json_service::<Gitlab>("/service/vbrandl/hoc/json").await;
|
|
||||||
test_json_service::<GitHub>("/service/vbrandl/hoc/json").await;
|
|
||||||
test_json_service::<Bitbucket>("/service/vbrandl/hoc/json").await;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: fix this test
|
|
||||||
// #[actix_rt::test]
|
|
||||||
async fn test_badge() {
|
|
||||||
test_service!(test_badge_service, "/service/{user}/{repo}", calculate_hoc);
|
|
||||||
|
|
||||||
test_badge_service::<Gitlab>("/service/vbrandl/hoc").await;
|
|
||||||
test_badge_service::<GitHub>("/service/vbrandl/hoc").await;
|
|
||||||
test_badge_service::<Bitbucket>("/service/vbrandl/hoc").await;
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
@use super::base;
|
@use super::base;
|
||||||
@use crate::statics::VersionInfo;
|
@use crate::statics::VersionInfo;
|
||||||
|
|
||||||
@(version_info: VersionInfo, repo_count: usize, domain: &str, url: &str, service: &str, path: &str)
|
@(version_info: VersionInfo, repo_count: usize, base_url: &str, url: &str, service: &str, path: &str)
|
||||||
|
|
||||||
@:base("Hits-of-Code Badges", "Badge Generator", {
|
@:base("Hits-of-Code Badges", "Badge Generator", {
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ Here is the markdown for the badge for <a href="https://@url/@path">@url/@path</
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
[](https://@domain/view/@service/@path)
|
[](@base_url/@service/@path/view)
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@ -18,6 +18,6 @@ It will be rendered like this
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
<a href="https://@domain/view/@service/@path"><img src="https://@domain/@service/@path" alt="example badge" /></a>
|
<a href="@base_url/@service/@path/view"><img src="@base_url/@service/@path" alt="example badge" /></a>
|
||||||
</pre>
|
</pre>
|
||||||
}, version_info, repo_count)
|
}, version_info, repo_count)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
@use super::base;
|
@use super::base;
|
||||||
@use crate::statics::VersionInfo;
|
@use crate::statics::VersionInfo;
|
||||||
|
|
||||||
@(version_info: VersionInfo, repo_count: usize, domain: &str)
|
@(version_info: VersionInfo, repo_count: usize, base_url: &str)
|
||||||
|
|
||||||
@:base("Hits-of-Code Badges", "Hits-of-Code Badges", {
|
@:base("Hits-of-Code Badges", "Hits-of-Code Badges", {
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ used for GitHub, GitLab and Bitbucket repositories. Just put the following code
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
[](https://@domain/view/<service>/<user>/<repo>)
|
[](@base_url/<service>/<user>/<repo>/view)
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@ -33,7 +33,7 @@ following Markdown
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
[](https://@domain/view/github/vbrandl/hoc)
|
[](@base_url/github/vbrandl/hoc/view)
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@ -41,7 +41,7 @@ would render this badge:
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
<a href="https://@domain/view/github/vbrandl/hoc"><img src="https://@domain/github/vbrandl/hoc"
|
<a href="@base_url/github/vbrandl/hoc/view"><img src="@base_url/github/vbrandl/hoc"
|
||||||
alt="example badge" /></a>
|
alt="example badge" /></a>
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ in your repository or you want a badge for another branch of your repository, ju
|
|||||||
You can also request the HoC as JSON by appending <code>/json</code> to the request path. This will return a JSON object
|
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
|
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
|
<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
|
href="@base_url/github/vbrandl/hoc/json">@base_url/github/vbrandl/hoc/json</a> might return something along
|
||||||
the lines of
|
the lines of
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ To include the badge in your readme, use the following markdown:
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
[](https://@repo_info.domain/view/@repo_info.path?branch=@repo_info.branch)
|
[](@repo_info.base_url/@repo_info.path/view?branch=@repo_info.branch)
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
18
tests/badge.rs
Normal file
18
tests/badge.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
mod util;
|
||||||
|
|
||||||
|
use actix_web::client;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn badge_succeeds() {
|
||||||
|
let test_app = util::spawn_app().await;
|
||||||
|
|
||||||
|
let client = client::Client::default();
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.get(&format!("{}/github/vbrandl/hoc", test_app.address))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request");
|
||||||
|
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
}
|
18
tests/health_check.rs
Normal file
18
tests/health_check.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
mod util;
|
||||||
|
|
||||||
|
use actix_web::client;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn health_check_works() {
|
||||||
|
let test_app = util::spawn_app().await;
|
||||||
|
|
||||||
|
let client = client::Client::default();
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.get(&format!("{}/health_check", test_app.address))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request");
|
||||||
|
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
}
|
18
tests/index.rs
Normal file
18
tests/index.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
mod util;
|
||||||
|
|
||||||
|
use actix_web::client;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn index_returns_success() {
|
||||||
|
let test_app = util::spawn_app().await;
|
||||||
|
|
||||||
|
let client = client::Client::default();
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.get(&format!("{}/", test_app.address))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request");
|
||||||
|
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
}
|
18
tests/json.rs
Normal file
18
tests/json.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
mod util;
|
||||||
|
|
||||||
|
use actix_web::client;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn json_returns_success() {
|
||||||
|
let test_app = util::spawn_app().await;
|
||||||
|
|
||||||
|
let client = client::Client::default();
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.get(&format!("{}/github/vbrandl/hoc/json", test_app.address))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request");
|
||||||
|
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
}
|
50
tests/util/mod.rs
Normal file
50
tests/util/mod.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
use hoc::{config::Settings, telemetry};
|
||||||
|
|
||||||
|
use std::net::TcpListener;
|
||||||
|
|
||||||
|
use tempfile::{tempdir, TempDir};
|
||||||
|
|
||||||
|
lazy_static::lazy_static! {
|
||||||
|
static ref TRACING: () = {
|
||||||
|
let filter = if std::env::var("TEST_LOG").is_ok() { "debug" } else { "" };
|
||||||
|
let subscriber = telemetry::get_subscriber("test", filter);
|
||||||
|
telemetry::init_subscriber(subscriber);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TestApp {
|
||||||
|
pub address: String,
|
||||||
|
repo_dir: TempDir,
|
||||||
|
cache_dir: TempDir,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn spawn_app() -> TestApp {
|
||||||
|
lazy_static::initialize(&TRACING);
|
||||||
|
|
||||||
|
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
||||||
|
|
||||||
|
let port = listener.local_addr().unwrap().port();
|
||||||
|
let address = format!("http://127.0.0.1:{}", port);
|
||||||
|
|
||||||
|
let repo_dir = tempdir().expect("Cannot create repo_dir");
|
||||||
|
let cache_dir = tempdir().expect("Cannot create cache_dir");
|
||||||
|
|
||||||
|
let mut settings = Settings::load().expect("Failed to read configuration.");
|
||||||
|
settings.repodir = repo_dir.path().to_path_buf();
|
||||||
|
settings.cachedir = cache_dir.path().to_path_buf();
|
||||||
|
// configuration.database.database_name = Uuid::new_v4().to_string();
|
||||||
|
|
||||||
|
// let connection_pool = configure_database(&configuration.database).await;
|
||||||
|
|
||||||
|
let server = hoc::run(listener, settings)
|
||||||
|
.await
|
||||||
|
.expect("Failed to bind address");
|
||||||
|
|
||||||
|
let _ = tokio::spawn(server);
|
||||||
|
|
||||||
|
TestApp {
|
||||||
|
address,
|
||||||
|
repo_dir,
|
||||||
|
cache_dir,
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user