From a4b2276031b58f5840f4ad9685b44855014d7692 Mon Sep 17 00:00:00 2001 From: max-lt Date: Sat, 23 Mar 2024 22:49:21 +0100 Subject: [PATCH] Add dockerfile --- .cargo/config.toml | 2 ++ .dockerignore | 6 +++++ ...c41296626244dbefb2f411638e44ae46ac2e0.json | 22 ++++++++++++++++ .vscode/settings.json | 2 +- Cargo.lock | 19 ++++++++++++-- Cargo.toml | 8 +++--- Dockerfile | 25 +++++++++++++++++++ README.md | 12 +++++++++ bin/main.rs | 5 ++-- src/lib.rs | 1 + src/log.rs | 2 +- src/nats.rs | 21 ++++++++++++++++ src/scheduled.rs | 2 +- 13 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 .dockerignore create mode 100644 .sqlx/query-83b9ee13ebbd0e75fe09075eef2c41296626244dbefb2f411638e44ae46ac2e0.json create mode 100644 Dockerfile create mode 100644 src/nats.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..d2eb4c7 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[env] +RUNTIME_SNAPSHOT_PATH = { value = "/tmp/openworkers-runtime-snapshot.bin", relative = false } diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..acbd2f1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.vscode +.git + +target + +.env diff --git a/.sqlx/query-83b9ee13ebbd0e75fe09075eef2c41296626244dbefb2f411638e44ae46ac2e0.json b/.sqlx/query-83b9ee13ebbd0e75fe09075eef2c41296626244dbefb2f411638e44ae46ac2e0.json new file mode 100644 index 0000000..25f4bd1 --- /dev/null +++ b/.sqlx/query-83b9ee13ebbd0e75fe09075eef2c41296626244dbefb2f411638e44ae46ac2e0.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT worker_id::text FROM domains WHERE name = $1 LIMIT 1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "worker_id", + "type_info": "Text" + } + ], + "parameters": { + "Left": [ + "Text" + ] + }, + "nullable": [ + null + ] + }, + "hash": "83b9ee13ebbd0e75fe09075eef2c41296626244dbefb2f411638e44ae46ac2e0" +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 0ff4bb3..28f2b36 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "cSpell.words": ["actix", "nats", "oneshot", "sqlx"] + "cSpell.words": ["actix", "dotenv", "nats", "oneshot", "sqlx"] } diff --git a/Cargo.lock b/Cargo.lock index 92c91f3..39ffdfc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -463,6 +463,12 @@ version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +[[package]] +name = "base64" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51" + [[package]] name = "base64-simd" version = "0.7.0" @@ -1195,6 +1201,12 @@ dependencies = [ "syn 2.0.53", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dotenvy" version = "0.15.7" @@ -2202,10 +2214,12 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openworkers-runner" -version = "0.1.4" +version = "0.1.5" dependencies = [ "actix-web", + "base64 0.22.0", "bytes", + "dotenv", "env_logger", "http", "log", @@ -2219,7 +2233,8 @@ dependencies = [ [[package]] name = "openworkers-runtime" -version = "0.1.4" +version = "0.1.5" +source = "git+https://github.com/openworkers/openworkers-runtime?tag=v0.1.5#50fdebef3c253ec8f8c23198f04c2ca35de4f19c" dependencies = [ "bytes", "deno_console", diff --git a/Cargo.toml b/Cargo.toml index 0ecf088..0d4f04e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "openworkers-runner" -version = "0.1.4" +version = "0.1.5" edition = "2021" default-run = "openworkers-runner" @@ -14,11 +14,13 @@ tokio = "1.36.0" env_logger = "0.11.2" http_v02 = { package = "http", version = "0.2.9" } sqlx = { version = "0.7", features = [ "runtime-tokio", "postgres", "uuid", "bigdecimal", "rust_decimal" ] } -# openworkers-runtime ={ git = "https://github.com/openworkers/openworkers-runtime", branch = "master" } -openworkers-runtime = { path = "../openworkers-runtime" } +openworkers-runtime ={ git = "https://github.com/openworkers/openworkers-runtime", tag = "v0.1.5"} +# openworkers-runtime = { path = "../openworkers-runtime" } nats = "0.24.1" serde_json = "1.0.114" serde = { version = "1.0.197", features = ["derive"] } +dotenv = "0.15.0" +base64 = "0.22.0" # https://doc.rust-lang.org/cargo/reference/profiles.html # https://github.com/johnthagen/min-sized-rust?tab=readme-ov-file#minimizing-rust-binary-size diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f178f6b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM rust:1.77 as builder + +RUN mkdir -p /build/openworkers-runner + +ENV RUST_BACKTRACE=1 +ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin + +WORKDIR /build/openworkers-runner + +COPY . /build/openworkers-runner + +RUN touch $RUNTIME_SNAPSHOT_PATH + +RUN --mount=type=cache,target=~/.cargo \ + --mount=type=cache,target=/build/openworkers-runner/target \ + cargo run --release --bin snapshot && \ + cargo build --release && cp /build/openworkers-runner/target/release/openworkers-runner /build/output + +FROM debian:bookworm-slim + +COPY --from=builder /build/output /usr/local/bin/openworkers-runner + +CMD ["/usr/local/bin/openworkers-runner"] + +EXPOSE 8080 diff --git a/README.md b/README.md index e02dac6..60a9ef8 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,15 @@ export RUST_LOG=openworkers_runtime=debug,openworkers_runner=debug # Optional cargo run ``` + + +### Install sqlx-cli (optional - only for development) + +```bash +cargo install sqlx-cli --no-default-features --features rustls,postgres +``` + +#### Prepare the database +```bash +cargo sqlx prepare +``` diff --git a/bin/main.rs b/bin/main.rs index 5c3d4ce..42a4b10 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -179,6 +179,8 @@ async fn handle_request(data: Data, req: HttpRequest) -> HttpResponse #[actix_web::main] async fn main() -> std::io::Result<()> { + dotenv::dotenv().ok(); + if !std::env::var("RUST_LOG").is_ok() { std::env::set_var("RUST_LOG", "info"); } @@ -187,8 +189,7 @@ async fn main() -> std::io::Result<()> { debug!("start main"); - let db_url = std::env::var("DATABASE_URL") - .unwrap_or("postgres://admin:pass@127.0.0.1/swap_dev".to_string()); + let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set"); let pool = PgPoolOptions::new() .max_connections(4) .connect(&db_url) diff --git a/src/lib.rs b/src/lib.rs index c7c6b86..ce140ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod scheduled; pub mod store; pub mod log; +pub mod nats; \ No newline at end of file diff --git a/src/log.rs b/src/log.rs index 71cec3c..4eb422b 100644 --- a/src/log.rs +++ b/src/log.rs @@ -4,7 +4,7 @@ pub fn create_log_handler(worker_id: String) -> std::sync::mpsc::Sender(); std::thread::spawn(move || { - let nc = nats::connect("nats://127.0.0.1:4222").expect("failed to connect to nats"); + let nc = crate::nats::nats_connect(); for event in rx { log::debug!("{:?}", event); diff --git a/src/nats.rs b/src/nats.rs new file mode 100644 index 0000000..b0ac860 --- /dev/null +++ b/src/nats.rs @@ -0,0 +1,21 @@ +use base64::{engine::general_purpose::STANDARD, Engine as _}; + +pub fn nats_connect() -> nats::Connection { + let nats_servers = std::env::var("NATS_SERVERS").expect("NATS_SERVERS must be set"); + + log::debug!("connecting to nats: {nats_servers}"); + + let conn = match std::env::var("NATS_CREDS") { + Ok(credentials) => { + // Decode the base64 encoded credentials + let credentials: Vec = STANDARD.decode(credentials).expect("failed to decode credentials"); + let credentials = String::from_utf8(credentials).expect("failed to convert credentials to string"); + let options = nats::Options::with_static_credentials(&credentials).expect("failed to create nats options"); + + options.connect(nats_servers) + }, + Err(_) => nats::connect(nats_servers), + }; + + conn.expect("failed to connect to nats") +} diff --git a/src/scheduled.rs b/src/scheduled.rs index 04d7396..fb5d12d 100644 --- a/src/scheduled.rs +++ b/src/scheduled.rs @@ -64,7 +64,7 @@ pub fn handle_scheduled(db: store::Database) { .unwrap(); let handle = local.spawn_local(async move { - let nc = nats::connect("nats://127.0.0.1:4222").expect("failed to connect to nats"); + let nc = crate::nats::nats_connect(); let sub = nc .queue_subscribe("scheduled", "runner") .expect("failed to subscribe to scheduled");