initial commit

This commit is contained in:
Dmitry Belyaev 2025-03-08 14:14:13 +03:00
commit eaea3f906c
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
9 changed files with 1808 additions and 0 deletions

1
.dockerignore Normal file

@ -0,0 +1 @@
target/

@ -0,0 +1,70 @@
name: Docker
on:
push:
branches: [ "master" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
env:
REGISTRY: gitea.b4tman.ru
IMAGE_NAME: ${{ gitea.repository }}
jobs:
build:
runs-on: cth-ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install cosign
uses: sigstore/cosign-installer@v3.5.0
with:
cosign-release: 'v2.2.4'
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.0.0
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v3.0.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ gitea.actor }}
password: ${{ secrets.PKGS_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5.0.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
flavor: |
latest=${{ github.ref == 'refs/heads/master' }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
labels: |
maintainer=${{ gitea.actor }}
org.opencontainers.image.description="${{ steps.get_description.outputs.description }}"
org.opencontainers.image.authors=${{ gitea.actor }}
org.opencontainers.image.licenses=MIT
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5.0.0
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Sign the published Docker image
env:
TAGS: ${{ steps.meta.outputs.tags }}
DIGEST: ${{ steps.build-and-push.outputs.digest }}
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}

1
.gitignore vendored Normal file

@ -0,0 +1 @@
/target

1628
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

8
Cargo.toml Normal file

@ -0,0 +1,8 @@
[package]
name = "cr-web-test"
version = "0.1.0"
edition = "2024"
[dependencies]
rocket = "0.5.1"

21
Dockerfile Normal file

@ -0,0 +1,21 @@
FROM rust:1-alpine AS chef
RUN apk add --no-cache musl-dev
RUN cargo install cargo-chef
WORKDIR /app
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
# Build dependencies - this is the caching Docker layer!
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release
FROM alpine AS runtime
WORKDIR /app
COPY --from=builder /app/target/release/cr-web-test /usr/local/bin
ENTRYPOINT ["/usr/local/bin/cr-web-test"]

21
LICENSE Normal file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Dmitry Belyaev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

3
README.md Normal file

@ -0,0 +1,3 @@
# cr-web-test
приложение для запуска внутри контейнера, для тестирования в Kubernetes

55
src/main.rs Normal file

@ -0,0 +1,55 @@
use rocket::fs::NamedFile;
use rocket::response::status;
use rocket::{self, State, get, launch, routes};
use std::env;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
struct HitCount {
count: AtomicUsize,
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[get("/hello/<name>")]
fn hello(name: String) -> String {
format!("Hello, {}!", name)
}
#[get("/env/<key>")]
fn get_env(key: String) -> Result<String, status::NotFound<String>> {
env::var(key).map_err(|e| status::NotFound(e.to_string()))
}
#[get("/files/<file>")]
async fn get_file(file: PathBuf) -> Result<NamedFile, status::NotFound<String>> {
if file.file_name() != Some(file.as_os_str()) {
return Err(status::NotFound(
"File name cannot contain slashes".to_string(),
));
}
let prefix = env::var("FILES_PREFIX").unwrap_or_else(|_| "/files".to_string());
let path = Path::new(&prefix).join(file);
NamedFile::open(&path)
.await
.map_err(|e| status::NotFound(e.to_string()))
}
#[get("/count")]
fn count(hit_count: &State<HitCount>) -> String {
let current_count = hit_count.count.load(Ordering::Relaxed);
hit_count.count.fetch_add(1, Ordering::Relaxed);
format!("Number of visits: {}", current_count)
}
#[launch]
fn rocket() -> _ {
rocket::build()
.manage(HitCount {
count: AtomicUsize::new(0),
})
.mount("/", routes![index, hello, get_env, get_file, count])
}