Skip to content

Commit

Permalink
feat: Setup podman conteneur
Browse files Browse the repository at this point in the history
  • Loading branch information
batleforc committed Aug 15, 2024
1 parent a8a90df commit 2f4d41f
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .containerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.nx
dist
node_modules
tmp
/.github
/.vscode
/target
.env
10 changes: 6 additions & 4 deletions apps/back/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::{env, path::PathBuf};

use dotenvy::dotenv;
use serde::Deserialize;
Expand All @@ -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)]
Expand Down Expand Up @@ -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")
}
Expand All @@ -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),
Expand Down
15 changes: 15 additions & 0 deletions build/Readme.md
Original file line number Diff line number Diff line change
@@ -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 .
```
54 changes: 54 additions & 0 deletions build/back/Containerfile
Original file line number Diff line number Diff line change
@@ -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"]
23 changes: 23 additions & 0 deletions build/front/Containerfile
Original file line number Diff line number Diff line change
@@ -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;" ]
17 changes: 17 additions & 0 deletions build/front/nginx.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions build/reverse-proxy/Containerfile
Original file line number Diff line number Diff line change
@@ -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;" ]
13 changes: 13 additions & 0 deletions build/reverse-proxy/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
server {

listen 3000;

location /api {
proxy_pass http://backend:5437;
}

location / {
proxy_pass http://frontend:8080;
}

}
36 changes: 36 additions & 0 deletions compose.full.yaml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion libs/tool_tracing/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,14 +54,19 @@ pub fn init_tracing(tracing_config: Vec<Tracing>, 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()
.tracing()
.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()),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit 2f4d41f

Please sign in to comment.