fix analytics route not working (#668)

This commit is contained in:
Geometrically 2023-08-05 12:07:38 -07:00 committed by GitHub
parent 5637d37ee1
commit 1f4ad732fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 32 deletions

View File

@ -6,7 +6,6 @@ use crate::queue::session::AuthQueue;
use crate::routes::ApiError; use crate::routes::ApiError;
use crate::util::env::parse_strings_from_var; use crate::util::env::parse_strings_from_var;
use crate::AnalyticsQueue; use crate::AnalyticsQueue;
use actix_cors::Cors;
use actix_web::{post, web}; use actix_web::{post, web};
use actix_web::{HttpRequest, HttpResponse}; use actix_web::{HttpRequest, HttpResponse};
use chrono::Utc; use chrono::Utc;
@ -18,32 +17,6 @@ use std::sync::Arc;
use url::Url; use url::Url;
use uuid::Uuid; use uuid::Uuid;
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("v2/analytics")
.wrap(
Cors::default()
.allowed_origin_fn(|origin, _req_head| {
let allowed_origins =
parse_strings_from_var("ANALYTICS_ALLOWED_ORIGINS").unwrap_or_default();
allowed_origins.contains(&"*".to_string())
|| allowed_origins
.contains(&origin.to_str().unwrap_or_default().to_string())
})
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![
actix_web::http::header::AUTHORIZATION,
actix_web::http::header::ACCEPT,
actix_web::http::header::CONTENT_TYPE,
])
.max_age(3600),
)
.service(page_view_ingest)
.service(playtime_ingest),
);
}
pub const FILTERED_HEADERS: &[&str] = &[ pub const FILTERED_HEADERS: &[&str] = &[
"authorization", "authorization",
"cookie", "cookie",

View File

@ -1,9 +1,12 @@
use actix_cors::Cors;
use crate::file_hosting::FileHostingError; use crate::file_hosting::FileHostingError;
use crate::util::cors::default_cors; use crate::util::cors::default_cors;
use actix_files::Files; use actix_files::Files;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use actix_web::{web, HttpResponse}; use actix_web::{web, HttpResponse};
use futures::FutureExt; use futures::FutureExt;
use crate::routes::analytics::{page_view_ingest, playtime_ingest};
use crate::util::env::parse_strings_from_var;
pub mod v2; pub mod v2;
pub mod v3; pub mod v3;
@ -12,6 +15,7 @@ mod index;
mod maven; mod maven;
mod not_found; mod not_found;
mod updates; mod updates;
mod analytics;
pub use self::not_found::not_found; pub use self::not_found::not_found;
@ -26,6 +30,29 @@ pub fn root_config(cfg: &mut web::ServiceConfig) {
.wrap(default_cors()) .wrap(default_cors())
.configure(updates::config), .configure(updates::config),
); );
cfg.service(
web::scope("analytics")
.wrap(
Cors::default()
.allowed_origin_fn(|origin, _req_head| {
let allowed_origins =
parse_strings_from_var("ANALYTICS_ALLOWED_ORIGINS").unwrap_or_default();
allowed_origins.contains(&"*".to_string())
|| allowed_origins
.contains(&origin.to_str().unwrap_or_default().to_string())
})
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![
actix_web::http::header::AUTHORIZATION,
actix_web::http::header::ACCEPT,
actix_web::http::header::CONTENT_TYPE,
])
.max_age(3600),
)
.service(page_view_ingest)
.service(playtime_ingest),
);
cfg.service( cfg.service(
web::scope("api/v1") web::scope("api/v1")
.wrap(default_cors()) .wrap(default_cors())

View File

@ -104,7 +104,7 @@ pub async fn count_download(
let url = url::Url::parse(&download_body.url) let url = url::Url::parse(&download_body.url)
.map_err(|_| ApiError::InvalidInput("invalid download URL specified!".to_string()))?; .map_err(|_| ApiError::InvalidInput("invalid download URL specified!".to_string()))?;
let ip = super::analytics::convert_to_ip_v6(&download_body.ip) let ip = crate::routes::analytics::convert_to_ip_v6(&download_body.ip)
.unwrap_or_else(|_| Ipv4Addr::new(127, 0, 0, 1).to_ipv6_mapped()); .unwrap_or_else(|_| Ipv4Addr::new(127, 0, 0, 1).to_ipv6_mapped());
analytics_queue analytics_queue
@ -135,7 +135,7 @@ pub async fn count_download(
.headers .headers
.clone() .clone()
.into_iter() .into_iter()
.filter(|x| !super::analytics::FILTERED_HEADERS.contains(&&*x.0.to_lowercase())) .filter(|x| !crate::routes::analytics::FILTERED_HEADERS.contains(&&*x.0.to_lowercase()))
.collect(), .collect(),
}) })
.await; .await;

View File

@ -1,5 +1,4 @@
mod admin; mod admin;
mod analytics;
mod moderation; mod moderation;
mod notifications; mod notifications;
pub(crate) mod project_creation; pub(crate) mod project_creation;
@ -39,6 +38,4 @@ pub fn config(cfg: &mut actix_web::web::ServiceConfig) {
.configure(version_file::config) .configure(version_file::config)
.configure(versions::config), .configure(versions::config),
); );
cfg.configure(analytics::config);
} }