Skip to content

Commit

Permalink
chore: upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s committed Nov 21, 2023
1 parent faaa0c1 commit 2d822fa
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 47 deletions.
47 changes: 24 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nanocl-get-started"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -11,6 +11,15 @@ test = true
bench = false
path = "src/main.rs"

[profile.dev]
opt-level = 0

[profile.release]
strip = true
# opt-level = "s"
# lto = true
# codegen-units = 1

[dependencies]
ntex = { version = "0.7.9", features = ["tokio"] }
ntex = { version = "0.7.11", features = ["tokio"] }
serde = { version = "1.0.192", features = ["derive"] }
12 changes: 7 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# stage 1 - Setup cargo-chef
FROM --platform=$BUILDPLATFORM rust:1.73.0-alpine3.17 as planner
FROM --platform=$BUILDPLATFORM rust:1.74.0-alpine3.17 as planner

WORKDIR /app
RUN apk add gcc g++ make
Expand All @@ -9,7 +9,7 @@ COPY ./Cargo.lock ./Cargo.lock
RUN cargo chef prepare --recipe-path recipe.json

# state 2 - Cook our dependencies
FROM --platform=$BUILDPLATFORM rust:1.73.0-alpine3.17 as cacher
FROM --platform=$BUILDPLATFORM rust:1.74.0-alpine3.17 as cacher

WORKDIR /app
COPY --from=planner /usr/local/cargo/bin/cargo-chef /usr/local/cargo/bin/cargo-chef
Expand All @@ -20,14 +20,14 @@ RUN export ARCH=$(uname -m) \
&& cargo chef cook --release --target=$ARCH-unknown-linux-musl --recipe-path recipe.json

# stage 3 - Build our project
FROM rust:1.73.0-alpine3.17 as builder
FROM --platform=$BUILDPLATFORM rust:1.74.0-alpine3.17 as builder

## Build our metrs daemon binary
WORKDIR /app
COPY --from=cacher /usr/local/cargo /usr/local/cargo
COPY --from=cacher /app .
COPY ./src ./src
RUN apk add musl-dev git upx
RUN apk add musl-dev git upx
ENV RUSTFLAGS="-C target-feature=+crt-static"
RUN export ARCH=$(uname -m) \
&& cargo build --release --target=$ARCH-unknown-linux-musl
Expand All @@ -38,13 +38,15 @@ RUN export ARCH=$(uname -m) \
&& cp /app/target/$ARCH-unknown-linux-musl/release/nanocl-get-started /bin/nanocl-get-started

# stage 4 - Create runtime image
FROM scratch
FROM --platform=$BUILDPLATFORM scratch

## Copy the binary
COPY --from=builder /bin/nanocl-get-started /bin/nanocl-get-started

LABEL org.opencontainers.image.source https://github.com/nxthat/nanocl-get-started
LABEL org.opencontainers.image.description Nanocl get started image

EXPOSE 9000

## Set entrypoint
ENTRYPOINT ["/bin/nanocl-get-started"]
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,51 @@

## Official Nanocl get started

Image used for the [get started](https://docs.next-hat.com/guides/nanocl/get-started/orientation-and-setup) tutorial
Image used for [Nanocl][nanocl] get started [tutorial][get-started].</br>
That just return headers and environnement variables for all incomming requests.

Example:

Create a new [Statefile][statefile] `get_started.yml` :

```yaml
Kind: Deployment
ApiVersion: v0.11

Namespace: global

# See all options:
# https://docs.next-hat.com/references/nanocl/resource
Resources:
- Name: get-started.com
Kind: ProxyRule
Version: v0.8
Data:
Rules:
- Domain: get-started.com
Network: Internal
Locations:
- Path: /
Target:
Key: get-started.global.c
Port: 9000

# See all options:
# https://docs.next-hat.com/references/nanocl/cargo
Cargoes:
- Name: get-started
Container:
Image: ghcr.io/nxthat/nanocl-get-started:latest
Env:
- APP=GET_STARTED
```
Execute the [Statefile][statefile] :
```sh
nanocl state apply -s get_started.yml
```

[nanocl]: https://next-hat.com/nanocl
[get-started]: https://docs.next-hat.com/guides/nanocl/get-started/orientation-and-setup
[statefile]: https://docs.next-hat.com/references/nanocl/statefile
29 changes: 13 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
use std::collections::HashMap;

use ntex::web;
use ntex::{web, http};

#[derive(serde::Serialize)]
struct DefaultResponse {
headers: HashMap<String, String>,
headers: http::HeaderMap,
envs: HashMap<String, String>,
}

#[web::get("{all}*")]
async fn endpoint(req: web::HttpRequest) -> web::HttpResponse {
let envs = std::env::vars();
let headers = req
.headers()
.iter()
.fold(HashMap::new(), |mut acc, (k, v)| {
acc.insert(k.to_string(), v.to_str().unwrap_or("").to_string());
acc
});
let envs = envs.fold(HashMap::new(), |mut acc, (k, v)| {
let headers = req.headers().clone();
let envs = std::env::vars().fold(HashMap::new(), |mut acc, (k, v)| {
acc.insert(k, v);
acc
});
let resp = DefaultResponse { headers, envs };
web::HttpResponse::Ok().json(&resp)
}

#[ntex::main]
async fn main() -> std::io::Result<()> {
let port = std::env::var("PORT").unwrap_or_else(|_| "8080".to_owned());
let server = web::HttpServer::new(|| web::App::new().service(endpoint));
server.bind(&format!("0.0.0.0:{port}"))?.run().await?;
fn main() -> std::io::Result<()> {
ntex::rt::System::new("main").block_on(async {
let port = std::env::var("PORT").unwrap_or_else(|_| "9000".to_owned());
let server = web::HttpServer::new(|| web::App::new().service(endpoint));
let addr = &format!("0.0.0.0:{port}");
println!("Listening on: {addr}");
server.bind(addr)?.run().await?;
Ok::<_, std::io::Error>(())
})?;
Ok(())
}

0 comments on commit 2d822fa

Please sign in to comment.