-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |