-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathmain.rs
156 lines (127 loc) · 4.38 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::path::PathBuf;
use std::{env, sync::Arc};
use anyhow::Result;
use tokio::signal;
use noalbs::{chat::ChatPlatform, config, Noalbs};
use tracing::warn;
#[tokio::main]
async fn main() -> Result<()> {
dotenv::dotenv().ok();
noalbs::print_logo();
let _ = print_if_new_version().await;
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "noalbs=info");
}
if cfg!(windows) {
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_ansi(false)
.init();
} else {
tracing_subscriber::fmt::init();
}
check_env_file();
let user_manager = noalbs::user_manager::UserManager::new();
// Used to send messages to the chat handler
let (chat_tx, chat_rx) = tokio::sync::mpsc::channel(100);
let mut chat_handler = noalbs::chat::ChatHandler::new(chat_rx, user_manager.clone());
if env::var("CONFIG_DIR").is_ok() {
let users = load_users_from_dir(env::var("CONFIG_DIR")?, chat_tx.clone()).await?;
for user in users {
user_manager.add(user?).await;
}
} else {
let user = load_user_from_file("config.json".to_owned(), chat_tx.clone()).await?;
user_manager.add(user).await;
}
if env::var("TWITCH_BOT_USERNAME").is_ok() {
let bot_username = env::var("TWITCH_BOT_USERNAME")?;
let oauth = env::var("TWITCH_BOT_OAUTH")?;
let twitch = noalbs::chat::Twitch::new(bot_username, oauth, chat_tx.clone());
for (_, username) in user_manager
.get_all_chat()
.await
.iter()
.filter(|(platform, _)| platform == &ChatPlatform::Twitch)
{
twitch.join_channel(username.to_lowercase());
}
chat_handler.add_chat_sender(ChatPlatform::Twitch, Arc::new(twitch));
};
let _ = tokio::task::spawn(async move {
chat_handler.handle_messages().await;
});
if env::var("API_PORT").is_ok() {
let port: u16 = env::var("API_PORT")?.parse()?;
let webserver = noalbs::web_server::WebServer::new(port, user_manager.clone());
webserver.run().await;
}
match signal::ctrl_c().await {
Ok(()) => {}
Err(err) => {
eprintln!("Unable to listen for shutdown signal: {}", err);
}
}
Ok(())
}
pub async fn load_user_from_file<P>(
path: P,
broadcast_tx: noalbs::ChatSender,
) -> Result<Noalbs, noalbs::error::Error>
where
P: Into<PathBuf>,
{
let path = path.into();
let file = config::File { name: path };
Noalbs::new(Box::new(file), broadcast_tx).await
}
pub async fn load_users_from_dir<P>(
dir: P,
broadcast_tx: noalbs::ChatSender,
) -> Result<Vec<Result<Noalbs, noalbs::error::Error>>>
where
P: Into<PathBuf>,
{
let dir = dir.into();
let noalbs_users = std::fs::read_dir(dir)?
.filter_map(|f| f.ok())
.map(|f| f.path())
.filter(|e| e.extension().unwrap() == "json")
.map(|p| Noalbs::new(Box::new(config::File { name: p }), broadcast_tx.clone()))
.collect::<Vec<_>>();
let noalbs_users = futures_util::future::join_all(noalbs_users).await;
Ok(noalbs_users)
}
async fn print_if_new_version() -> Result<(), noalbs::error::Error> {
let url = "https://api.github.com/repos/715209/nginx-obs-automatic-low-bitrate-switching/releases/latest";
let dlu = "https://github.com/715209/nginx-obs-automatic-low-bitrate-switching/releases/latest";
let client = reqwest::Client::new();
let res = client
.get(url)
.header(
reqwest::header::USER_AGENT,
"nginx-obs-automatic-low-bitrate-switching",
)
.send()
.await?
.json::<GithubApi>()
.await?;
if !res.tag_name.contains(noalbs::VERSION) {
println!("NEW VERSION {} AVAILABLE", res.tag_name);
println!("Download at {}\n", dlu);
}
Ok(())
}
#[derive(serde::Deserialize, Debug)]
struct GithubApi {
tag_name: String,
}
fn check_env_file() {
if env::var("TWITCH_BOT_USERNAME").is_err() {
warn!("Couldn't load chat credentials from .env - continuing without connecting to chat.");
warn!(
"Hint: rename env.example to .env and edit it with your login information - see README"
);
warn!("https://github.com/715209/nginx-obs-automatic-low-bitrate-switching/tree/v2#readme");
};
}