Skip to content

Commit

Permalink
feat: accept database URLs instead
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiDood committed Dec 20, 2023
1 parent 37839ac commit 70ccf2b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 5 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
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::<api::Config>()?;

// Set up TCP listener
use std::net::{Ipv4Addr, TcpListener};
let listener = TcpListener::bind((Ipv4Addr::UNSPECIFIED, app_port))?;
listener.set_nonblocking(true)?;

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();
Expand Down Expand Up @@ -87,6 +73,6 @@ fn main() -> anyhow::Result<()> {
anyhow::Ok(())
})?;

log::info!("Shutting down");
log::info!("shutting down");
Ok(())
}

0 comments on commit 70ccf2b

Please sign in to comment.