Fixed some warnings and improved the code quality some more
This commit is contained in:
124
Cargo.lock
generated
124
Cargo.lock
generated
@@ -26,6 +26,55 @@ dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
|
||||
dependencies = [
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.89"
|
||||
@@ -158,6 +207,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"axum",
|
||||
"clap",
|
||||
"common",
|
||||
"csdemo",
|
||||
"diesel",
|
||||
@@ -257,9 +307,9 @@ checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.1.18"
|
||||
version = "1.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476"
|
||||
checksum = "2d74707dde2ba56f86ae90effb3b43ddd369504387e718014de010cec7959800"
|
||||
dependencies = [
|
||||
"shlex",
|
||||
]
|
||||
@@ -297,12 +347,58 @@ dependencies = [
|
||||
"half",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.5.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
|
||||
|
||||
[[package]]
|
||||
name = "collection_literals"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "186dce98367766de751c42c4f03970fc60fc012296e706ccbb9d5df9b6c1e271"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
|
||||
|
||||
[[package]]
|
||||
name = "common"
|
||||
version = "0.1.0"
|
||||
@@ -1178,6 +1274,12 @@ version = "2.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4"
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.12.1"
|
||||
@@ -1628,9 +1730,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.20.0"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33ea5043e58958ee56f3e15a90aee535795cd7dfd319846288d93c5b57d85cbe"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "openssl"
|
||||
@@ -1839,9 +1941,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "postgres-types"
|
||||
version = "0.2.7"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02048d9e032fb3cc3413bbf7b83a15d84a5d419778e2628751896d856498eee9"
|
||||
checksum = "f66ea23a2d0e5734297357705193335e0a957696f34bed2f2faefacb2fec336f"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"fallible-iterator",
|
||||
@@ -2886,9 +2988,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tokio-postgres"
|
||||
version = "0.7.11"
|
||||
version = "0.7.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "03adcf0147e203b6032c0b2d30be1415ba03bc348901f3ff1cc0df6a733e60c3"
|
||||
checksum = "3b5d3742945bc7d7f210693b0c58ae542c6fd47b17adbbda0885f3dcb34a6bdb"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"byteorder",
|
||||
@@ -3250,6 +3352,12 @@ version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86bd8d4e895da8537e5315b8254664e6b769c4ff3db18321b297a1e7004392e3"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.10.0"
|
||||
|
||||
@@ -27,3 +27,4 @@ common = { path = "../common/" }
|
||||
|
||||
csdemo = { package = "csdemo", git = "https://github.com/Lol3rrr/csdemo.git", ref = "main" }
|
||||
memmap2 = { version = "0.9" }
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
|
||||
@@ -16,30 +16,36 @@ pub mod steam {
|
||||
pub steamid: String,
|
||||
pub personaname: String,
|
||||
#[serde(flatten)]
|
||||
other: HashMap<String, serde_json::Value>,
|
||||
_other: HashMap<String, serde_json::Value>,
|
||||
}
|
||||
|
||||
pub fn router(url: &str, callback_path: &str) -> axum::Router {
|
||||
struct SteamApiState {
|
||||
openid: steam_openid::SteamOpenId,
|
||||
api_key: String,
|
||||
}
|
||||
|
||||
pub fn router(url: &str, callback_path: &str, api_key: impl Into<String>) -> axum::Router {
|
||||
axum::Router::new()
|
||||
.route("/login", axum::routing::get(steam_login))
|
||||
.route("/callback", axum::routing::get(steam_callback))
|
||||
.with_state(Arc::new(
|
||||
steam_openid::SteamOpenId::new(url, callback_path).unwrap(),
|
||||
))
|
||||
.with_state(Arc::new(SteamApiState {
|
||||
openid: steam_openid::SteamOpenId::new(url, callback_path).unwrap(),
|
||||
api_key: api_key.into(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(openid))]
|
||||
#[tracing::instrument(skip(state))]
|
||||
async fn steam_login(
|
||||
State(openid): State<Arc<steam_openid::SteamOpenId>>,
|
||||
State(state): State<Arc<SteamApiState>>,
|
||||
) -> Result<axum::response::Redirect, axum::http::StatusCode> {
|
||||
let url = openid.get_redirect_url();
|
||||
let url = state.openid.get_redirect_url();
|
||||
|
||||
Ok(axum::response::Redirect::to(url))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(openid, session, request))]
|
||||
#[tracing::instrument(skip(state, session, request))]
|
||||
async fn steam_callback(
|
||||
State(openid): State<Arc<steam_openid::SteamOpenId>>,
|
||||
State(state): State<Arc<SteamApiState>>,
|
||||
mut session: crate::UserSession,
|
||||
request: axum::extract::Request,
|
||||
) -> Result<axum::response::Redirect, axum::http::StatusCode> {
|
||||
@@ -50,12 +56,12 @@ pub mod steam {
|
||||
axum::http::StatusCode::BAD_REQUEST
|
||||
})?;
|
||||
|
||||
let id = openid.verify(query).await.map_err(|e| {
|
||||
let id = state.openid.verify(query).await.map_err(|e| {
|
||||
tracing::error!("Verifying OpenID: {:?}", e);
|
||||
axum::http::StatusCode::BAD_REQUEST
|
||||
})?;
|
||||
|
||||
let steam_client = crate::steam_api::Client::new(std::env::var("STEAM_API_KEY").unwrap());
|
||||
let steam_client = crate::steam_api::Client::new(&state.api_key);
|
||||
let profile_response_data: ProfileInfoResponse = match steam_client
|
||||
.get(
|
||||
"http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/",
|
||||
@@ -79,7 +85,7 @@ pub mod steam {
|
||||
})
|
||||
.on_conflict(crate::schema::users::dsl::steamid)
|
||||
.do_update()
|
||||
.set((crate::schema::users::dsl::name.eq(player.personaname)));
|
||||
.set(crate::schema::users::dsl::name.eq(player.personaname));
|
||||
tracing::debug!("Running Query: {:?}", query);
|
||||
|
||||
if let Err(e) = query.execute(&mut db_con).await {
|
||||
@@ -141,14 +147,26 @@ pub mod user {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RouterConfig {
|
||||
pub steam_api_key: String,
|
||||
pub steam_callback_base_url: String,
|
||||
pub steam_callback_path: String,
|
||||
pub upload_dir: std::path::PathBuf,
|
||||
}
|
||||
|
||||
pub fn router(
|
||||
base_analysis: tokio::sync::mpsc::UnboundedSender<crate::analysis::AnalysisInput>,
|
||||
config: RouterConfig,
|
||||
) -> axum::Router {
|
||||
axum::Router::new()
|
||||
.nest(
|
||||
"/steam/",
|
||||
steam::router("http://192.168.0.156:3000", "/api/steam/callback"),
|
||||
steam::router(
|
||||
&config.steam_callback_base_url,
|
||||
&config.steam_callback_path,
|
||||
config.steam_api_key,
|
||||
),
|
||||
)
|
||||
.nest("/demos/", demos::router("uploads/", base_analysis))
|
||||
.nest("/demos/", demos::router(config.upload_dir, base_analysis))
|
||||
.nest("/user/", user::router())
|
||||
}
|
||||
|
||||
@@ -95,6 +95,8 @@ async fn upload(
|
||||
|
||||
let mut db_con = crate::db_connection().await;
|
||||
|
||||
// Turn all of this into a single transaction
|
||||
|
||||
let query =
|
||||
diesel::dsl::insert_into(crate::schema::demos::dsl::demos).values(crate::models::Demo {
|
||||
demo_id,
|
||||
@@ -102,11 +104,14 @@ async fn upload(
|
||||
});
|
||||
query.execute(&mut db_con).await.unwrap();
|
||||
|
||||
state.base_analysis.send(crate::analysis::AnalysisInput {
|
||||
steamid: steam_id.to_string(),
|
||||
demoid: demo_id,
|
||||
path: demo_file_path,
|
||||
});
|
||||
state
|
||||
.base_analysis
|
||||
.send(crate::analysis::AnalysisInput {
|
||||
steamid: steam_id.to_string(),
|
||||
demoid: demo_id,
|
||||
path: demo_file_path,
|
||||
})
|
||||
.unwrap();
|
||||
let processing_query =
|
||||
diesel::dsl::insert_into(crate::schema::processing_status::dsl::processing_status)
|
||||
.values(crate::models::ProcessingStatus { demo_id, info: 0 });
|
||||
@@ -146,18 +151,21 @@ async fn analyise(
|
||||
}
|
||||
|
||||
let user_folder = std::path::Path::new(&state.upload_folder).join(format!("{}/", steam_id));
|
||||
state.base_analysis.send(crate::analysis::AnalysisInput {
|
||||
path: user_folder.join(format!("{}.dem", demo_id)),
|
||||
demoid: demo_id,
|
||||
steamid: steam_id.to_string(),
|
||||
});
|
||||
state
|
||||
.base_analysis
|
||||
.send(crate::analysis::AnalysisInput {
|
||||
path: user_folder.join(format!("{}.dem", demo_id)),
|
||||
demoid: demo_id,
|
||||
steamid: steam_id.to_string(),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(session))]
|
||||
#[tracing::instrument(skip(_session))]
|
||||
async fn info(
|
||||
session: UserSession,
|
||||
_session: UserSession,
|
||||
Path(demo_id): Path<i64>,
|
||||
) -> Result<axum::response::Json<common::DemoInfo>, axum::http::StatusCode> {
|
||||
tracing::info!("Get info for Demo: {:?}", demo_id);
|
||||
|
||||
@@ -19,25 +19,6 @@ impl DieselStore {
|
||||
Self {}
|
||||
}
|
||||
|
||||
fn id_to_bytes(&self, val: i128) -> Vec<i64> {
|
||||
let id_bytes = val.to_be_bytes();
|
||||
vec![
|
||||
i64::from_be_bytes((id_bytes[0..8]).try_into().unwrap()),
|
||||
i64::from_be_bytes((id_bytes[8..16]).try_into().unwrap()),
|
||||
]
|
||||
}
|
||||
fn bytes_to_id(&self, val: Vec<i64>) -> i128 {
|
||||
assert_eq!(2, val.len());
|
||||
|
||||
let fb = val[0].to_be_bytes();
|
||||
let sb = val[1].to_be_bytes();
|
||||
|
||||
i128::from_be_bytes([
|
||||
fb[0], fb[1], fb[2], fb[3], fb[4], fb[5], fb[6], fb[7], sb[0], sb[1], sb[2], sb[3],
|
||||
sb[4], sb[5], sb[6], sb[7],
|
||||
])
|
||||
}
|
||||
|
||||
fn expiry_to_string(&self, expiry_date: &time::OffsetDateTime) -> String {
|
||||
expiry_date.format(&EXPIRY_FORMAT).unwrap()
|
||||
}
|
||||
@@ -54,7 +35,6 @@ impl tower_sessions::SessionStore for DieselStore {
|
||||
) -> tower_sessions::session_store::Result<()> {
|
||||
let db_id = session_record.id.0.to_string();
|
||||
|
||||
let data = serde_json::to_value(&session_record.data).unwrap();
|
||||
let expiry_date = self.expiry_to_string(&session_record.expiry_date);
|
||||
|
||||
let steamid = session_record
|
||||
|
||||
@@ -43,13 +43,12 @@ pub async fn get_demo_from_upload(
|
||||
pub mod api;
|
||||
pub mod steam_api;
|
||||
|
||||
#[tracing::instrument(skip(upload_folder, base_analysis_tx))]
|
||||
pub async fn run_api<UP>(
|
||||
upload_folder: UP,
|
||||
#[tracing::instrument(skip(upload_folder, base_analysis_tx, steam_api_key))]
|
||||
pub async fn run_api(
|
||||
upload_folder: impl Into<std::path::PathBuf>,
|
||||
base_analysis_tx: tokio::sync::mpsc::UnboundedSender<analysis::AnalysisInput>,
|
||||
) where
|
||||
UP: Into<std::path::PathBuf>,
|
||||
{
|
||||
steam_api_key: impl Into<String>,
|
||||
) {
|
||||
let upload_folder: std::path::PathBuf = upload_folder.into();
|
||||
|
||||
let session_store = crate::diesel_sessionstore::DieselStore::new();
|
||||
@@ -64,7 +63,18 @@ pub async fn run_api<UP>(
|
||||
}
|
||||
|
||||
let router = axum::Router::new()
|
||||
.nest("/api/", crate::api::router(base_analysis_tx))
|
||||
.nest(
|
||||
"/api/",
|
||||
crate::api::router(
|
||||
base_analysis_tx,
|
||||
crate::api::RouterConfig {
|
||||
steam_api_key: steam_api_key.into(),
|
||||
steam_callback_base_url: "http://192.168.0.156:3000".into(),
|
||||
steam_callback_path: "/api/steam/callback".into(),
|
||||
upload_dir: upload_folder.clone(),
|
||||
},
|
||||
),
|
||||
)
|
||||
.layer(session_layer)
|
||||
.nest_service(
|
||||
"/",
|
||||
@@ -75,7 +85,7 @@ pub async fn run_api<UP>(
|
||||
axum::serve(listener, router).await.unwrap();
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
#[tracing::instrument(skip(base_analysis_rx))]
|
||||
pub async fn run_analysis(
|
||||
mut base_analysis_rx: tokio::sync::mpsc::UnboundedReceiver<analysis::AnalysisInput>,
|
||||
) {
|
||||
@@ -89,7 +99,7 @@ pub async fn run_analysis(
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
dbg!(&result);
|
||||
tracing::debug!("Analysis-Result: {:?}", result);
|
||||
|
||||
let mut db_con = crate::db_connection().await;
|
||||
|
||||
@@ -109,11 +119,12 @@ pub async fn run_analysis(
|
||||
db_con
|
||||
.transaction::<'_, '_, '_, _, diesel::result::Error, _>(|conn| {
|
||||
Box::pin(async move {
|
||||
store_info_query.execute(conn).await.map(|e| ())?;
|
||||
update_process_info.execute(conn).await.map(|e| ())?;
|
||||
store_info_query.execute(conn).await?;
|
||||
update_process_info.execute(conn).await?;
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.await;
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt;
|
||||
|
||||
static UPLOAD_FOLDER: &str = "uploads/";
|
||||
|
||||
const MIGRATIONS: diesel_async_migrations::EmbeddedMigrations =
|
||||
diesel_async_migrations::embed_migrations!("../migrations/");
|
||||
|
||||
@@ -9,8 +7,22 @@ async fn run_migrations(connection: &mut diesel_async::AsyncPgConnection) {
|
||||
MIGRATIONS.run_pending_migrations(connection).await.unwrap();
|
||||
}
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
struct CliArgs {
|
||||
#[clap(long = "upload-folder", default_value = "uploads/")]
|
||||
upload_folder: std::path::PathBuf,
|
||||
|
||||
#[clap(long = "api", default_value_t = true)]
|
||||
api: bool,
|
||||
|
||||
#[clap(long = "analysis", default_value_t = true)]
|
||||
analysis: bool,
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() {
|
||||
use clap::Parser;
|
||||
|
||||
let registry = tracing_subscriber::Registry::default()
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.with(tracing_subscriber::filter::filter_fn(|meta| {
|
||||
@@ -20,6 +32,8 @@ async fn main() {
|
||||
|
||||
tracing::info!("Starting...");
|
||||
|
||||
let args = CliArgs::parse();
|
||||
|
||||
tracing::info!("Applying Migrations");
|
||||
run_migrations(&mut backend::db_connection().await).await;
|
||||
tracing::info!("Completed Migrations");
|
||||
@@ -29,8 +43,24 @@ async fn main() {
|
||||
|
||||
let mut component_set = tokio::task::JoinSet::new();
|
||||
|
||||
component_set.spawn(backend::run_api(UPLOAD_FOLDER, base_analysis_tx));
|
||||
component_set.spawn(backend::run_analysis(base_analysis_rx));
|
||||
if args.api {
|
||||
let steam_api_key = match std::env::var("STEAM_API_KEY") {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
tracing::error!("Missing 'STEAM_API_KEY' environment variable - {:?}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
component_set.spawn(backend::run_api(
|
||||
args.upload_folder.clone(),
|
||||
base_analysis_tx,
|
||||
steam_api_key,
|
||||
));
|
||||
}
|
||||
if args.analysis {
|
||||
component_set.spawn(backend::run_analysis(base_analysis_rx));
|
||||
}
|
||||
|
||||
component_set.join_all().await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user