Skip to content

Commit

Permalink
adding distroless-web
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgift committed Mar 22, 2023
1 parent a471bbe commit 1361633
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
10 changes: 10 additions & 0 deletions distroless-web/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "distroless-web"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4"
rand = "0.8"
8 changes: 8 additions & 0 deletions distroless-web/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM rust:1.67.1 as build-env
WORKDIR /app
COPY . /app
RUN cargo build --release

FROM gcr.io/distroless/cc
COPY --from=build-env /app/target/release/distroless-web /
CMD ["./distroless-web"]
23 changes: 23 additions & 0 deletions distroless-web/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
install:
cargo clean &&\
cargo build -j 1

build:
docker build -t fruit .

rundocker:
docker run -it --rm -p 8080:8080 fruit

format:
cargo fmt --quiet

lint:
cargo clippy --quiet

test:
cargo test --quiet

run:
cargo run

all: format lint test run
24 changes: 24 additions & 0 deletions distroless-web/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*A library that returns back random fruit */

use rand::Rng;

//create an const array of 10 fruits
pub const FRUITS: [&str; 10] = [
"Apple",
"Banana",
"Orange",
"Pineapple",
"Strawberry",
"Watermelon",
"Grapes",
"Mango",
"Papaya",
"Kiwi",
];

//create a function that returns a random fruit
pub fn random_fruit() -> &'static str {
let mut rng = rand::thread_rng();
let random_index = rng.gen_range(0..FRUITS.len());
FRUITS[random_index]
}
47 changes: 47 additions & 0 deletions distroless-web/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
//import the random fruit function from the lib.rs file
use distroless_web::random_fruit;

//create a function that returns a hello world
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello World Random Fruit!")
}

//create a function that returns a random fruit
#[get("/fruit")]
async fn fruit() -> impl Responder {
//print the random fruit
println!("Random Fruit: {}", random_fruit());
HttpResponse::Ok().body(random_fruit())
}

//create a function that returns a 200 status code
#[get("/health")]
async fn health() -> impl Responder {
HttpResponse::Ok()
}

//create a function that returns the version of the service
#[get("/version")]
async fn version() -> impl Responder {
//print the version of the service
println!("Version: {}", env!("CARGO_PKG_VERSION"));
HttpResponse::Ok().body(env!("CARGO_PKG_VERSION"))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
//add a print message to the console that the service is running
println!("Running the service");
HttpServer::new(|| {
App::new()
.service(hello)
.service(fruit)
.service(health)
.service(version)
})
.bind("0.0.0.0:8080")?
.run()
.await
}

0 comments on commit 1361633

Please sign in to comment.