From 2f4d41f7f5f9a2f0a39c635cd0ce65f6fb5c976f Mon Sep 17 00:00:00 2001 From: Max batleforc Date: Thu, 15 Aug 2024 14:43:24 +0200 Subject: [PATCH] feat: Setup podman conteneur --- .containerignore | 8 +++++ apps/back/src/config.rs | 10 +++--- build/Readme.md | 15 +++++++++ build/back/Containerfile | 54 +++++++++++++++++++++++++++++++ build/front/Containerfile | 23 +++++++++++++ build/front/nginx.conf | 17 ++++++++++ build/reverse-proxy/Containerfile | 7 ++++ build/reverse-proxy/nginx.conf | 13 ++++++++ compose.full.yaml | 36 +++++++++++++++++++++ libs/tool_tracing/src/init.rs | 8 ++++- package.json | 2 +- 11 files changed, 187 insertions(+), 6 deletions(-) create mode 100644 .containerignore create mode 100644 build/Readme.md create mode 100644 build/back/Containerfile create mode 100644 build/front/Containerfile create mode 100644 build/front/nginx.conf create mode 100644 build/reverse-proxy/Containerfile create mode 100644 build/reverse-proxy/nginx.conf create mode 100644 compose.full.yaml diff --git a/.containerignore b/.containerignore new file mode 100644 index 0000000..038cd44 --- /dev/null +++ b/.containerignore @@ -0,0 +1,8 @@ +.nx +dist +node_modules +tmp +/.github +/.vscode +/target +.env \ No newline at end of file diff --git a/apps/back/src/config.rs b/apps/back/src/config.rs index da4c1ff..d3ce6ec 100644 --- a/apps/back/src/config.rs +++ b/apps/back/src/config.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{env, path::PathBuf}; use dotenvy::dotenv; use serde::Deserialize; @@ -7,6 +7,7 @@ use tracing::info; const API_PORT: &str = "API_PORT"; const CONTENT_PATH: &str = "CONTENT_PATH"; +const CONFIG_PATH: &str = "CONFIG_PATH"; const ENV: &str = "ENV"; #[derive(Deserialize, Debug, Clone)] @@ -40,7 +41,8 @@ fn override_config_with_env(config: Config) -> Config { } fn parse_config_from_file(path_buf: PathBuf) -> Config { - let file = std::fs::File::open(path_buf).expect("file should open read only"); + let file = std::fs::File::open(path_buf.clone()) + .unwrap_or_else(|_| panic!("file should open read only {}", path_buf.display())); let reader = std::io::BufReader::new(file); serde_yaml::from_reader(reader).expect("file should be proper YAML") } @@ -51,8 +53,8 @@ pub fn parse_config(path_buf: PathBuf) -> Config { } pub fn parse_local_config() -> Config { - let mut path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - path_buf.push("../../folio_content/content/config.yaml"); + let mut path_buf = env::current_dir().unwrap(); + path_buf.push(env::var(CONFIG_PATH).unwrap_or("folio_content/content/config.yaml".to_string())); match dotenv() { Ok(_) => info!("Loaded .env file"), Err(err) => println!("No .env file found: {:?}", err), diff --git a/build/Readme.md b/build/Readme.md new file mode 100644 index 0000000..42f84b0 --- /dev/null +++ b/build/Readme.md @@ -0,0 +1,15 @@ +# Build docker system + +Each app will be build in a separate container. This will allow us to scale the app independently and update only the front or back end without affecting the other. + +## Build the front end + +```bash +podman build -t front:latest-dev -f build/front/Containerfile . +``` + +## Build the back end + +```bash +podman build -t back:latest-dev -f build/back/Containerfile . +``` diff --git a/build/back/Containerfile b/build/back/Containerfile new file mode 100644 index 0000000..f35c8a6 --- /dev/null +++ b/build/back/Containerfile @@ -0,0 +1,54 @@ +FROM registry.hub.docker.com/library/rust:1.80 AS build + +WORKDIR /app + +# Prepare the build environment and cache dependencies +RUN mkdir -p libs/markdown_header/src && \ + mkdir -p libs/markdown_struct/src && \ + mkdir -p libs/tool_tracing/src && \ + mkdir -p apps/back/src && \ + mkdir -p apps/back/src/bin + +COPY Cargo.toml . +COPY ./libs/markdown_header/Cargo.toml ./libs/markdown_header/ +COPY ./libs/markdown_struct/Cargo.toml ./libs/markdown_struct/ +COPY ./libs/tool_tracing/Cargo.toml ./libs/tool_tracing/ +COPY ./apps/back/Cargo.toml ./apps/back/ +COPY .cargo . + +RUN echo "pub fn main() {}" > libs/markdown_header/src/lib.rs && \ + echo "pub fn main() {}" > libs/markdown_struct/src/lib.rs && \ + echo "pub fn main() {}" > libs/tool_tracing/src/lib.rs && \ + echo "fn main() {}" > apps/back/src/main.rs && \ + echo "fn main() {}" > apps/back/src/bin/sandbox.rs && \ + echo "fn main() {}" > apps/back/src/bin/gen_swagger.rs + +RUN cargo build --release + +# Build the application +COPY . . +RUN touch apps/back/src/main.rs +RUN cargo build --release + +RUN strip dist/target/release/server && \ + strip dist/target/release/swagger + +FROM gcr.io/distroless/cc-debian12 +#FROM registry.hub.docker.com/library/debian:bullseye-slim + +WORKDIR /app + +COPY --from=build /app/dist/target/release/server . +COPY --from=build /app/dist/target/release/swagger . + +COPY ./folio_content/ . + +EXPOSE 5437 + +ENV API_PORT 5437 +ENV ENV production +ENV CONFIG_PATH content/config.yaml +ENV CONTENT_PATH ./content + +CMD ["./server"] +#CMD ["tail", "-f", "/dev/null"] \ No newline at end of file diff --git a/build/front/Containerfile b/build/front/Containerfile new file mode 100644 index 0000000..d426c38 --- /dev/null +++ b/build/front/Containerfile @@ -0,0 +1,23 @@ +FROM registry.hub.docker.com/library/node:21 AS build +WORKDIR /app +ENV ENV production +COPY package.json ./ +COPY yarn.lock ./ +RUN yarn +COPY . ./ +ENV ENV production +RUN npx nx build front + + + + +FROM registry.hub.docker.com/library/nginx:1.27-alpine AS deploy +WORKDIR /usr/share/nginx/html + +RUN rm -rf ./* +COPY ./build/front/nginx.conf /etc/nginx/conf.d/default.conf +COPY --from=build /app/dist/apps/front . + +EXPOSE 8080 + +ENTRYPOINT [ "nginx", "-g", "daemon off;" ] \ No newline at end of file diff --git a/build/front/nginx.conf b/build/front/nginx.conf new file mode 100644 index 0000000..416020f --- /dev/null +++ b/build/front/nginx.conf @@ -0,0 +1,17 @@ +server { + + listen 8080; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + error_page 500 502 503 504 /50x.html; + error_page 404 /index.html; + + location = /50x.html { + root /usr/share/nginx/html; + } +} \ No newline at end of file diff --git a/build/reverse-proxy/Containerfile b/build/reverse-proxy/Containerfile new file mode 100644 index 0000000..46e580a --- /dev/null +++ b/build/reverse-proxy/Containerfile @@ -0,0 +1,7 @@ +FROM registry.hub.docker.com/library/nginx:1.27-alpine + +COPY ./build/reverse-proxy/nginx.conf /etc/nginx/conf.d/default.conf + +EXPOSE 3000 + +ENTRYPOINT [ "nginx", "-g", "daemon off;" ] \ No newline at end of file diff --git a/build/reverse-proxy/nginx.conf b/build/reverse-proxy/nginx.conf new file mode 100644 index 0000000..e750128 --- /dev/null +++ b/build/reverse-proxy/nginx.conf @@ -0,0 +1,13 @@ +server { + + listen 3000; + + location /api { + proxy_pass http://backend:5437; + } + + location / { + proxy_pass http://frontend:8080; + } + +} \ No newline at end of file diff --git a/compose.full.yaml b/compose.full.yaml new file mode 100644 index 0000000..e52eeb2 --- /dev/null +++ b/compose.full.yaml @@ -0,0 +1,36 @@ +services: + jaeger: + image: jaegertracing/all-in-one:${JAEGER_VERSION:-latest} + ports: + - '16686:16686' + - '4318:4318' + - '14268:14268' + - '4317:4317' + environment: + - LOG_LEVEL=debug + + backend: + build: + context: . + dockerfile: build/back/Containerfile + ports: + - '5437:5437' + environment: + OTEL_OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317 + + frontend: + build: + context: . + dockerfile: build/front/Containerfile + ports: + - '8080:8080' + + reverse-proxy: + build: + context: . + dockerfile: build/reverse-proxy/Containerfile + ports: + - '3000:3000' + depends_on: + - frontend + - backend diff --git a/libs/tool_tracing/src/init.rs b/libs/tool_tracing/src/init.rs index 762908b..e61fec8 100644 --- a/libs/tool_tracing/src/init.rs +++ b/libs/tool_tracing/src/init.rs @@ -4,6 +4,7 @@ use opentelemetry::KeyValue; use opentelemetry_otlp::WithExportConfig; use opentelemetry_sdk::trace::Config; use opentelemetry_sdk::{runtime, Resource}; +use std::env; use std::{fs::File, sync::Arc, vec}; use time::format_description; use tracing::level_filters::LevelFilter; @@ -53,6 +54,11 @@ pub fn init_tracing(tracing_config: Vec, name: String) { Some(endpoint) => endpoint.to_string(), None => "http://localhost:4317".to_string(), }; + let endpoint_from_env = env::var(format!( + "{}_OTEL_EXPORTER_OTLP_ENDPOINT", + name.to_uppercase() + )) + .unwrap_or(endpoint); let pod_name = std::env::var("POD_NAME").unwrap_or_else(|_| "not_a_pod".to_string()); let telemetry = opentelemetry_otlp::new_pipeline() @@ -60,7 +66,7 @@ pub fn init_tracing(tracing_config: Vec, name: String) { .with_exporter( opentelemetry_otlp::new_exporter() .tonic() - .with_endpoint(endpoint), + .with_endpoint(endpoint_from_env), ) .with_trace_config(Config::default().with_resource(Resource::new(vec![ KeyValue::new("service.name", name.clone()), diff --git a/package.json b/package.json index 83e8aa5..89c4c8d 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "rust:test": "cargo llvm-cov --open --ignore-filename-regex 'init'", - "rust:start": "podman compose up -d && cargo run", + "rust:start": "podman compose up -d && nx run back:run", "rust:generate": "cargo run --bin swagger -- ./swagger.json", "vue:gen_api": "npx @hey-api/openapi-ts -i ./swagger.json -o libs/api-client/src/api -c @hey-api/client-axios" },