save settings on exit, use env for discord secrets

pull/5/head
Patrick Cleavelin 2023-03-01 22:01:36 -06:00
parent b4332bb9fc
commit b984f048f6
3 changed files with 94 additions and 84 deletions

View File

@ -9,6 +9,7 @@ use axum::http::{HeaderValue, Method};
use axum::routing::{get, post};
use axum::Router;
use futures::StreamExt;
use settings::{ApiState, DiscordSecret};
use songbird::tracks::TrackQueue;
use std::collections::HashMap;
use std::env;
@ -114,7 +115,15 @@ impl EventHandler for Handler {
}
}
fn spawn_api(settings: Arc<Mutex<Settings>>) -> tokio::task::JoinHandle<()> {
fn spawn_api(settings: Arc<Mutex<Settings>>) {
let secrets = DiscordSecret {
client_id: env::var("DISCORD_CLIENT_ID").expect("expected DISCORD_CLIENT_ID env var"),
client_secret: env::var("DISCORD_CLIENT_SECRET")
.expect("expected DISCORD_CLIENT_SECRET env var"),
};
let state = ApiState { settings, secrets };
tokio::spawn(async move {
let api = Router::new()
.route("/health", get(routes::health))
@ -135,19 +144,17 @@ fn spawn_api(settings: Arc<Mutex<Settings>>) -> tokio::task::JoinHandle<()> {
.allow_headers(Any)
.allow_methods([Method::GET, Method::POST]),
)
.with_state(settings);
.with_state(Arc::new(state));
let addr = SocketAddr::from(([0, 0, 0, 0], 7756));
info!("socket listening on {addr}");
axum::Server::bind(&addr)
.serve(api.into_make_service())
.await
.unwrap();
})
});
}
async fn spawn_bot(settings: Arc<Mutex<Settings>>) -> Vec<tokio::task::JoinHandle<()>> {
let mut tasks = vec![];
async fn spawn_bot(settings: Arc<Mutex<Settings>>) {
let token = env::var("DISCORD_TOKEN").expect("expected DISCORD_TOKEN env var");
let songbird = songbird::Songbird::serenity();
@ -166,13 +173,13 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) -> Vec<tokio::task::JoinHandl
.expect("Error creating client");
info!("Starting bot with token '{token}'");
tasks.push(tokio::spawn(async move {
tokio::spawn(async move {
if let Err(err) = client.start().await {
error!("An error occurred while running the client: {err:?}");
}
}));
});
tasks.push(tokio::spawn(async move {
tokio::spawn(async move {
while let Some(msg) = rx.recv().await {
match msg {
HandlerMessage::Ready(ctx) => {
@ -243,10 +250,7 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) -> Vec<tokio::task::JoinHandl
Some(Intro::Online(intro)) => match songbird::ytdl(&intro.url).await {
Ok(source) => source,
Err(err) => {
error!(
"Error starting youtube source from {}: {err:?}",
intro.url
);
error!("Error starting youtube source from {}: {err:?}", intro.url);
continue;
}
},
@ -261,7 +265,7 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) -> Vec<tokio::task::JoinHandl
continue;
}
}
},
}
None => {
error!(
"Failed to find intro for user {} on guild {} in channel {}, IntroIndex: {}",
@ -289,14 +293,12 @@ async fn spawn_bot(settings: Arc<Mutex<Settings>>) -> Vec<tokio::task::JoinHandl
}
}
}
}));
tasks
});
}
#[tokio::main]
#[instrument]
async fn main() {
async fn main() -> std::io::Result<()> {
tracing_subscriber::fmt::init();
let settings = serde_json::from_str::<Settings>(
@ -308,20 +310,19 @@ async fn main() {
info!("{settings:?}");
let mut tasks = vec![];
let settings = Arc::new(Mutex::new(settings));
if run_api {
tasks.push(spawn_api(settings.clone()));
spawn_api(settings.clone());
}
if run_bot {
tasks.append(&mut spawn_bot(settings.clone()).await);
spawn_bot(settings.clone()).await;
}
let tasks = futures::stream::iter(tasks);
let mut buffered = tasks.buffer_unordered(5);
while buffered.next().await.is_some() {}
info!("spawned background tasks");
let _ = tokio::signal::ctrl_c().await;
settings.lock().await.save()?;
info!("Received Ctrl-C, shuttdown down.");
Ok(())
}

View File

@ -1,21 +1,19 @@
use std::{collections::HashMap, sync::Arc};
use axum::{
body::StreamBody,
extract::{Path, Query, State},
http::HeaderMap,
response::IntoResponse,
Json,
};
use futures::Stream;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use tokio::sync::Mutex;
use tracing::{error, info};
use uuid::Uuid;
use crate::settings::{Auth, AuthUser, GuildSettings, Intro, IntroIndex, Settings, UserSettings};
use crate::settings::{ApiState, Auth, AuthUser, Intro, IntroIndex};
#[derive(Serialize)]
pub(crate) enum IntroResponse<'a> {
@ -47,10 +45,8 @@ pub(crate) struct MeChannel<'a> {
pub(crate) intros: &'a Vec<IntroIndex>,
}
pub(crate) async fn health(State(state): State<Arc<Mutex<Settings>>>) -> Json<Value> {
let settings = state.lock().await;
Json(json!(*settings))
pub(crate) async fn health() -> &'static str {
"Hello!"
}
#[derive(Debug, thiserror::Error)]
@ -78,7 +74,7 @@ struct DiscordUser {
}
pub(crate) async fn auth(
State(settings): State<Arc<Mutex<Settings>>>,
State(state): State<Arc<ApiState>>,
Query(params): Query<HashMap<String, String>>,
) -> Result<Json<Value>, Error> {
let Some(code) = params.get("code") else {
@ -88,8 +84,8 @@ pub(crate) async fn auth(
info!("attempting to get access token with code {}", code);
let mut data = HashMap::new();
data.insert("client_id", "577634620728934400");
data.insert("client_secret", "CLIENT_SECRET_HERE");
data.insert("client_id", state.secrets.client_id.as_str());
data.insert("client_secret", state.secrets.client_secret.as_str());
data.insert("grant_type", "authorization_code");
data.insert("code", code);
data.insert("redirect_uri", "http://localhost:5173/auth");
@ -116,7 +112,7 @@ pub(crate) async fn auth(
.json()
.await?;
let mut settings = settings.lock().await;
let mut settings = state.settings.lock().await;
settings.auth_users.insert(
token.clone(),
AuthUser {
@ -129,11 +125,11 @@ pub(crate) async fn auth(
}
pub(crate) async fn add_intro_to_user(
State(state): State<Arc<Mutex<Settings>>>,
State(state): State<Arc<ApiState>>,
headers: HeaderMap,
Path((guild, channel, intro_index)): Path<(u64, String, usize)>,
) {
let mut settings = state.lock().await;
let mut settings = state.settings.lock().await;
let Some(token) = headers.get("token").and_then(|v| v.to_str().ok()) else { return; };
let user = match settings.auth_users.get(token) {
Some(user) => user.name.clone(),
@ -155,11 +151,11 @@ pub(crate) async fn add_intro_to_user(
}
pub(crate) async fn remove_intro_to_user(
State(state): State<Arc<Mutex<Settings>>>,
State(state): State<Arc<ApiState>>,
headers: HeaderMap,
Path((guild, channel, intro_index)): Path<(u64, String, usize)>,
) {
let mut settings = state.lock().await;
let mut settings = state.settings.lock().await;
let Some(token) = headers.get("token").and_then(|v| v.to_str().ok()) else { return; };
let user = match settings.auth_users.get(token) {
Some(user) => user.name.clone(),
@ -184,20 +180,17 @@ pub(crate) async fn remove_intro_to_user(
}
pub(crate) async fn intros(
State(state): State<Arc<Mutex<Settings>>>,
State(state): State<Arc<ApiState>>,
Path(guild): Path<u64>,
) -> Json<Value> {
let settings = state.lock().await;
let settings = state.settings.lock().await;
let Some(guild) = settings.guilds.get(&guild) else { return Json(json!(IntroResponse::NoGuildFound)); };
Json(json!(IntroResponse::Intros(&guild.intros)))
}
pub(crate) async fn me(
State(state): State<Arc<Mutex<Settings>>>,
headers: HeaderMap,
) -> Json<Value> {
let settings = state.lock().await;
pub(crate) async fn me(State(state): State<Arc<ApiState>>, headers: HeaderMap) -> Json<Value> {
let settings = state.settings.lock().await;
let Some(token) = headers.get("token").and_then(|v| v.to_str().ok()) else { return Json(json!(MeResponse::NoUserFound)); };
let user = match settings.auth_users.get(token) {

View File

@ -2,6 +2,7 @@ use std::{collections::HashMap, sync::Arc};
use serde::{Deserialize, Serialize};
use serenity::prelude::TypeMapKey;
use tracing::trace;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct Auth {
@ -18,6 +19,17 @@ pub(crate) struct AuthUser {
pub name: String,
}
pub(crate) struct ApiState {
pub settings: Arc<tokio::sync::Mutex<Settings>>,
pub secrets: DiscordSecret,
}
#[derive(Clone)]
pub(crate) struct DiscordSecret {
pub client_id: String,
pub client_secret: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Settings {
@ -27,7 +39,7 @@ pub(crate) struct Settings {
pub(crate) run_bot: bool,
pub(crate) guilds: HashMap<u64, GuildSettings>,
#[serde(skip)]
#[serde(default)]
pub(crate) auth_users: HashMap<String, AuthUser>,
}
impl TypeMapKey for Settings {
@ -36,6 +48,7 @@ impl TypeMapKey for Settings {
impl Settings {
pub(crate) fn save(&self) -> Result<(), std::io::Error> {
trace!("attempting to save config");
let serialized = serde_json::to_string_pretty(&self)?;
std::fs::copy(
@ -45,8 +58,11 @@ impl Settings {
chrono::Utc::now().naive_utc().format("%Y-%m-%d %H:%M:%S")
),
)?;
trace!("created copy of original settings");
std::fs::write("./config/settings.json", serialized)?;
trace!("saved settings to disk");
Ok(())
}
}