Skip to content

Commit

Permalink
feat: #62 initial setup for cli tool (#64)
Browse files Browse the repository at this point in the history
- used serenity lib to build discord bot

- made http requests with reqwest

- updated .gitignore and pre-commit for rust integration
  • Loading branch information
miguelcsx authored Sep 26, 2024
1 parent 2bd869a commit a3ab2ae
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 3 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,25 @@ cython_debug/
data/db/*.db
data/reports/*
data/mindmaps/*

# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ repos:
- id: rust-check
name: Check Rust Code
entry: ./scripts/check_rust.sh
files: discord/
language: rust
12 changes: 12 additions & 0 deletions discord/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "discord"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = "4.5.18"
dotenv = "0.15.0"
once_cell = "1.19.0"
reqwest = "0.12.7"
serenity = "0.12.2"
tokio = "1.40.0"
Empty file added discord/src/cli/mod.rs
Empty file.
Empty file added discord/src/commands/mod.rs
Empty file.
3 changes: 3 additions & 0 deletions discord/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// discord/src/config/mod.rs

pub mod settings;
35 changes: 35 additions & 0 deletions discord/src/config/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// discord/src/config/settings.rs

use dotenv::dotenv;
use once_cell::sync::OnceCell;
use std::env;


#[derive(Debug, Clone)]
pub struct Config {
pub api_url: String,
pub discord_token: String,
}

// Initialize a static instance of the configuration
static CONFIG: OnceCell<Config> = OnceCell::new();

pub fn load_config() {
// Load the configuration from the environment
dotenv().ok();

let api_url = env::var("API_URL").expect("API_URL must be set");
let discord_token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN must be set");

let config = Config {
api_url,
discord_token,
};

// Set the configuration
CONFIG.set(config).expect("Failed to set configuration");
}

pub fn get_config() -> &'static Config {
CONFIG.get().expect("Configuration not loaded")
}
Empty file added discord/src/events/mod.rs
Empty file.
24 changes: 24 additions & 0 deletions discord/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
mod config;
mod services;

use config::settings;
use services::api_client;

#[tokio::main(flavor = "current_thread")]
async fn main() {
// Load the configuration
settings::load_config();

// Get the configuration
let config = settings::get_config();

// Print the configuration
println!("API URL: {}", config.api_url);
println!("Discord Token: {}", config.discord_token);

// Get data from the API
match api_client::get("").await {
Ok(response) => println!("Response: {}", response),
Err(err) => eprintln!("Error: {}", err),
}
}
31 changes: 31 additions & 0 deletions discord/src/services/api_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// discord/src/services/api_client.rs

use reqwest::Client;
use crate::config::settings::get_config;
use std::error::Error;


pub async fn get(endpoint: &str) -> Result<String, Box<dyn Error>> {
// Get the configuration
let config = get_config();

// Construct the URL
let url = format!("{}/{}", config.api_url, endpoint);

// Create a new reqwest client
let client = Client::new();

// Send a GET request to the API
let response = client.get(&url)
.header("Authorization", &config.discord_token)
.send()
.await?;

if response.status().is_success() {
let body = response.text().await?;
Ok(body)
} else {
Err("Failed to get data".into())
}
}

3 changes: 3 additions & 0 deletions discord/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// discord/src/services/mod.rs

pub mod api_client;
Empty file added discord/src/utils/mod.rs
Empty file.
File renamed without changes.
10 changes: 8 additions & 2 deletions scripts/check_rust.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#!/bin/sh

# Change to the discord directory
cd discord || {
echo "Directory 'discord' does not exist."
exit 1
}

# Set up Rust environment (assuming Rust is already installed)
rust_version="1.57.0"

# Optionally install Rustup
# Optionally install Rustup (uncomment if needed)
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

# Install Rust components
Expand All @@ -16,4 +22,4 @@ cargo check --all
cargo fmt --all -- --check

# Run Clippy
cargo clippy --all -- -D warnings
cargo clippy --all -- -D warningss

0 comments on commit a3ab2ae

Please sign in to comment.