diff --git a/README.md b/README.md index 098fae5..f01c47f 100644 --- a/README.md +++ b/README.md @@ -4,17 +4,13 @@ _Quizzo_ is a [Discord bot](https://discord.com/api/oauth2/authorize?client_id=8 # Development This bot is powered by the [Twilight library](https://github.com/twilight-rs/twilight) for the [Rust programming language](https://www.rust-lang.org/tools/install). Before running the bot, the following environment variables must be set: -**Variable** | **Description** | Required? | Default -------------- | ----------------------------------------------------------------------------------------- | :-------: | ------: -`PORT` | Network port to bind to when launching the bot. | ✔ | -`PUB_KEY` | Hex-encoded cryptograhpic public key provided by the [Discord Developer Portal][discord]. | ✔ | -`APP_ID` | Application ID provided by the [Discord Developer Portal][discord]. | ✔ | -`BOT_TOKEN` | Bot token provided by the [Discord Developer Portal][discord]. | ✔ | -`PG_PORT` | Port at which the PostgreSQL instance is hosted. | ❌ | `5432` -`PG_HOSTNAME` | Hostname at which the PostgreSQL instance is hosted. | ✔ | -`PG_DATABASE` | Default database to which the PostgreSQL driver must connect to. | ✔ | -`PG_USERNAME` | Username for PostgreSQL user authentication. | ✔ | -`PG_PASSWORD` | Password for PostgreSQL user authentication. | ✔ | +**Variable** | **Description** | Required? | Default +------------ | ----------------------------------------------------------------------------------------- | :-------: | ------: +`PORT` | Network port to bind to when launching the bot. | ✔ | +`PUB_KEY` | Hex-encoded cryptograhpic public key provided by the [Discord Developer Portal][discord]. | ✔ | +`APP_ID` | Application ID provided by the [Discord Developer Portal][discord]. | ✔ | +`BOT_TOKEN` | Bot token provided by the [Discord Developer Portal][discord]. | ✔ | +`PG_URL` | URL at which the PostgreSQL instance is hosted. | ❌ | `5432` [discord]: https://discord.com/developers/applications diff --git a/src/main.rs b/src/main.rs index 0c5c60f..cd98869 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,34 +1,21 @@ fn main() -> anyhow::Result<()> { env_logger::init(); - log::info!("Starting up"); + log::info!("starting up"); // Retrieve the public key - use std::env::{var, VarError}; + use std::env::var; let pub_key = var("PUB_KEY")?.into_bytes(); let mut pub_bytes = [0; 32]; hex::decode_to_slice(pub_key, &mut pub_bytes)?; let pub_key = api::VerifyingKey::from_bytes(&pub_bytes)?; + log::debug!("loaded public key"); // Set up Postgres driver configuration let app_port = var("PORT")?.parse()?; let app_id = var("APP_ID")?.parse()?; let bot_token = var("BOT_TOKEN")?; - let config = { - let username = var("PG_USERNAME")?; - let password = var("PG_PASSWORD")?; - let hostname = var("PG_HOSTNAME")?; - let database = var("PG_DATABASE")?; - let db_port = match var("PG_PORT") { - Ok(port) => port.parse()?, - Err(VarError::NotPresent) => 5432, - Err(err) => return Err(anyhow::Error::new(err)), - }; - let mut config = api::Config::new(); - config.user(&username).password(&password).host(&hostname).port(db_port).dbname(&database); - config - }; + let config = var("PG_URL")?.parse::()?; - // Set up TCP listener use std::net::{Ipv4Addr, TcpListener}; let listener = TcpListener::bind((Ipv4Addr::UNSPECIFIED, app_port))?; listener.set_nonblocking(true)?; @@ -36,7 +23,6 @@ fn main() -> anyhow::Result<()> { let addr = listener.local_addr()?; log::info!("listening to {addr}"); - // Set up runtime let runtime = tokio::runtime::Builder::new_multi_thread().enable_io().enable_time().build()?; let tcp = { let _guard = runtime.enter(); @@ -87,6 +73,6 @@ fn main() -> anyhow::Result<()> { anyhow::Ok(()) })?; - log::info!("Shutting down"); + log::info!("shutting down"); Ok(()) }