From c2899951f6463f75af088e6e0c145613e5102ac3 Mon Sep 17 00:00:00 2001 From: Aleksei Voronov Date: Thu, 7 Sep 2023 19:20:38 +0200 Subject: [PATCH] Read chatgpt and postgresql credentials from .env --- .env.example | 2 ++ .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 1 + README.md | 7 +++++++ src/main.rs | 27 +++++++++++++++++++++++---- 6 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..4be004e --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +CHAT_GPT_API_KEY="fake-chat-gpt-key" +DATABASE_URL="postgres://postgres:password@localhost/nederlandskie" diff --git a/.gitignore b/.gitignore index ea8c4bf..fedaa2b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.env diff --git a/Cargo.lock b/Cargo.lock index 8e8d618..62288e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -481,6 +481,12 @@ dependencies = [ "subtle", ] +[[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" @@ -1239,6 +1245,7 @@ dependencies = [ "chat-gpt-lib-rs", "chrono", "ciborium", + "dotenv", "futures", "libipld-core", "rs-car", diff --git a/Cargo.toml b/Cargo.toml index 6cc5819..79ca759 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ atrium-xrpc = "0.4.0" chat-gpt-lib-rs = "0.2.1" chrono = "0.4.29" ciborium = "0.2.1" +dotenv = "0.15.0" futures = "0.3.28" libipld-core = { version = "0.16.0", features = ["serde-codec"] } rs-car = "0.4.1" diff --git a/README.md b/README.md index 646d9a8..e150250 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,13 @@ Heavily WIP. Doesn't work yet at all, but does read the stream of posts as they - [ ] Publish the feed - [ ] Handle deleting of posts +## Initial setup + +Copy `.env.example` into `.env` and set up the environment variables within: + +- `CHAT_GPT_API_KEY` for your ChatGPT key +- `DATABASE_URL` for PostgreSQL credentials + ## Running `cargo run` diff --git a/src/main.rs b/src/main.rs index 55b9ba6..4cf6b64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,10 @@ mod processes; mod services; +use std::env; + use anyhow::Result; +use dotenv::dotenv; use crate::processes::post_saver::PostSaver; use crate::processes::profile_classifier::ProfileClassifier; @@ -9,13 +12,29 @@ use crate::services::ai::AI; use crate::services::bluesky::Bluesky; use crate::services::database::Database; +struct Config { + chat_gpt_api_key: String, + database_url: String, +} + +impl Config { + fn load() -> Result { + dotenv()?; + + Ok(Self { + chat_gpt_api_key: env::var("CHAT_GPT_API_KEY")?, + database_url: env::var("DATABASE_URL")?, + }) + } +} + #[tokio::main] async fn main() -> Result<()> { - // TODO: Use env vars - let ai = AI::new("fake-api-key", "https://api.openai.com"); + let config = Config::load()?; + + let ai = AI::new(&config.chat_gpt_api_key, "https://api.openai.com"); let bluesky = Bluesky::new("https://bsky.social"); - let database = - Database::connect("postgres://postgres:password@localhost/nederlandskie").await?; + let database = Database::connect(&config.database_url).await?; let post_saver = PostSaver::new(&database, &bluesky); let profile_classifier = ProfileClassifier::new(&database, &ai, &bluesky);